<s> from functools import wraps <EOL> from inspect import getmembers , isfunction <EOL> from webob import exc <EOL> import six <EOL> from . decorators import expose <EOL> from . util import _cfg , iscontroller <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if six . PY3 : <EOL> from . compat import is_bound_method as ismethod <EOL> else : <EOL> from inspect import ismethod <EOL> class _SecureState ( object ) : <EOL> def __init__ ( self , desc , boolean_value ) : <EOL> self . description = desc <EOL> self . boolean_value = boolean_value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . description <EOL> def __nonzero__ ( self ) : <EOL> return self . boolean_value <EOL> def __bool__ ( self ) : <EOL> return self . __nonzero__ ( ) <EOL> Any = _SecureState ( '<STR_LIT>' , False ) <EOL> Protected = _SecureState ( '<STR_LIT>' , True ) <EOL> def _unlocked_method ( func ) : <EOL> _cfg ( func ) [ '<STR_LIT>' ] = Any <EOL> return func <EOL> def _secure_method ( check_permissions_func ) : <EOL> def wrap ( func ) : <EOL> cfg = _cfg ( func ) <EOL> cfg [ '<STR_LIT>' ] = Protected <EOL> cfg [ '<STR_LIT>' ] = check_permissions_func <EOL> return func <EOL> return wrap <EOL> class _UnlockedAttribute ( object ) : <EOL> def __init__ ( self , obj ) : <EOL> self . obj = obj <EOL> @ _unlocked_method <EOL> @ expose ( ) <EOL> def _lookup ( self , * remainder ) : <EOL> return self . obj , remainder <EOL> class _SecuredAttribute ( object ) : <EOL> def __init__ ( self , obj , check_permissions ) : <EOL> self . obj = obj <EOL> self . check_permissions = check_permissions <EOL> self . _parent = None <EOL> def _check_permissions ( self ) : <EOL> if isinstance ( self . check_permissions , six . string_types ) : <EOL> return getattr ( self . parent , self . check_permissions ) ( ) <EOL> else : <EOL> return self . check_permissions ( ) <EOL> def __get_parent ( self ) : <EOL> return self . _parent <EOL> def __set_parent ( self , parent ) : <EOL> if ismethod ( parent ) : <EOL> self . _parent = six . get_method_self ( parent ) <EOL> else : <EOL> self . _parent = parent <EOL> parent = property ( __get_parent , __set_parent ) <EOL> @ _secure_method ( '<STR_LIT>' ) <EOL> @ expose ( ) <EOL> def _lookup ( self , * remainder ) : <EOL> return self . obj , remainder <EOL> def _allowed_check_permissions_types ( x ) : <EOL> return ( <EOL> ismethod ( x ) or <EOL> isfunction ( x ) or <EOL> isinstance ( x , six . string_types ) <EOL> ) <EOL> def unlocked ( func_or_obj ) : <EOL> """<STR_LIT>""" <EOL> if ismethod ( func_or_obj ) or isfunction ( func_or_obj ) : <EOL> return _unlocked_method ( func_or_obj ) <EOL> else : <EOL> return _UnlockedAttribute ( func_or_obj ) <EOL> def secure ( func_or_obj , check_permissions_for_obj = None ) : <EOL> """<STR_LIT>""" <EOL> if _allowed_check_permissions_types ( func_or_obj ) : <EOL> return _secure_method ( func_or_obj ) <EOL> else : <EOL> if not _allowed_check_permissions_types ( check_permissions_for_obj ) : <EOL> msg = "<STR_LIT>" + "<STR_LIT>" <EOL> raise TypeError ( msg ) <EOL> return _SecuredAttribute ( func_or_obj , check_permissions_for_obj ) <EOL> class SecureControllerMeta ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dict_ ) : <EOL> cls . _pecan = dict ( <EOL> secured = Protected , <EOL> check_permissions = cls . check_permissions , <EOL> unlocked = [ ] <EOL> ) <EOL> for name , value in getmembers ( cls ) [ : ] : <EOL> if ( isfunction if six . PY3 else ismethod ) ( value ) : <EOL> if iscontroller ( value ) and value . _pecan . get ( <EOL> '<STR_LIT>' <EOL> ) is None : <EOL> wrapped = _make_wrapper ( value ) <EOL> wrapped . _pecan [ '<STR_LIT>' ] = Protected <EOL> wrapped . _pecan [ '<STR_LIT>' ] = cls . check_permissions <EOL> setattr ( cls , name , wrapped ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> if name . startswith ( '<STR_LIT>' ) and name . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> if isinstance ( value , _UnlockedAttribute ) : <EOL> cls . _pecan [ '<STR_LIT>' ] . append ( value . obj ) <EOL> setattr ( cls , name , value . obj ) <EOL> elif isinstance ( value , _SecuredAttribute ) : <EOL> cls . _pecan [ '<STR_LIT>' ] . append ( value ) <EOL> class SecureControllerBase ( object ) : <EOL> @ classmethod <EOL> def check_permissions ( cls ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> SecureController = SecureControllerMeta ( <EOL> '<STR_LIT>' , <EOL> ( SecureControllerBase , ) , <EOL> { '<STR_LIT>' : SecureControllerMeta . __doc__ } <EOL> ) <EOL> def _make_wrapper ( f ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( f ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return f ( * args , ** kwargs ) <EOL> wrapper . _pecan = f . _pecan . copy ( ) <EOL> return wrapper <EOL> def handle_security ( controller , im_self = None ) : <EOL> """<STR_LIT>""" <EOL> if controller . _pecan . get ( '<STR_LIT>' , False ) : <EOL> check_permissions = controller . _pecan [ '<STR_LIT>' ] <EOL> if isinstance ( check_permissions , six . string_types ) : <EOL> check_permissions = getattr ( <EOL> im_self or six . get_method_self ( controller ) , <EOL> check_permissions <EOL> ) <EOL> if not check_permissions ( ) : <EOL> raise exc . HTTPUnauthorized <EOL> def cross_boundary ( prev_obj , obj ) : <EOL> """<STR_LIT>""" <EOL> if prev_obj is None : <EOL> return <EOL> if isinstance ( obj , _SecuredAttribute ) : <EOL> obj . parent = prev_obj <EOL> if hasattr ( prev_obj , '<STR_LIT>' ) : <EOL> if obj not in prev_obj . _pecan . get ( '<STR_LIT>' , [ ] ) : <EOL> handle_security ( prev_obj ) </s>
