author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 19 Feb 2010 09:34:14 +0100 | |
branch | stable |
changeset 4643 | 921737d2e3a8 |
parent 4252 | 6c4f109c2b03 |
child 5080 | cfc7c2b24f9e |
permissions | -rw-r--r-- |
0 | 1 |
"""web session component: by dfault the session is actually the db connection |
2 |
object :/ |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2706
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
1133 | 11 |
from cubicweb.web import InvalidSession |
0 | 12 |
from cubicweb.web.application import AbstractSessionManager |
13 |
||
14 |
||
15 |
class InMemoryRepositorySessionManager(AbstractSessionManager): |
|
16 |
"""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
|
17 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2706
diff
changeset
|
18 |
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
|
19 |
AbstractSessionManager.__init__(self, *args, **kwargs) |
0 | 20 |
# XXX require a RepositoryAuthenticationManager which violates |
21 |
# authenticate interface by returning a session instead of a user |
|
22 |
#assert isinstance(self.authmanager, RepositoryAuthenticationManager) |
|
23 |
self._sessions = {} |
|
24 |
||
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
25 |
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
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
|
0 | 30 |
def current_sessions(self): |
31 |
return self._sessions.values() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
32 |
|
0 | 33 |
def get_session(self, req, sessionid): |
34 |
"""return existing session for the given session identifier""" |
|
35 |
if not sessionid in self._sessions: |
|
36 |
raise InvalidSession() |
|
37 |
session = self._sessions[sessionid] |
|
38 |
if self.has_expired(session): |
|
39 |
self.close_session(session) |
|
40 |
raise InvalidSession() |
|
41 |
# give an opportunity to auth manager to hijack the session |
|
42 |
# (necessary with the RepositoryAuthenticationManager in case |
|
43 |
# the connection to the repository has expired) |
|
44 |
try: |
|
45 |
session = self.authmanager.validate_session(req, session) |
|
46 |
# necessary in case session has been hijacked |
|
47 |
self._sessions[session.sessionid] = session |
|
48 |
except InvalidSession: |
|
49 |
# invalid session |
|
50 |
del self._sessions[sessionid] |
|
51 |
raise |
|
52 |
return session |
|
53 |
||
54 |
def open_session(self, req): |
|
55 |
"""open and return a new session for the given request |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
56 |
|
0 | 57 |
:raise ExplicitLogin: if authentication is required |
58 |
""" |
|
59 |
session = self.authmanager.authenticate(req) |
|
60 |
self._sessions[session.sessionid] = session |
|
61 |
return session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
62 |
|
0 | 63 |
def close_session(self, session): |
64 |
"""close session on logout or on invalid session detected (expired out, |
|
65 |
corrupted...) |
|
66 |
""" |
|
67 |
self.info('closing http session %s' % session) |
|
68 |
del self._sessions[session.sessionid] |
|
69 |
try: |
|
70 |
session.close() |
|
71 |
except: |
|
72 |
# already closed, may occurs if the repository session expired but |
|
73 |
# not the web session |
|
74 |
pass |