Skip to content

Instantly share code, notes, and snippets.

@jcelliott
Created August 20, 2014 03:41
Show Gist options
  • Save jcelliott/ec23ef0948dbc834e5bb to your computer and use it in GitHub Desktop.
Save jcelliott/ec23ef0948dbc834e5bb to your computer and use it in GitHub Desktop.
def authorized_for(user, op):
if user[1] == 'admin':
return True
else:
return False
def forbidden():
print('forbidden!')
def check_authorized(operation):
def decorator(func):
def wrapped(user_id, user_role):
if authorized_for((user_id, user_role), operation):
return func(user_id, user_role)
else:
return forbidden()
return wrapped
return decorator
@check_authorized('top_secret_operation')
def test_route(user_id, user_role):
print('authorized for test_route')
test_route('234234', 'admin')
test_route('123124', 'notadmin')
"""
output:
authorized for test_route
forbidden!
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment