pyramid_cubicweb/login.py
changeset 11537 caf268942436
parent 11527 aa05b41a5816
child 11562 a49f08423f02
equal deleted inserted replaced
11536:6618408c0629 11537:caf268942436
       
     1 """ Provide login views that reproduce a classical CubicWeb behavior"""
     1 from pyramid import security
     2 from pyramid import security
     2 from pyramid.httpexceptions import HTTPSeeOther
     3 from pyramid.httpexceptions import HTTPSeeOther
     3 from pyramid.view import view_config
     4 from pyramid.view import view_config
     4 
     5 
     5 import cubicweb
     6 import cubicweb
     7 from pyramid_cubicweb.core import render_view
     8 from pyramid_cubicweb.core import render_view
     8 
     9 
     9 
    10 
    10 @view_config(route_name='login')
    11 @view_config(route_name='login')
    11 def login_form(request):
    12 def login_form(request):
       
    13     """ Default view for the 'login' route.
       
    14 
       
    15     Display the 'login' CubicWeb view, which is should be a login form"""
    12     request.response.text = render_view(request, 'login')
    16     request.response.text = render_view(request, 'login')
    13     return request.response
    17     return request.response
    14 
    18 
    15 
    19 
    16 @view_config(route_name='login', request_param=('__login', '__password'))
    20 @view_config(route_name='login', request_param=('__login', '__password'))
    17 def login_password_login(request):
    21 def login_password_login(request):
       
    22     """ Handle GET/POST of __login/__password on the 'login' route.
       
    23 
       
    24     The authentication itself is delegated to the CubicWeb repository.
       
    25 
       
    26     Request parameters:
       
    27 
       
    28     :param __login: The user login (or email if :confval:`allow-email-login` is
       
    29                     on.
       
    30     :param __password: The user password
       
    31     :param __setauthcookie: (optional) If defined and equal to '1', set the
       
    32                             authentication cookie maxage to 1 week.
       
    33 
       
    34                             If not, the authentication cookie is a session
       
    35                             cookie.
       
    36     """
    18     repo = request.registry['cubicweb.repository']
    37     repo = request.registry['cubicweb.repository']
    19 
    38 
    20     user_eid = None
    39     user_eid = None
    21 
    40 
    22     login = request.params['__login']
    41     login = request.params['__login']
    46     raise HTTPSeeOther(new_path, headers=headers)
    65     raise HTTPSeeOther(new_path, headers=headers)
    47 
    66 
    48 
    67 
    49 @view_config(route_name='login', effective_principals=security.Authenticated)
    68 @view_config(route_name='login', effective_principals=security.Authenticated)
    50 def login_already_loggedin(request):
    69 def login_already_loggedin(request):
       
    70     """ 'login' route view for Authenticated users.
       
    71 
       
    72     Simply redirect the user to '/'."""
    51     raise HTTPSeeOther('/')
    73     raise HTTPSeeOther('/')
    52 
    74 
    53 
    75 
    54 def includeme(config):
    76 def includeme(config):
       
    77     """ Create the 'login' route ('/login') and load this module views"""
    55     config.add_route('login', '/login')
    78     config.add_route('login', '/login')
    56     config.scan('pyramid_cubicweb.login')
    79     config.scan('pyramid_cubicweb.login')