web/views/sessions.py
author Julien Cristau <julien.cristau@logilab.fr>
Mon, 18 May 2015 16:36:26 +0200
changeset 10438 f27a489994e8
parent 10369 0c678b63d017
child 10557 9826dc78eec1
permissions -rw-r--r--
[devtools] make test db name for postgresql process-specific In case we run two tests concurrently on the same cluster, better not use the same db name for both.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10358
d551d0a162d5 [web/sessions] reduce repoapi usage where possible
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
10369
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    18
"""web session: by default the session is actually the db connection """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
9573
99166335a8e0 Move setting session.mtime from dbapi to web session manager
Julien Cristau <julien.cristau@logilab.fr>
parents: 9543
diff changeset
    21
from time import time
99166335a8e0 Move setting session.mtime from dbapi to web session manager
Julien Cristau <julien.cristau@logilab.fr>
parents: 9543
diff changeset
    22
10369
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    23
from cubicweb import RepositoryError, Unauthorized, BadConnectionId
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    24
from cubicweb.web import InvalidSession, component
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    25
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    26
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    27
class AbstractSessionManager(component.Component):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    28
    """manage session data associated to a session identifier"""
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    29
    __abstract__ = True
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    30
    __regid__ = 'sessionmanager'
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    31
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    32
    def __init__(self, repo):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    33
        vreg = repo.vreg
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    34
        self.session_time = vreg.config['http-session-time'] or None
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    35
        self.authmanager = vreg['components'].select('authmanager', repo=repo)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    36
        interval = (self.session_time or 0) / 2.
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    37
        if vreg.config.anonymous_user()[0] is not None:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    38
            self.cleanup_anon_session_time = vreg.config['cleanup-anonymous-session-time'] or 5 * 60
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    39
            assert self.cleanup_anon_session_time > 0
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    40
            if self.session_time is not None:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    41
                self.cleanup_anon_session_time = min(self.session_time,
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    42
                                                     self.cleanup_anon_session_time)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    43
            interval = self.cleanup_anon_session_time / 2.
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    44
        # we don't want to check session more than once every 5 minutes
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    45
        self.clean_sessions_interval = max(5 * 60, interval)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    46
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    47
    def clean_sessions(self):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    48
        """cleanup sessions which has not been unused since a given amount of
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    49
        time. Return the number of sessions which have been closed.
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    50
        """
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    51
        self.debug('cleaning http sessions')
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    52
        session_time = self.session_time
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    53
        closed, total = 0, 0
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    54
        for session in self.current_sessions():
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    55
            total += 1
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    56
            try:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    57
                last_usage_time = session.cnx.check()
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    58
            except AttributeError:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    59
                last_usage_time = session.mtime
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    60
            except BadConnectionId:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    61
                self.close_session(session)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    62
                closed += 1
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    63
            else:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    64
                no_use_time = (time() - last_usage_time)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    65
                if session.anonymous_session:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    66
                    if no_use_time >= self.cleanup_anon_session_time:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    67
                        self.close_session(session)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    68
                        closed += 1
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    69
                elif session_time is not None and no_use_time >= session_time:
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    70
                    self.close_session(session)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    71
                    closed += 1
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    72
        return closed, total - closed
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    73
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    74
    def current_sessions(self):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    75
        """return currently open sessions"""
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    76
        raise NotImplementedError()
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    77
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    78
    def get_session(self, req, sessionid):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    79
        """return existing session for the given session identifier"""
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    80
        raise NotImplementedError()
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    81
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    82
    def open_session(self, req):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    83
        """open and return a new session for the given request.
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    84
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    85
        raise :exc:`cubicweb.AuthenticationError` if authentication failed
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    86
        (no authentication info found or wrong user/password)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    87
        """
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    88
        raise NotImplementedError()
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    89
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    90
    def close_session(self, session):
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    91
        """close session on logout or on invalid session detected (expired out,
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    92
        corrupted...)
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    93
        """
0c678b63d017 [web] move abstract session manager to web/sessions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10358
diff changeset
    94
        raise NotImplementedError()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
class InMemoryRepositorySessionManager(AbstractSessionManager):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    """manage session data associated to a session identifier"""
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
    99
