Skip to content

Commit 0289ad1

Browse files
authored
Fix user permissions for members of groups and orgs (#471)
1 parent d3aaa14 commit 0289ad1

File tree

13 files changed

+186
-26
lines changed

13 files changed

+186
-26
lines changed

ckanext/querytool/controllers/querytool.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ def querytool_edit(self, querytool=None, data=None,
153153
}
154154

155155
context = _get_context()
156-
try:
157-
check_access('querytool_update', context, data_dict)
158-
except NotAuthorized:
159-
abort(403, _('Not authorized to see this page'))
160156

161157
_querytool = _get_action('querytool_get', data_dict)
158+
user_type = helpers.get_user_permission_type(c.userobj, _querytool.get('group')) if _querytool else []
159+
160+
if _querytool and user_type in ['member', None] and c.userobj.sysadmin is False and data_dict.get('name') != '':
161+
abort(403, _('Not authorized to see this page'))
162+
else:
163+
if user_type not in ['admin', 'editor'] and data_dict.get('name') != '':
164+
try:
165+
check_access('querytool_update', context, data_dict)
166+
except NotAuthorized:
167+
abort(403, _('Not authorized to see this page'))
162168

163169
if _querytool is None and len(querytool) > 0:
164170
abort(404, _('Report not found.'))
@@ -312,12 +318,17 @@ def edit_visualizations(self, querytool=None, data=None,
312318

313319
context = _get_context()
314320

315-
try:
316-
check_access('querytool_update', context, data_dict)
317-
except NotAuthorized:
318-
abort(403, _('Not authorized to see this page'))
319-
320321
_querytool = _get_action('querytool_get', data_dict)
322+
user_type = helpers.get_user_permission_type(c.userobj, _querytool.get('group')) if _querytool else []
323+
324+
if _querytool and user_type in ['member', None] and c.userobj.sysadmin is False:
325+
abort(403, _('Not authorized to see this page'))
326+
else:
327+
if user_type not in ['admin', 'editor']:
328+
try:
329+
check_access('querytool_update', context, data_dict)
330+
except NotAuthorized:
331+
abort(403, _('Not authorized to see this page'))
321332

322333
if _querytool is None and len(querytool) > 0:
323334
abort(404, _('Report not found.'))
@@ -683,6 +694,7 @@ def edit_visualizations(self, querytool=None, data=None,
683694
data['y_axis_values'] = ','.join(map(
684695
lambda column: column['name'],
685696
data['y_axis_columns']))
697+
data['owner_org'] = _querytool.get('owner_org')
686698

687699
vars = {'data': data, 'errors': errors,
688700
'error_summary': error_summary}

ckanext/querytool/helpers.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ def get_map_data(geojson_url, map_key_field, data_key_field,
502502

503503
@functools32.lru_cache(maxsize=128)
504504
def get_resource_data(sql_string):
505+
context = {}
506+
507+
if get_is_admin_or_editor_of_any_group(c.userobj):
508+
context['ignore_auth'] = True
505509

506510
response = toolkit.get_action('datastore_search_sql')(
507511
{}, {'sql': sql_string}
@@ -646,6 +650,40 @@ def get_user_permission(userobj):
646650
return True
647651

648652

653+
def get_orgs_for_user(userobj, org):
654+
orgs = _get_action('organization_list_for_user', {'id': userobj.id})
655+
org_names = [o['name'] for o in orgs]
656+
657+
if org in org_names:
658+
return True
659+
else:
660+
return False
661+
662+
663+
def get_all_orgs_for_user(userobj):
664+
orgs = _get_action('organization_list_for_user', {'id': userobj.id})
665+
666+
if orgs:
667+
return orgs
668+
669+
670+
def get_organization(org_id):
671+
return _get_action('organization_show', {'id': org_id}) if org_id else []
672+
673+
674+
def get_datasets_for_user(userobj, package_name):
675+
package = _get_action('package_show', {'name_or_id': package_name})
676+
org_access = get_orgs_for_user(userobj, package['organization']['name'])
677+
678+
for group in package.get('groups'):
679+
group_access = get_groups_for_user(userobj, group['name'])
680+
681+
if group_access or org_access:
682+
return True
683+
684+
return False
685+
686+
649687
def get_groups_for_user(userobj, group):
650688
groups = _get_action('group_list_authz', {'id': userobj.id})
651689
group_names = [g['name'] for g in groups]
@@ -656,6 +694,47 @@ def get_groups_for_user(userobj, group):
656694
return False
657695

658696

697+
def get_is_admin_or_editor_of_any_group(userobj):
698+
groups = _get_action('group_list_authz', {'id': userobj.id})
699+
is_admin_or_editor = [get_user_permission_type(userobj, group['id']) for group in groups]
700+
701+
if len(groups) != 0 and any(t in ['admin', 'editor'] for t in is_admin_or_editor):
702+
return True
703+
else:
704+
return False
705+
706+
707+
def get_edit_permission_for_user(userobj, group):
708+
member_list = toolkit.get_action('member_list')({}, {'id': group})
709+
710+
if c.userobj.id in member_list:
711+
return True
712+
return False
713+
714+
715+
def get_user_permission_type(userobj, group):
716+
member_list = toolkit.get_action('member_list')({}, {'id': group})
717+
718+
if c.userobj.sysadmin:
719+
return 'admin'
720+
721+
for m in member_list:
722+
if userobj.id in m:
723+
if 'Admin' in m:
724+
return 'admin'
725+
if 'Member' in m:
726+
return 'member'
727+
if 'Editor' in m:
728+
return 'editor'
729+
730+
731+
def get_all_org_permissions(userobj):
732+
orgs = get_all_orgs_for_user(userobj)
733+
permissions = [
734+
get_user_permission_type(userobj, org.get('id')) for org in orgs]
735+
return any(p in ['admin', 'editor'] for p in permissions)
736+
737+
659738
def get_querytool_get_chart_colors(data):
660739
try:
661740
data = json.loads(data)

ckanext/querytool/logic/action/get.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ def querytool_get_chart_data(context, data_dict):
211211
sql_without_group = sql_string.split('GROUP BY')[0]
212212
sql_group = sql_string.split('GROUP BY')[1]
213213
categories_data = {}
214-
#log.error(data_dict)
215214

216215
if chart_filter:
217216
previous_filters.append(chart_filter)
@@ -243,7 +242,6 @@ def querytool_get_chart_data(context, data_dict):
243242
x.append(value)
244243

245244
for record in records:
246-
#log.error(record)
247245
if upper_bounds and lower_bounds and \
248246
record.get(upper_bounds.lower()) and \
249247
record.get(upper_bounds.lower()):

ckanext/querytool/logic/action/update.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def querytool_update(context, data_dict):
6565
dataset_name = data.get('dataset_name')
6666
dataset = _get_action('package_show')(context, {'id': dataset_name})
6767
dataset['groups'] = [{'name': str(data.get('group'))}]
68+
context['ignore_auth'] = True
6869
_get_action('package_patch')(context, dataset)
6970

7071
image_url = data_dict['image_url']

ckanext/querytool/plugin.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,23 @@ def get_helpers(self):
311311
'get_groups_for_user':
312312
helpers.get_groups_for_user,
313313
'querytool_get_chart_colors':
314-
helpers.get_querytool_get_chart_colors
314+
helpers.get_querytool_get_chart_colors,
315+
'get_edit_permission_for_user':
316+
helpers.get_edit_permission_for_user,
317+
'get_user_permission_type':
318+
helpers.get_user_permission_type,
319+
'get_is_admin_or_editor_of_any_group':
320+
helpers.get_is_admin_or_editor_of_any_group,
321+
'get_orgs_for_user':
322+
helpers.get_orgs_for_user,
323+
'get_organization':
324+
helpers.get_organization,
325+
'get_datasets_for_user':
326+
helpers.get_datasets_for_user,
327+
'get_all_orgs_for_user':
328+
helpers.get_all_orgs_for_user,
329+
'get_all_org_permissions':
330+
helpers.get_all_org_permissions
315331
}
316332

317333
# IAuthFunctions

ckanext/querytool/templates/group/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
{% set ctrl = 'ckanext.querytool.controllers.querytool:QueryToolController' %}
1717

18-
{% if h.check_access('querytool_update') %}
18+
{% if h.check_access('querytool_update') and h.get_is_admin_or_editor_of_any_group(c.userobj) %}
1919
{% link_for _('New Report'), controller=ctrl, action='querytool_edit', querytool='', class_='btn btn-primary', icon='plus-square' %}
2020
{% endif %}
2121
{% endblock %}

ckanext/querytool/templates/group/report_index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
{% set ctrl = 'ckanext.querytool.controllers.querytool:QueryToolController' %}
1717

18-
{% if h.check_access('querytool_update') %}
18+
{% if (h.get_is_admin_or_editor_of_any_group(c.userobj) or h.check_access('querytool_update')) and h.get_all_org_permissions(c.userobj) %}
1919
{% link_for _('New Report'), controller=ctrl, action='querytool_edit', querytool='', class_='btn btn-primary', icon='plus-square' %}
2020
{% endif %}
2121
{% endblock %}

ckanext/querytool/templates/group/reports.html

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% block primary_content_inner %}
44
{% set ctrl = 'ckanext.querytool.controllers.querytool:QueryToolController' %}
55

6-
{% if h.check_access('querytool_update') %}
6+
{% if h.check_access('querytool_update') and h.get_is_admin_or_editor_of_any_group(c.userobj) %}
77
{% link_for _('New Report'), controller=ctrl, action='querytool_edit', querytool='', class_='btn btn-primary', icon='plus-square' %}
88
{% endif %}
99

@@ -16,6 +16,9 @@
1616
{% set edit_data_url = h.url_for('querytool_edit', querytool='/' + querytool.name) %}
1717
{% set edit_visualizations_url = h.url_for('querytool_edit_visualizations', querytool= '/' + querytool.name) %}
1818
{% set delete_url = h.url_for('querytool_delete', querytool='/' + querytool.name) %}
19+
{% set member_type = h.get_user_permission_type(c.userobj, querytool.group) %}
20+
21+
{% if h.get_groups_for_user(c.userobj, querytool.group) and (member_type == 'member' or member_type == 'admin' or c.userobj.sysadmin) %}
1922
<div class="applicaitons-box">
2023

2124
<div class="thumb">
@@ -43,8 +46,13 @@ <h2>{{ querytool.title }}</h2>
4346

4447
<ul class="buttons-group">
4548
{% set user_permission = h.get_groups_for_user(c.userobj, querytool.group) %}
46-
{% if user_permission == True %}
47-
<li><a class="btn btn-minimal" role="button" href="{{ edit_data_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit filters and data') }}</a></li>
49+
{% set user_type = h.get_user_permission_type(c.userobj, querytool.group) %}
50+
{% set org_user_type = h.get_user_permission_type(c.userobj, querytool.owner_org) %}
51+
52+
{% if user_type in ['admin', 'editor'] or user_permission and user_type != 'member' or c.userobj.sysadmin %}
53+
{% if org_user_type in ['admin', 'editor'] %}
54+
<li><a class="btn btn-minimal" role="button" href="{{ edit_data_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit filters and data') }}</a></li>
55+
{% endif %}
4856
<li><a class="btn btn-minimal" role="button" href="{{ edit_visualizations_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit visualizations') }}</a></li>
4957
<li><a class="btn btn-minimal" role="button" href="{{ h.url_for('querytool_public_read', name=querytool.name) }}" type="submit" name="view"><span class="fa fa-eye" aria-hidden="true"></span> {{ _('View') }}</a></li>
5058
<li><a class="btn delete-querytool-btn" href="{{ delete_url }}" data-module="confirm-action" data-module-content="{{ _('Are you sure you want to delete this Report?') }}">{% block delete_button_text %}<span class="fa fa-trash-o" aria-hidden="true"></span> {{ _('Delete') }}{% endblock %}</a></li>
@@ -56,6 +64,7 @@ <h2>{{ querytool.title }}</h2>
5664
<p class="clearfix"></p>
5765

5866
</div>
67+
{% endif %}
5968
{% endif %}
6069
{% endfor %}
6170

@@ -83,10 +92,20 @@ <h2>{{ querytool.title }}</h2>
8392
<p>{{_('Type:')}} <span class="text-primary">{{ querytool.type|capitalize }}</span></p>
8493
</div>
8594
<ul class="inline query-actions">
86-
<li><a class="btn" href="{{ edit_data_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit filters and data') }}</a></li>
95+
{% set user_permission = h.get_groups_for_user(c.userobj, querytool.group) %}
96+
{% set user_type = h.get_user_permission_type(c.userobj, querytool.group) %}
97+
{% set org_user_type = h.get_user_permission_type(c.userobj, querytool.owner_org) %}
98+
99+
{% if user_type in ['admin', 'editor'] or user_permission and user_type != 'member' or c.userobj.sysadmin %}
100+
{% if org_user_type in ['admin', 'editor'] %}
101+
<li><a class="btn" href="{{ edit_data_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit filters and data') }}</a></li>
102+
{% endif %}
87103
<li><a class="btn" href="{{ edit_visualizations_url}}" type="submit" name="edit"><span class="fa fa-pencil" aria-hidden="true"></span> {{ _('Edit visualizations') }}</a></li>
88104
<li><a class="btn" href="{{ h.url_for('querytool_public_read', name=querytool.name) }}" type="submit" name="view"><span class="fa fa-eye" aria-hidden="true"></span> {{ _('View') }}</a></li>
89105
<li><a class="btn delete-querytool-btn" href="{{ delete_url }}" data-module="confirm-action" data-module-content="{{ _('Are you sure you want to delete this Report?') }}"><span class="fa fa-trash-o" aria-hidden="true"></span> {{ _('Delete') }}</a></li>
106+
{% else %}
107+
<li><a class="btn" href="{{ h.url_for('querytool_public_read', name=querytool.name) }}" type="submit" name="view"><span class="fa fa-eye" aria-hidden="true"></span> {{ _('View') }}</a></li>
108+
{% endif %}
90109
</ul>
91110
</div>
92111
{% endif %}

ckanext/querytool/templates/querytool/admin/base_groups.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% endblock %}
1515

1616
{% block page_primary_action %}
17-
{% if h.check_access('querytool_update') %}
17+
{% if h.check_access('querytool_update') and h.get_is_admin_or_editor_of_any_group(c.userobj) %}
1818
{% link_for _('New Report'), controller=ctrl, action='querytool_edit', querytool='', class_='btn btn-primary', icon='plus-square' %}
1919
{% endif %}
2020
{% endblock %}

ckanext/querytool/templates/querytool/admin/base_list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{% endblock %}
2121

2222
{% block page_primary_action %}
23-
{% if h.check_access('querytool_update') %}
23+
{% if h.get_is_admin_or_editor_of_any_group(c.userobj) or h.check_access('querytool_update') %}
2424
{% link_for _('New Report'), controller=ctrl, action='querytool_edit', querytool='', class_='btn btn-primary', icon='plus-square' %}
2525
{% endif %}
2626
{% endblock %}

0 commit comments

Comments
 (0)