diff --git a/ckan/controllers/user.py b/ckan/controllers/user.py index ecc8e700163..ab162ef0659 100644 --- a/ckan/controllers/user.py +++ b/ckan/controllers/user.py @@ -603,30 +603,21 @@ def dashboard(self, id=None, offset=0): return render('user/dashboard.html') def dashboard_datasets(self): - context = {'model': model, 'session': model.Session, - 'user': c.user or c.author, 'for_view': True} + context = {'for_view': True} data_dict = {'user_obj': c.userobj} self._setup_template_variables(context, data_dict) return render('user/dashboard_datasets.html') def dashboard_organizations(self): - context = {'model': model, 'session': model.Session, - 'user': c.user or c.author, 'for_view': True} + context = {'for_view': True} data_dict = {'user_obj': c.userobj} self._setup_template_variables(context, data_dict) - # TODO: Add organizations that user is a member of here. I tried - # h.organizations_available() but that errors in the template - c.organizations = []#h.organizations_available() return render('user/dashboard_organizations.html') def dashboard_groups(self): - context = {'model': model, 'session': model.Session, - 'user': c.user or c.author, 'for_view': True} + context = {'for_view': True} data_dict = {'user_obj': c.userobj} self._setup_template_variables(context, data_dict) - # TODO: Add groups that user is a member of here. I tried - # h.groups_available() but that errors in the template - c.groups = []#h.groups_available() return render('user/dashboard_groups.html') def follow(self, id): diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 5c8a12d5613..1e06e73356d 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -1269,18 +1269,20 @@ def popular(type_, number, min=1, title=None): return snippet('snippets/popular.html', title=title, number=number, min=min) -def groups_available(): - ''' return a list of available groups ''' - context = {'model': model, 'session': model.Session, - 'user': c.user or c.author} - data_dict = {'available_only': True} +def groups_available(am_member=False): + ''' return a list of available groups + + :param am_member: only show groups user is a member of + :type am-am: bool + ''' + context = {} + data_dict = {'available_only': True, 'am_member': am_member} return logic.get_action('group_list_authz')(context, data_dict) def organizations_available(permission='edit_group'): ''' return a list of available organizations ''' - context = {'model': model, 'session': model.Session, - 'user': c.user} + context = {'user': c.user} data_dict = {'permission': permission} return logic.get_action('organization_list_for_user')(context, data_dict) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 178313ad3c4..29967d11a21 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -418,13 +418,18 @@ def group_list_authz(context, data_dict): (optional, default: ``False``) :type available_only: boolean + :param am_member: only show groups user is a member of else sys_admins get all + :type am-am: bool + (optional, default: ``False``) + :returns: the names of groups that the user is authorized to edit :rtype: list of strings ''' model = context['model'] user = context['user'] - available_only = data_dict.get('available_only',False) + available_only = data_dict.get('available_only', False) + am_member = data_dict.get('am_member', False) _check_access('group_list_authz',context, data_dict) @@ -436,7 +441,7 @@ def group_list_authz(context, data_dict): if not user_id: return [] - if not sysadmin: + if not sysadmin or am_member: q = model.Session.query(model.Member) \ .filter(model.Member.table_name == 'user') \ .filter(model.Member.capacity.in_(roles)) \ @@ -452,7 +457,7 @@ def group_list_authz(context, data_dict): .filter(model.Group.is_organization == False) \ .filter(model.Group.state == 'active') - if not sysadmin: + if not sysadmin or am_member: q = q.filter(model.Group.id.in_(group_ids)) groups = q.all() @@ -462,7 +467,7 @@ def group_list_authz(context, data_dict): if package: groups = set(groups) - set(package.get_groups()) - return [{'id':group.id,'name':group.name} for group in groups] + return [{'id': group.id, 'name': group.name, 'type': group.type} for group in groups] def organization_list_for_user(context, data_dict): '''Return the list of organizations that the user is a member of. @@ -485,35 +490,35 @@ def organization_list_for_user(context, data_dict): .filter(model.Group.is_organization == True) \ .filter(model.Group.state == 'active') - if sysadmin: - # Sysadmins can see all organizations - return [{'id':org.id,'name':org.name,'title':org.title} for org in orgs_q.all()] + if not sysadmin: + # for non-Sysadmins check they have the required permission - permission = data_dict.get('permission', 'edit_group') + permission = data_dict.get('permission', 'edit_group') - roles = ckan.new_authz.get_roles_with_permission(permission) + roles = ckan.new_authz.get_roles_with_permission(permission) - if not roles: - return [] - user_id = new_authz.get_user_id_for_username(user, allow_none=True) - if not user_id: - return [] + if not roles: + return [] + user_id = new_authz.get_user_id_for_username(user, allow_none=True) + if not user_id: + return [] - q = model.Session.query(model.Member) \ - .filter(model.Member.table_name == 'user') \ - .filter(model.Member.capacity.in_(roles)) \ - .filter(model.Member.table_id == user_id) + q = model.Session.query(model.Member) \ + .filter(model.Member.table_name == 'user') \ + .filter(model.Member.capacity.in_(roles)) \ + .filter(model.Member.table_id == user_id) - group_ids = [] - for row in q.all(): - group_ids.append(row.group_id) + group_ids = [] + for row in q.all(): + group_ids.append(row.group_id) - if not group_ids: - return [] + if not group_ids: + return [] - q = orgs_q.filter(model.Group.id.in_(group_ids)) + orgs_q = orgs_q.filter(model.Group.id.in_(group_ids)) - return [{'id':org.id,'name':org.name,'title':org.title} for org in q.all()] + return [{'id': org.id, 'name': org.name, 'title': org.title, 'type': org.type} + for org in orgs_q.all()] def group_revision_list(context, data_dict): '''Return a group's revisions. diff --git a/ckan/templates/group/snippets/group_item.html b/ckan/templates/group/snippets/group_item.html index 9e32ce86e6a..5766d4b2f29 100644 --- a/ckan/templates/group/snippets/group_item.html +++ b/ckan/templates/group/snippets/group_item.html @@ -11,7 +11,7 @@ {% endfor %} #} -{% set url = h.url_for(group.type ~ '_read', action='read', id=group.name) %} +{% set url = h.url_for(group.type + '_read', action='read', id=group.name) %}
  • {% block image %} {{ group.name }} diff --git a/ckan/templates/organization/snippets/organization_item.html b/ckan/templates/organization/snippets/organization_item.html index 818d5cc338a..cb74354d566 100644 --- a/ckan/templates/organization/snippets/organization_item.html +++ b/ckan/templates/organization/snippets/organization_item.html @@ -11,7 +11,8 @@ {% endfor %} #} -{% set url = h.url_for(organization.type ~ '_read', action='read', id=organization.name) %} + +{% set url = h.url_for(organization.type + '_read', action='read', id=organization.name) %}
  • {% block image %} {{ organization.name }} diff --git a/ckan/templates/user/dashboard_groups.html b/ckan/templates/user/dashboard_groups.html index 4604eafb083..80253c7d472 100644 --- a/ckan/templates/user/dashboard_groups.html +++ b/ckan/templates/user/dashboard_groups.html @@ -10,8 +10,9 @@ {% block primary_content_inner %}

    {{ _('My Groups') }}

    - {% if c.groups %} - {% snippet "group/snippets/group_list.html", groups=c.groups %} + {% set groups = h.groups_available(am_member=True) %} + {% if groups %} + {% snippet "group/snippets/group_list.html", groups=groups %} {% else %}

    {{ _('You are not a member of any groups.') }} diff --git a/ckan/templates/user/dashboard_organizations.html b/ckan/templates/user/dashboard_organizations.html index 9f96a392397..4b8bf3bab33 100644 --- a/ckan/templates/user/dashboard_organizations.html +++ b/ckan/templates/user/dashboard_organizations.html @@ -10,8 +10,9 @@ {% block primary_content_inner %}

    {{ _('My Organizations') }}

    - {% if c.organizations %} - {% snippet "organization/snippets/organization_list.html", organizations=c.organizations %} + {% set organizations = h.organizations_available() %} + {% if organizations %} + {% snippet "organization/snippets/organization_list.html", organizations=organizations %} {% else %}

    {{ _('You are not a member of any organizations.') }}