Skip to content

Commit

Permalink
allow cases where user is not set in globals
Browse files Browse the repository at this point in the history
Such cases include CLI scripts and tests. This will most likely just
delay failure, but at least it means some permissions are checked for
cases where we don't have a user.
  • Loading branch information
Shahar Evron committed Nov 26, 2020
1 parent 6b745f3 commit c7d96ed
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ckanext/authz_service/authz_binding/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ def ckan_get_user_role_in_group(group_id):
# tyope: (str) -> Optional[str]
"""Get the current user's role in a group / organization
"""
user = get_user_context()['user']
user = get_user_context().get('user')
if not user:
return None
return users_role_for_group_or_org(group_id, user)


def ckan_is_sysadmin():
# type: () -> bool
"""Tell if the current user is a CKAN sysadmin
"""
user = get_user_context()['user']
user = get_user_context().get('user')
return is_sysadmin(user)


Expand All @@ -59,4 +61,9 @@ def get_user_context():
call this. This allows tests to patch this function to set the current
user.
"""
return dict(model=model, user=g.user, auth_user_obj=g.userobj)
context = dict(model=model)
if hasattr(g, 'user'):
context['user'] = g.user
if hasattr(g, 'userobj'):
context['auth_user_obj'] = g.userobj
return context

0 comments on commit c7d96ed

Please sign in to comment.