Created
August 20, 2014 03:41
-
-
Save jcelliott/ec23ef0948dbc834e5bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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