Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter fields by user on response of /api/vector/data #980

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions g3w-admin/core/api/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ def response_config_mode(self, request):
'fields': fields,
}

# Filter fields by user
if self.request.user:
visiblefields = self.layer.visible_fields_for_user(self.request.user)
if len(visiblefields) != len(vector_params['fields']):
newfields = []
for f in vector_params['fields']:
if f['name'] in visiblefields:
newfields.append(f)

if newfields:
vector_params['fields'] = newfields

# post_create_maplayerattributes signal
post_create_maplayerattributes.send(
self, layer=self.layer, vector_params=vector_params)
Expand Down
31 changes: 31 additions & 0 deletions g3w-admin/qdjango/tests/test_column_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@ def test_vector_api(self):
self.assertIsNotNone(record['AREA'])
self.assertIsNotNone(record['SOURCETHM'])

# Test for /api/vector/config
response = self._testApiCallAdmin01(
'core-vector-api', [
'config',
'qdjango',
self.world.project.pk,
self.world.qgis_layer.id()])

resp = json.loads(response.content)

fields = [f['name'] for f in resp['vector']['fields']]

self.assertTrue('AREA' in fields)
self.assertTrue('SOURCETHM' in fields)


acl = ColumnAcl(layer=self.world, user=self.test_user1,
restricted_fields=['AREA', 'SOURCETHM'])
acl.save()
Expand All @@ -306,6 +322,21 @@ def test_vector_api(self):
self.assertIsNone(record['AREA'])
self.assertIsNone(record['SOURCETHM'])

# Test for /api/vector/config
response = self._testApiCallAdmin01(
'core-vector-api', [
'config',
'qdjango',
self.world.project.pk,
self.world.qgis_layer.id()])

resp = json.loads(response.content)

fields = [f['name'] for f in resp['vector']['fields']]

self.assertFalse('AREA' in fields)
self.assertFalse('SOURCETHM' in fields)

# Test for download API
# -------------------------------------------------

Expand Down
Loading