Last active
November 12, 2016 05:42
-
-
Save bolerap/d10d9df3b4b08327c9cae4584710d3df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import functools | |
import inspect | |
# decorator | |
def check_role(f): | |
# Use this decorator to keep update some attributes of f such as doc string, name ... | |
@functools.wraps(f) | |
def wrapper(*args, **kwargs): | |
# make arguments as a dict, this avoid to check argument which is position or keyword argument | |
func_args = inspect.getcallargs(f, *args, **kwargs) | |
if func_args.get('user') != 'admin': | |
raise Exception('You have not allowed to operate with products') | |
return f(*args, **kwargs) | |
return wrapper | |
# class | |
class Store(object): | |
@check_role | |
def get_product(self, user, product): | |
return self.storage.get(product) | |
@check_role | |
def put_product(self, user, product): | |
return self.storage.put(product) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment