<s> """<STR_LIT>""" <EOL> from flask import current_app <EOL> from flaskext . auth . auth import get_current_user_data , not_logged_in <EOL> def has_permission ( role , resource , action ) : <EOL> """<STR_LIT>""" <EOL> role = current_app . auth . load_role ( role ) <EOL> return role . has_permission ( resource , action ) if role else False <EOL> def permission_required ( resource , action , callback = None ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( func ) : <EOL> def decorator ( * args , ** kwargs ) : <EOL> user_data = get_current_user_data ( ) <EOL> if user_data is None : <EOL> return not_logged_in ( callback , * args , ** kwargs ) <EOL> if not has_permission ( user_data . get ( '<STR_LIT>' ) , resource , action ) : <EOL> if callback is None : <EOL> return current_app . auth . not_permitted_callback ( * args , ** kwargs ) <EOL> else : <EOL> return callback ( * args , ** kwargs ) <EOL> return callback ( * args , ** kwargs ) <EOL> return func ( * args , ** kwargs ) <EOL> return decorator <EOL> return wrap <EOL> class Permission ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , action ) : <EOL> self . resource = resource <EOL> self . action = action <EOL> def __eq__ ( self , other ) : <EOL> return self . resource == other . resource and self . action == other . action <EOL> class Role ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , permissions ) : <EOL> self . name = name <EOL> self . permissions = permissions <EOL> def has_permission ( self , resource , action ) : <EOL> return any ( [ resource == perm . resource and action == perm . action for perm in self . permissions ] ) </s>