2887
1282dc6525c5 give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2706
diff changeset
   100
    def __init__(self, *args, **kwargs):
1282dc6525c5 give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2706
diff changeset
   101
        AbstractSessionManager.__init__(self, *args, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        # XXX require a RepositoryAuthenticationManager which violates
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        #     authenticate interface by returning a session instead of a user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        #assert isinstance(self.authmanager, RepositoryAuthenticationManager)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        self._sessions = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
5080
cfc7c2b24f9e [cleanup] some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   107
    # dump_data / restore_data to avoid loosing open sessions on registry
cfc7c2b24f9e [cleanup] some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   108
    # reloading
2706
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   109
    def dump_data(self):
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   110
        return self._sessions
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   111
    def restore_data(self, data):
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   112
        self._sessions = data
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   113
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    def current_sessions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        return self._sessions.values()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   116
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    def get_session(self, req, sessionid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        """return existing session for the given session identifier"""
7428
5338d895b891 [web session] fix session handling so we get a chance to have for instance the 'forgotpwd' feature working on a site where anonymous are not allowed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   119
        if sessionid not in self._sessions:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
            raise InvalidSession()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        session = self._sessions[sessionid]
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   122
        try:
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   123
            user = self.authmanager.validate_session(req, session)
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   124
        except InvalidSession:
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   125
            self.close_session(session)
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   126
            raise
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   127
        if session.closed:
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   128
            self.close_session(session)
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   129
            raise InvalidSession()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        return session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9015
diff changeset
   132
    def open_session(self, req):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   133
        """open and return a new session for the given request. The session is
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   134
        also bound to the request.
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   135
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   136
        raise :exc:`cubicweb.AuthenticationError` if authentication failed
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   137
        (no authentication info found or wrong user/password)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        """
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   139
        session, login = self.authmanager.authenticate(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        self._sessions[session.sessionid] = session
9573
99166335a8e0 Move setting session.mtime from dbapi to web session manager
Julien Cristau <julien.cristau@logilab.fr>
parents: 9543
diff changeset
   141
        session.mtime = time()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        return session
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   143
9018
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   144
    def postlogin(self, req, session):
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   145
        """postlogin: the user have been related to a session
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   146
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   147
        Both req and session are passed to this function because actually
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   148
        linking the request to the session is not yet done and not the
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   149
        responsability of this object.
6791
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   150
        """
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   151
        # Update last connection date
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   152
        # XXX: this should be in a post login hook in the repository, but there
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   153
        #      we can't differentiate actual login of automatic session
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   154
        #      reopening. Is it actually a problem?
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   155
        if 'last_login_time' in req.vreg.schema:
9018
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   156
            self._update_last_login_time(session)
9543
39f981482e34 merge 3.18.x in 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9071 9492
diff changeset
   157
        req.set_message(req._('welcome %s!') % session.user.login)
6791
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   158
9018
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   159
    def _update_last_login_time(self, session):
6791
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   160
        # XXX should properly detect missing permission / non writeable source
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   161
        # and avoid "except (RepositoryError, Unauthorized)" below
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   162
        try:
10358
d551d0a162d5 [web/sessions] reduce repoapi usage where possible
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
   163
            with session.new_cnx() as cnx:
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   164
                cnx.execute('SET X last_login_time NOW WHERE X eid %(x)s',
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   165
                           {'x' : session.user.eid})
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   166
                cnx.commit()
6791
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   167
        except (RepositoryError, Unauthorized):
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   168
            pass
6791
fe58b234f9c2 [web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6279
diff changeset
   169
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    def close_session(self, session):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        """close session on logout or on invalid session detected (expired out,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        corrupted...)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        """
5799
93f4b4d2fecf use sessionid in log message
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   174
        self.info('closing http session %s' % session.sessionid)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   175
        self._sessions.pop(session.sessionid, None)
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9019
diff changeset
   176
        if not session.closed:
9624
a42fae4cd45a [web/sessions] use session.sessionid instead of deprecated session.id
Julien Cristau <julien.cristau@logilab.fr>
parents: 9573
diff changeset
   177
            session.repo.close(session.sessionid)