<s> from functools import wraps <EOL> from flask import request , g , current_app , _app_ctx_stack as stack <EOL> from werkzeug . local import LocalProxy <EOL> from werkzeug . exceptions import Unauthorized <EOL> from bouncer import Ability <EOL> from bouncer . constants import * <EOL> _bouncer = LocalProxy ( lambda : current_app . extensions [ '<STR_LIT>' ] ) <EOL> def ensure ( action , subject ) : <EOL> request . _authorized = True <EOL> current_user = _bouncer . get_current_user ( ) <EOL> ability = Ability ( current_user ) <EOL> ability . authorization_method = _bouncer . get_authorization_method ( ) <EOL> ability . aliased_actions = _bouncer . alias_actions <EOL> if ability . cannot ( action , subject ) : <EOL> msg = "<STR_LIT>" . format ( current_user , action , subject ) <EOL> raise Unauthorized ( msg ) <EOL> bounce = ensure <EOL> class Condition ( object ) : <EOL> def __init__ ( self , action , subject ) : <EOL> self . action = action <EOL> self . subject = subject <EOL> def test ( self ) : <EOL> ensure ( self . action , self . subject ) <EOL> def requires ( action , subject ) : <EOL> def decorator ( f ) : <EOL> f . _explict_rule_set = True <EOL> @ wraps ( f ) <EOL> def decorated_function ( * args , ** kwargs ) : <EOL> Condition ( action , subject ) . test ( ) <EOL> return f ( * args , ** kwargs ) <EOL> return decorated_function <EOL> return decorator <EOL> def skip_authorization ( f ) : <EOL> @ wraps ( f ) <EOL> def decorated_function ( * args , ** kwargs ) : <EOL> request . _authorized = True <EOL> return f ( * args , ** kwargs ) <EOL> return decorated_function <EOL> class Bouncer ( object ) : <EOL> """<STR_LIT>""" <EOL> special_methods = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:index>" ] <EOL> def __init__ ( self , app = None , ** kwargs ) : <EOL> self . authorization_method_callback = None <EOL> self . _alias_actions = self . default_alias_actions ( ) <EOL> self . _authorization_method = None <EOL> self . flask_classy_classes = list ( ) <EOL> self . explict_rules = list ( ) <EOL> self . get_current_user = self . default_user_loader <EOL> self . app = None <EOL> if app is not None : <EOL> self . init_app ( app , ** kwargs ) <EOL> def get_app ( self , reference_app = None ) : <EOL> """<STR_LIT>""" <EOL> if reference_app is not None : <EOL> return reference_app <EOL> if self . app is not None : <EOL> return self . app <EOL> ctx = stack . top <EOL> if ctx is not None : <EOL> return ctx . app <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def init_app ( self , app , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . app = app <EOL> self . _init_extension ( ) <EOL> self . app . before_request ( self . check_implicit_rules ) <EOL> if kwargs . get ( '<STR_LIT>' , False ) : <EOL> self . app . after_request ( self . check_authorization ) <EOL> def _init_extension ( self ) : <EOL> if not hasattr ( self . app , '<STR_LIT>' ) : <EOL> self . app . extensions = dict ( ) <EOL> self . app . extensions [ '<STR_LIT>' ] = self <EOL> def check_authorization ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> raise Unauthorized <EOL> elif not request . _authorized : <EOL> raise Unauthorized <EOL> return response <EOL> def check_implicit_rules ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . request_is_managed_by_flask_classy ( ) : <EOL> return <EOL> if self . method_is_explictly_overwritten ( ) : <EOL> return <EOL> class_name , action = request . endpoint . split ( '<STR_LIT::>' ) <EOL> clazz = [ classy_class for classy_class in self . flask_classy_classes if classy_class . __name__ == class_name ] [ <NUM_LIT:0> ] <EOL> Condition ( action , clazz . __target_model__ ) . test ( ) <EOL> def method_is_explictly_overwritten ( self ) : <EOL> view_func = current_app . view_functions [ request . endpoint ] <EOL> return hasattr ( view_func , '<STR_LIT>' ) and view_func . _explict_rule_set is True <EOL> def request_is_managed_by_flask_classy ( self ) : <EOL> if request . endpoint is None : <EOL> return False <EOL> if '<STR_LIT::>' not in request . endpoint : <EOL> return False <EOL> class_name , action = request . endpoint . split ( '<STR_LIT::>' ) <EOL> return any ( class_name == classy_class . __name__ for classy_class in self . flask_classy_classes ) and action in self . special_methods <EOL> def default_user_loader ( self ) : <EOL> if hasattr ( g , '<STR_LIT>' ) : <EOL> return g . current_user <EOL> elif hasattr ( g , '<STR_LIT:user>' ) : <EOL> return g . user <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def user_loader ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . get_current_user = value <EOL> return value <EOL> @ property <EOL> def alias_actions ( self ) : <EOL> return self . _alias_actions <EOL> @ alias_actions . setter <EOL> def alias_actions ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _alias_actions = value <EOL> def default_alias_actions ( self ) : <EOL> return { <EOL> READ : [ INDEX , SHOW , GET ] , <EOL> CREATE : [ NEW , PUT , POST ] , <EOL> UPDATE : [ EDIT , PATCH ] <EOL> } <EOL> def monitor ( self , * classy_routes ) : <EOL> self . flask_classy_classes . extend ( classy_routes ) <EOL> def authorization_method ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _authorization_method = value <EOL> return self . _authorization_method <EOL> def get_authorization_method ( self ) : <EOL> if self . _authorization_method is not None : <EOL> return self . _authorization_method <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) </s>
