web/views/authentication.py
branchtls-sprint
changeset 1488 6da89a703c5a
parent 0 b97547f5f1fa
child 1490 6b024694d493
equal deleted inserted replaced
1487:7ea2d806de76 1488:6da89a703c5a
    49             else:
    49             else:
    50                 raise InvalidSession('bad connection id')
    50                 raise InvalidSession('bad connection id')
    51         # associate the connection to the current request
    51         # associate the connection to the current request
    52         req.set_connection(cnx, user)
    52         req.set_connection(cnx, user)
    53         return cnx
    53         return cnx
    54         
    54 
       
    55     def login_from_email(self, login):
       
    56         session = self.repo.internal_session()
       
    57         rset = session.execute('Any L WHERE U login L, U primary_email M, '
       
    58                                'M address %(login)s', {'login': login})
       
    59         if rset.rowcount == 1:
       
    60             login = rset[0][0]
       
    61         return login
       
    62 
    55     def authenticate(self, req, _login=None, _password=None):
    63     def authenticate(self, req, _login=None, _password=None):
    56         """authenticate user and return corresponding user object
    64         """authenticate user and return corresponding user object
    57         
    65 
    58         :raise ExplicitLogin: if authentication is required (no authentication
    66         :raise ExplicitLogin: if authentication is required (no authentication
    59         info found or wrong user/password)
    67         info found or wrong user/password)
    60 
    68 
    61         Note: this method is violating AuthenticationManager interface by
    69         Note: this method is violating AuthenticationManager interface by
    62         returning a session instance instead of the user. This is expected by
    70         returning a session instance instead of the user. This is expected by
    64         """
    72         """
    65         if _login is not None:
    73         if _login is not None:
    66             login, password = _login, _password
    74             login, password = _login, _password
    67         else:
    75         else:
    68             login, password = req.get_authorization()
    76             login, password = req.get_authorization()
       
    77         if '@' in (login or u''):
       
    78             login = self.login_from_email(login)
    69         if not login:
    79         if not login:
    70             # No session and no login -> try anonymous
    80             # No session and no login -> try anonymous
    71             login, password = self.vreg.config.anonymous_user()
    81             login, password = self.vreg.config.anonymous_user()
    72             if not login: # anonymous not authorized
    82             if not login: # anonymous not authorized
    73                 raise ExplicitLogin()
    83                 raise ExplicitLogin()