<s> from glance . common import cfg <EOL> from glance . common import exception <EOL> from glance . common import utils <EOL> from glance . common import wsgi <EOL> from glance . registry . db import api as db_api <EOL> class RequestContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , auth_tok = None , user = None , tenant = None , roles = None , <EOL> is_admin = False , read_only = False , show_deleted = False , <EOL> owner_is_tenant = True ) : <EOL> self . auth_tok = auth_tok <EOL> self . user = user <EOL> self . tenant = tenant <EOL> self . roles = roles or [ ] <EOL> self . is_admin = is_admin <EOL> self . read_only = read_only <EOL> self . _show_deleted = show_deleted <EOL> self . owner_is_tenant = owner_is_tenant <EOL> @ property <EOL> def owner ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . tenant if self . owner_is_tenant else self . user <EOL> @ property <EOL> def show_deleted ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _show_deleted or self . is_admin : <EOL> return True <EOL> return False <EOL> class ContextMiddleware ( wsgi . Middleware ) : <EOL> opts = [ <EOL> cfg . BoolOpt ( '<STR_LIT>' , default = True ) , <EOL> ] <EOL> def __init__ ( self , app , conf , ** local_conf ) : <EOL> self . conf = conf <EOL> self . conf . register_opts ( self . opts ) <EOL> self . ctxcls = RequestContext <EOL> if '<STR_LIT>' in local_conf : <EOL> self . ctxcls = utils . import_class ( local_conf [ '<STR_LIT>' ] ) <EOL> super ( ContextMiddleware , self ) . __init__ ( app ) <EOL> def make_context ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( '<STR_LIT>' , self . conf . owner_is_tenant ) <EOL> return self . ctxcls ( * args , ** kwargs ) <EOL> def process_request ( self , req ) : <EOL> """<STR_LIT>""" <EOL> auth_tok = req . headers . get ( '<STR_LIT>' , <EOL> req . headers . get ( '<STR_LIT>' ) ) <EOL> if auth_tok : <EOL> if req . headers . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> user = req . headers . get ( '<STR_LIT>' ) <EOL> tenant = req . headers . get ( '<STR_LIT>' ) <EOL> roles = [ r . strip ( ) <EOL> for r in req . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> is_admin = '<STR_LIT>' in roles <EOL> else : <EOL> raise exception . NotAuthorized ( ) <EOL> else : <EOL> user = None <EOL> tenant = None <EOL> roles = [ ] <EOL> is_admin = True <EOL> req . context = self . make_context ( <EOL> auth_tok = auth_tok , user = user , tenant = tenant , roles = roles , <EOL> is_admin = is_admin ) </s>
