<s> from __future__ import absolute_import <EOL> import functools <EOL> import logging <EOL> from pprint import pformat <EOL> from urllib import urlencode <EOL> import flask <EOL> from flask import request , current_app <EOL> from flask . ext . login import current_user <EOL> import werkzeug as wz <EOL> from flask_acl . core import iter_object_acl , get_object_context , check <EOL> from flask_acl . permission import default_permission_sets <EOL> from flask_acl . predicate import default_predicates <EOL> log = logging . getLogger ( __name__ ) <EOL> class _Redirect ( Exception ) : <EOL> pass <EOL> class ACLManager ( object ) : <EOL> """<STR_LIT>""" <EOL> login_view = '<STR_LIT>' <EOL> def __init__ ( self , app = None ) : <EOL> self . _context_processors = [ ] <EOL> self . permission_sets = default_permission_sets . copy ( ) <EOL> self . predicates = default_predicates . copy ( ) <EOL> if app : <EOL> self . init_app ( app ) <EOL> def init_app ( self , app ) : <EOL> app . acl_manager = self <EOL> app . extensions [ '<STR_LIT>' ] = self <EOL> app . config . setdefault ( '<STR_LIT>' , True ) <EOL> app . errorhandler ( _Redirect ) ( lambda r : flask . redirect ( r . args [ <NUM_LIT:0> ] ) ) <EOL> def predicate ( self , name , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> if predicate is None : <EOL> return functools . partial ( self . predicate , name ) <EOL> self . predicates [ name ] = predicate <EOL> return predicate <EOL> def permission_set ( self , name , permission_set = None ) : <EOL> """<STR_LIT>""" <EOL> if permission_set is None : <EOL> return functools . partial ( self . permission_set , name ) <EOL> self . permission_sets [ name ] = permission_set <EOL> return permission_set <EOL> def context_processor ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . _context_processors . append ( func ) <EOL> def route_acl ( self , * acl , ** options ) : <EOL> """<STR_LIT>""" <EOL> def _route_acl ( func ) : <EOL> func . __acl__ = acl <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> permission = '<STR_LIT>' + request . method . lower ( ) <EOL> local_opts = options . copy ( ) <EOL> local_opts . setdefault ( '<STR_LIT:default>' , current_app . config [ '<STR_LIT>' ] ) <EOL> self . assert_can ( permission , func , ** local_opts ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapped <EOL> return _route_acl <EOL> def can ( self , permission , obj , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = { '<STR_LIT:user>' : current_user } <EOL> for func in self . _context_processors : <EOL> context . update ( func ( ) ) <EOL> context . update ( get_object_context ( obj ) ) <EOL> context . update ( kwargs ) <EOL> return check ( permission , iter_object_acl ( obj ) , ** context ) <EOL> def assert_can ( self , permission , obj , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> flash_message = kwargs . pop ( '<STR_LIT>' , None ) <EOL> stealth = kwargs . pop ( '<STR_LIT>' , False ) <EOL> default = kwargs . pop ( '<STR_LIT:default>' , None ) <EOL> res = self . can ( permission , obj , ** kwargs ) <EOL> res = default if res is None else res <EOL> if not res : <EOL> if flash_message and not stealth : <EOL> flask . flash ( flash_message , '<STR_LIT>' ) <EOL> if current_user . is_authenticated ( ) : <EOL> if flash_message is not False : <EOL> flask . flash ( flash_message or '<STR_LIT>' % permission ) <EOL> flask . abort ( <NUM_LIT> ) <EOL> elif not stealth and self . login_view : <EOL> if flash_message is not False : <EOL> flask . flash ( flash_message or '<STR_LIT>' ) <EOL> raise _Redirect ( flask . url_for ( self . login_view ) + '<STR_LIT:?>' + urlencode ( dict ( next = <EOL> flask . request . script_root + flask . request . path <EOL> ) ) ) <EOL> else : <EOL> flask . abort ( <NUM_LIT> ) <EOL> def can_route ( self , endpoint , method = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> view = flask . current_app . view_functions . get ( endpoint ) <EOL> if not view : <EOL> endpoint , args = flask . _request_ctx . top . match ( endpoint ) <EOL> view = flask . current_app . view_functions . get ( endpoint ) <EOL> if not view : <EOL> return False <EOL> return self . can ( '<STR_LIT>' + ( method or '<STR_LIT:GET>' ) . lower ( ) , view , ** kwargs ) </s>
