<s> from pyramid . httpexceptions import ( <EOL> HTTPForbidden , <EOL> HTTPFound <EOL> ) <EOL> from pyramid . renderers import get_renderer <EOL> from pyramid . session import check_csrf_token <EOL> from pyramid . security import ( <EOL> remember , <EOL> forget , <EOL> Authenticated , <EOL> NO_PERMISSION_REQUIRED , <EOL> ) <EOL> from ... util import get_oid <EOL> from . . import mgmt_view <EOL> from substanced . interfaces import IUserLocator <EOL> from substanced . principal import DefaultUserLocator <EOL> from substanced . event import LoggedIn <EOL> @ mgmt_view ( <EOL> name = '<STR_LIT>' , <EOL> renderer = '<STR_LIT>' , <EOL> tab_condition = False , <EOL> permission = NO_PERMISSION_REQUIRED <EOL> ) <EOL> @ mgmt_view ( <EOL> renderer = '<STR_LIT>' , <EOL> context = HTTPForbidden , <EOL> permission = NO_PERMISSION_REQUIRED , <EOL> tab_condition = False <EOL> ) <EOL> @ mgmt_view ( <EOL> renderer = '<STR_LIT>' , <EOL> context = HTTPForbidden , <EOL> permission = NO_PERMISSION_REQUIRED , <EOL> effective_principals = Authenticated , <EOL> tab_condition = False <EOL> ) <EOL> def login ( context , request ) : <EOL> login_url = request . sdiapi . mgmt_path ( request . context , '<STR_LIT>' ) <EOL> referrer = request . url <EOL> if '<STR_LIT>' in referrer : <EOL> return HTTPForbidden ( ) <EOL> if login_url in referrer : <EOL> referrer = request . sdiapi . mgmt_path ( request . virtual_root ) <EOL> came_from = request . session . setdefault ( '<STR_LIT>' , referrer ) <EOL> login = '<STR_LIT>' <EOL> password = '<STR_LIT>' <EOL> if '<STR_LIT>' in request . params : <EOL> try : <EOL> check_csrf_token ( request ) <EOL> except : <EOL> request . sdiapi . flash ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> login = request . params [ '<STR_LIT>' ] <EOL> password = request . params [ '<STR_LIT:password>' ] <EOL> adapter = request . registry . queryMultiAdapter ( <EOL> ( context , request ) , <EOL> IUserLocator <EOL> ) <EOL> if adapter is None : <EOL> adapter = DefaultUserLocator ( context , request ) <EOL> user = adapter . get_user_by_login ( login ) <EOL> if user is not None and user . check_password ( password ) : <EOL> request . session . pop ( '<STR_LIT>' , None ) <EOL> headers = remember ( request , get_oid ( user ) ) <EOL> request . registry . notify ( LoggedIn ( login , user , context , request ) ) <EOL> return HTTPFound ( location = came_from , headers = headers ) <EOL> request . sdiapi . flash ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> template = get_renderer ( '<STR_LIT>' <EOL> ) . implementation ( ) <EOL> return dict ( <EOL> url = request . sdiapi . mgmt_path ( request . virtual_root , '<STR_LIT>' ) , <EOL> came_from = came_from , <EOL> login = login , <EOL> password = password , <EOL> login_template = template , <EOL> ) <EOL> @ mgmt_view ( <EOL> name = '<STR_LIT>' , <EOL> tab_condition = False , <EOL> permission = NO_PERMISSION_REQUIRED <EOL> ) <EOL> def logout ( request ) : <EOL> headers = forget ( request ) <EOL> return HTTPFound ( location = request . sdiapi . mgmt_path ( request . context ) , <EOL> headers = headers ) </s>
