Skip to content

Commit

Permalink
[#2939] Move is_sysadmin as a standalone function
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Oct 8, 2012
1 parent 981043c commit 81adad8
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions ckan/new_authz.py
Expand Up @@ -14,26 +14,33 @@
class AuthFunctions:
_functions = {}

def is_authorized(action, context, data_dict=None):
if context.get('ignore_auth'):
return {'success': True}

# sysadmins can do anything
user = context.get('user')
def is_sysadmin(username):
''' returns True is username is a sysadmin '''
if not username:
return False
# see if we can authorise without touching the database
admin_tested = False
try:
if user and c.userobj and c.userobj.name == user:
if c.userobj and c.userobj.name == username:
if c.userobj.sysadmin:
return {'success': True}
admin_tested = True
return True
return False
except TypeError:
# c is not available
pass
if user and not admin_tested:
u = model.User.get(user)
if u and u.sysadmin:
return {'success': True}
# get user from the database
u = model.User.get(username)
if u and u.sysadmin:
return True
return False


def is_authorized(action, context, data_dict=None):
if context.get('ignore_auth'):
return {'success': True}

# sysadmins can do anything
if is_sysadmin(context.get('user')):
return {'success': True}

auth_function = _get_auth_function(action)
if auth_function:
Expand Down

0 comments on commit 81adad8

Please sign in to comment.