author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 30 Jun 2011 16:26:25 +0200 | |
branch | stable |
changeset 7589 | d3459fe041f0 |
parent 7428 | 5338d895b891 |
child 7815 | 2a164a9cf81c |
permissions | -rw-r--r-- |
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:
7348
diff
changeset
|
1 |
# copyright 2003-2011 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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
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:
5377
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5722
61d6a4caa963
[iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5655
diff
changeset
|
18 |
"""CubicWeb web client application object""" |
0 | 19 |
|
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:
5174
diff
changeset
|
20 |
from __future__ import with_statement |
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:
5174
diff
changeset
|
21 |
|
0 | 22 |
__docformat__ = "restructuredtext en" |
23 |
||
24 |
import sys |
|
25 |
from time import clock, time |
|
26 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
27 |
from logilab.common.deprecation import deprecated |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
|
0 | 29 |
from rql import BadRQLQuery |
30 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
31 |
from cubicweb import set_log_methods, cwvreg |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
32 |
from cubicweb import ( |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
33 |
ValidationError, Unauthorized, AuthenticationError, NoSelectableObject, |
6791
fe58b234f9c2
[web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6680
diff
changeset
|
34 |
BadConnectionId, CW_EVENT_MANAGER) |
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:
5174
diff
changeset
|
35 |
from cubicweb.dbapi import DBAPISession |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
36 |
from cubicweb.web import LOGGER, component |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
37 |
from cubicweb.web import ( |
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:
5174
diff
changeset
|
38 |
StatusResponse, DirectResponse, Redirect, NotFound, LogOut, |
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:
5174
diff
changeset
|
39 |
RemoteCallFailed, InvalidSession, RequestError) |
0 | 40 |
|
41 |
# make session manager available through a global variable so the debug view can |
|
42 |
# print information about web session |
|
43 |
SESSION_MANAGER = None |
|
44 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
class AbstractSessionManager(component.Component): |
0 | 46 |
"""manage session data associated to a session identifier""" |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2887
diff
changeset
|
47 |
__regid__ = 'sessionmanager' |
1426 | 48 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
49 |
def __init__(self, vreg): |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
50 |
self.session_time = vreg.config['http-session-time'] or None |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
51 |
self.authmanager = vreg['components'].select('authmanager', vreg=vreg) |
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
52 |
interval = (self.session_time or 0) / 2. |
5325
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
53 |
if vreg.config.anonymous_user() is not None: |
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
54 |
self.cleanup_anon_session_time = vreg.config['cleanup-anonymous-session-time'] or 5 * 60 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
55 |
assert self.cleanup_anon_session_time > 0 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
56 |
if self.session_time is not None: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
57 |
self.cleanup_anon_session_time = min(self.session_time, |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
58 |
self.cleanup_anon_session_time) |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
59 |
interval = self.cleanup_anon_session_time / 2. |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
60 |
# we don't want to check session more than once every 5 minutes |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
61 |
self.clean_sessions_interval = max(5 * 60, interval) |
1426 | 62 |
|
0 | 63 |
def clean_sessions(self): |
64 |
"""cleanup sessions which has not been unused since a given amount of |
|
65 |
time. Return the number of sessions which have been closed. |
|
66 |
""" |
|
67 |
self.debug('cleaning http sessions') |
|
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
68 |
session_time = self.session_time |
0 | 69 |
closed, total = 0, 0 |
70 |
for session in self.current_sessions(): |
|
71 |
total += 1 |
|
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
72 |
try: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
73 |
last_usage_time = session.cnx.check() |
7589
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
74 |
except AttributeError: |
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
75 |
last_usage_time = session.mtime |
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
76 |
except BadConnectionId: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
77 |
self.close_session(session) |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
78 |
closed += 1 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
79 |
else: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
80 |
no_use_time = (time() - last_usage_time) |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
81 |
if session.anonymous_session: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
82 |
if no_use_time >= self.cleanup_anon_session_time: |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
83 |
self.close_session(session) |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
84 |
closed += 1 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
85 |
elif session_time is not None and no_use_time >= session_time: |
0 | 86 |
self.close_session(session) |
87 |
closed += 1 |
|
88 |
return closed, total - closed |
|
1426 | 89 |
|
0 | 90 |
def current_sessions(self): |
91 |
"""return currently open sessions""" |
|
92 |
raise NotImplementedError() |
|
1426 | 93 |
|
0 | 94 |
def get_session(self, req, sessionid): |
95 |
"""return existing session for the given session identifier""" |
|
96 |
raise NotImplementedError() |
|
97 |
||
98 |
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:
5174
diff
changeset
|
99 |
"""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:
5174
diff
changeset
|
100 |
also bound to the request. |
1426 | 101 |
|
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:
5174
diff
changeset
|
102 |
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:
5174
diff
changeset
|
103 |
(no authentication info found or wrong user/password) |
0 | 104 |
""" |
105 |
raise NotImplementedError() |
|
1426 | 106 |
|
0 | 107 |
def close_session(self, session): |
108 |
"""close session on logout or on invalid session detected (expired out, |
|
109 |
corrupted...) |
|
110 |
""" |
|
111 |
raise NotImplementedError() |
|
112 |
||
113 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
114 |
class AbstractAuthenticationManager(component.Component): |
0 | 115 |
"""authenticate user associated to a request and check session validity""" |
116 |
id = 'authmanager' |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
117 |
vreg = None # XXX necessary until property for deprecation warning is on appobject |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
118 |
|
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
119 |
def __init__(self, vreg): |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
120 |
self.vreg = vreg |
0 | 121 |
|
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:
5174
diff
changeset
|
122 |
def validate_session(self, req, session): |
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:
5174
diff
changeset
|
123 |
"""check session validity, reconnecting it to the repository if the |
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:
5174
diff
changeset
|
124 |
associated connection expired in the repository side (hence the |
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:
5174
diff
changeset
|
125 |
necessity for this method). |
1426 | 126 |
|
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:
5174
diff
changeset
|
127 |
raise :exc:`InvalidSession` if session is corrupted for a reason or |
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:
5174
diff
changeset
|
128 |
another and should be closed |
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:
5174
diff
changeset
|
129 |
""" |
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:
5174
diff
changeset
|
130 |
raise NotImplementedError() |
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:
5174
diff
changeset
|
131 |
|
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:
5174
diff
changeset
|
132 |
def authenticate(self, req): |
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:
5174
diff
changeset
|
133 |
"""authenticate user using connection information found in the request, |
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:
5174
diff
changeset
|
134 |
and return corresponding a :class:`~cubicweb.dbapi.Connection` instance, |
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:
5174
diff
changeset
|
135 |
as well as login and authentication information dictionary used to open |
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:
5174
diff
changeset
|
136 |
the connection. |
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:
5174
diff
changeset
|
137 |
|
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:
5174
diff
changeset
|
138 |
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:
5174
diff
changeset
|
139 |
(no authentication info found or wrong user/password) |
0 | 140 |
""" |
141 |
raise NotImplementedError() |
|
142 |
||
1426 | 143 |
|
0 | 144 |
class CookieSessionHandler(object): |
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
145 |
"""a session handler using a cookie to store the session identifier""" |
1426 | 146 |
|
0 | 147 |
def __init__(self, appli): |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
148 |
self.vreg = appli.vreg |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
149 |
self.session_manager = self.vreg['components'].select('sessionmanager', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
150 |
vreg=self.vreg) |
0 | 151 |
global SESSION_MANAGER |
152 |
SESSION_MANAGER = self.session_manager |
|
5000
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
153 |
if self.vreg.config.mode != 'test': |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
154 |
# don't try to reset session manager during test, this leads to |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
155 |
# weird failures when running multiple tests |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
156 |
CW_EVENT_MANAGER.bind('after-registry-reload', |
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
157 |
self.reset_session_manager) |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
158 |
|
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
159 |
def reset_session_manager(self): |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
160 |
data = self.session_manager.dump_data() |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
161 |
self.session_manager = self.vreg['components'].select('sessionmanager', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
162 |
vreg=self.vreg) |
2706
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
163 |
self.session_manager.restore_data(data) |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
164 |
global SESSION_MANAGER |
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
165 |
SESSION_MANAGER = self.session_manager |
0 | 166 |
|
5325
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
167 |
@property |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
168 |
def clean_sessions_interval(self): |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
169 |
return self.session_manager.clean_sessions_interval |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
170 |
|
0 | 171 |
def clean_sessions(self): |
172 |
"""cleanup sessions which has not been unused since a given amount of |
|
173 |
time |
|
174 |
""" |
|
175 |
self.session_manager.clean_sessions() |
|
1426 | 176 |
|
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
177 |
def session_cookie(self, req): |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
178 |
"""return a string giving the name of the cookie used to store the |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
179 |
session identifier. |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
180 |
""" |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
181 |
if req.https: |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
182 |
return '__%s_https_session' % self.vreg.config.appid |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
183 |
return '__%s_session' % self.vreg.config.appid |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
184 |
|
0 | 185 |
def set_session(self, req): |
186 |
"""associate a session to the request |
|
187 |
||
188 |
Session id is searched from : |
|
189 |
- # form variable |
|
190 |
- cookie |
|
191 |
||
192 |
if no session id is found, open a new session for the connected user |
|
193 |
or request authentification as needed |
|
194 |
||
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5868
diff
changeset
|
195 |
:raise Redirect: if authentication has occurred and succeed |
0 | 196 |
""" |
197 |
cookie = req.get_cookie() |
|
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
198 |
sessioncookie = self.session_cookie(req) |
0 | 199 |
try: |
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
200 |
sessionid = str(cookie[sessioncookie].value) |
0 | 201 |
except KeyError: # no session cookie |
202 |
session = self.open_session(req) |
|
203 |
else: |
|
204 |
try: |
|
205 |
session = self.get_session(req, sessionid) |
|
206 |
except InvalidSession: |
|
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:
5174
diff
changeset
|
207 |
# try to open a new session, so we get an anonymous session if |
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:
5174
diff
changeset
|
208 |
# allowed |
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:
7348
diff
changeset
|
209 |
session = self.open_session(req) |
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:
7348
diff
changeset
|
210 |
else: |
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:
7348
diff
changeset
|
211 |
if not session.cnx: |
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:
7348
diff
changeset
|
212 |
# session exists but is not bound to a connection. We should |
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:
7348
diff
changeset
|
213 |
# try to authenticate |
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:
7348
diff
changeset
|
214 |
loginsucceed = False |
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:
7348
diff
changeset
|
215 |
try: |
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:
7348
diff
changeset
|
216 |
if self.open_session(req, allow_no_cnx=False): |
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:
7348
diff
changeset
|
217 |
loginsucceed = True |
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:
7348
diff
changeset
|
218 |
except Redirect: |
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:
7348
diff
changeset
|
219 |
# may be raised in open_session (by postlogin mechanism) |
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:
7348
diff
changeset
|
220 |
# on successful connection |
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:
7348
diff
changeset
|
221 |
loginsucceed = True |
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:
7348
diff
changeset
|
222 |
raise |
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:
7348
diff
changeset
|
223 |
except AuthenticationError: |
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:
7348
diff
changeset
|
224 |
# authentication failed, continue to use this session |
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:
7348
diff
changeset
|
225 |
req.set_session(session) |
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:
7348
diff
changeset
|
226 |
finally: |
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:
7348
diff
changeset
|
227 |
if loginsucceed: |
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:
7348
diff
changeset
|
228 |
# session should be replaced by new session created |
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:
7348
diff
changeset
|
229 |
# in open_session |
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:
7348
diff
changeset
|
230 |
self.session_manager.close_session(session) |
0 | 231 |
|
232 |
def get_session(self, req, sessionid): |
|
7589
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
233 |
session = self.session_manager.get_session(req, sessionid) |
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
234 |
session.mtime = time() |
d3459fe041f0
[web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7428
diff
changeset
|
235 |
return session |
1426 | 236 |
|
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:
7348
diff
changeset
|
237 |
def open_session(self, req, allow_no_cnx=True): |
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:
7348
diff
changeset
|
238 |
session = self.session_manager.open_session(req, allow_no_cnx=allow_no_cnx) |
0 | 239 |
cookie = req.get_cookie() |
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
240 |
sessioncookie = self.session_cookie(req) |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
241 |
cookie[sessioncookie] = session.sessionid |
6582
8eb7883b4223
[pylint] fix a bug of pylint detected errors and i18n pb (calling builtins._ instead of req._)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6570
diff
changeset
|
242 |
if req.https and req.base_url().startswith('https://'): |
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
243 |
cookie[sessioncookie]['secure'] = True |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
244 |
req.set_cookie(cookie, sessioncookie, maxage=None) |
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:
5174
diff
changeset
|
245 |
if not session.anonymous_session: |
6791
fe58b234f9c2
[web session] refactor to finally closes #343036: allow _postlogin behaviour overloading
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6680
diff
changeset
|
246 |
self.session_manager.postlogin(req) |
0 | 247 |
return session |
248 |
||
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
249 |
def logout(self, req, goto_url): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
250 |
"""logout from the instance by cleaning the session and raising |
0 | 251 |
`AuthenticationError` |
252 |
""" |
|
5494
f3bb53f1737c
[web session] fix potential key error on logout (occurs once the session has been transparently reconnected, hence session.sessionid and session.cnx.sessionid differs)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
253 |
self.session_manager.close_session(req.session) |
6680
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
254 |
sessioncookie = self.session_cookie(req) |
b62ebf1d9388
[web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
255 |
req.remove_cookie(req.get_cookie(), sessioncookie) |
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:
5174
diff
changeset
|
256 |
raise LogOut(url=goto_url) |
0 | 257 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
258 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
259 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
260 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 261 |
|
262 |
class CubicWebPublisher(object): |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
263 |
"""the publisher is a singleton hold by the web frontend, and is responsible |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
264 |
to publish HTTP request. |
0 | 265 |
""" |
1426 | 266 |
|
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
267 |
def __init__(self, config, |
0 | 268 |
session_handler_fact=CookieSessionHandler, |
269 |
vreg=None): |
|
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
270 |
self.info('starting web instance from %s', config.apphome) |
0 | 271 |
if vreg is None: |
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
272 |
vreg = cwvreg.CubicWebVRegistry(config) |
0 | 273 |
self.vreg = vreg |
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
274 |
# connect to the repository and get instance's schema |
0 | 275 |
self.repo = config.repository(vreg) |
276 |
if not vreg.initialized: |
|
5650
86e874fe30ea
[web] cleanup use of config in web application initialisation
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5587
diff
changeset
|
277 |
config.init_cubes(self.repo.get_cubes()) |
0 | 278 |
vreg.init_properties(self.repo.properties()) |
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
279 |
vreg.set_schema(self.repo.get_schema()) |
0 | 280 |
# set the correct publish method |
281 |
if config['query-log-file']: |
|
282 |
from threading import Lock |
|
283 |
self._query_log = open(config['query-log-file'], 'a') |
|
284 |
self.publish = self.log_publish |
|
1426 | 285 |
self._logfile_lock = Lock() |
0 | 286 |
else: |
287 |
self._query_log = None |
|
288 |
self.publish = self.main_publish |
|
289 |
# instantiate session and url resolving helpers |
|
290 |
self.session_handler = session_handler_fact(self) |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
291 |
self.set_urlresolver() |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2685
diff
changeset
|
292 |
CW_EVENT_MANAGER.bind('after-registry-reload', self.set_urlresolver) |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
293 |
|
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
294 |
def set_urlresolver(self): |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
295 |
self.url_resolver = self.vreg['components'].select('urlpublisher', |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
296 |
vreg=self.vreg) |
1426 | 297 |
|
0 | 298 |
def connect(self, req): |
299 |
"""return a connection for a logged user object according to existing |
|
300 |
sessions (i.e. a new connection may be created or an already existing |
|
301 |
one may be reused |
|
302 |
""" |
|
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:
7348
diff
changeset
|
303 |
self.session_handler.set_session(req) |
0 | 304 |
|
305 |
# publish methods ######################################################### |
|
1426 | 306 |
|
0 | 307 |
def log_publish(self, path, req): |
308 |
"""wrapper around _publish to log all queries executed for a given |
|
309 |
accessed path |
|
310 |
""" |
|
311 |
try: |
|
312 |
return self.main_publish(path, req) |
|
313 |
finally: |
|
314 |
cnx = req.cnx |
|
5244
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
315 |
if cnx: |
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:
5174
diff
changeset
|
316 |
with self._logfile_lock: |
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:
5174
diff
changeset
|
317 |
try: |
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:
5174
diff
changeset
|
318 |
result = ['\n'+'*'*80] |
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:
5174
diff
changeset
|
319 |
result.append(req.url()) |
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:
5174
diff
changeset
|
320 |
result += ['%s %s -- (%.3f sec, %.3f CPU sec)' % q |
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:
5174
diff
changeset
|
321 |
for q in cnx.executed_queries] |
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:
5174
diff
changeset
|
322 |
cnx.executed_queries = [] |
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:
5174
diff
changeset
|
323 |
self._query_log.write('\n'.join(result).encode(req.encoding)) |
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:
5174
diff
changeset
|
324 |
self._query_log.flush() |
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:
5174
diff
changeset
|
325 |
except Exception: |
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:
5174
diff
changeset
|
326 |
self.exception('error while logging queries') |
0 | 327 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2706
diff
changeset
|
328 |
@deprecated("[3.4] use vreg['controllers'].select(...)") |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
329 |
def select_controller(self, oid, req): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
330 |
try: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
331 |
return self.vreg['controllers'].select(oid, req=req, appli=self) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
332 |
except NoSelectableObject: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
333 |
raise Unauthorized(req._('not authorized')) |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
334 |
|
0 | 335 |
def main_publish(self, path, req): |
336 |
"""method called by the main publisher to process <path> |
|
1426 | 337 |
|
0 | 338 |
should return a string containing the resulting page or raise a |
339 |
`NotFound` exception |
|
340 |
||
341 |
:type path: str |
|
342 |
:param path: the path part of the url to publish |
|
1426 | 343 |
|
0 | 344 |
:type req: `web.Request` |
345 |
:param req: the request object |
|
346 |
||
347 |
:rtype: str |
|
348 |
:return: the result of the pusblished url |
|
349 |
""" |
|
350 |
path = path or 'view' |
|
351 |
# don't log form values they may contains sensitive information |
|
6318
de6264ac7c50
[publisher] add session id when logging published path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6273
diff
changeset
|
352 |
self.info('publish "%s" (%s, form params: %s)', |
de6264ac7c50
[publisher] add session id when logging published path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6273
diff
changeset
|
353 |
path, req.session.sessionid, req.form.keys()) |
0 | 354 |
# remove user callbacks on a new request (except for json controllers |
355 |
# to avoid callbacks being unregistered before they could be called) |
|
356 |
tstart = clock() |
|
5865
af414723598d
[publisher] avoid useless rollback after successful commit, which clutters debug logs and may also not be cost-free
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5721
diff
changeset
|
357 |
commited = False |
0 | 358 |
try: |
359 |
try: |
|
360 |
ctrlid, rset = self.url_resolver.process(req, path) |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
361 |
try: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
362 |
controller = self.vreg['controllers'].select(ctrlid, req, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
363 |
appli=self) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
364 |
except NoSelectableObject: |
5584
c1823448f81d
[web] disallow authenticated users to access to the login form (closes #914873)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5494
diff
changeset
|
365 |
if ctrlid == 'login': |
c1823448f81d
[web] disallow authenticated users to access to the login form (closes #914873)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5494
diff
changeset
|
366 |
raise Unauthorized(req._('log out first')) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
367 |
raise Unauthorized(req._('not authorized')) |
581
09f87f2c535e
update_search_state in the publisher since it should be done whatever the controller
sylvain.thenault@logilab.fr
parents:
168
diff
changeset
|
368 |
req.update_search_state() |
0 | 369 |
result = controller.publish(rset=rset) |
5244
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
370 |
if req.cnx: |
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
371 |
# no req.cnx if anonymous aren't allowed and we are |
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
372 |
# displaying some anonymous enabled view such as the cookie |
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
373 |
# authentication form |
0 | 374 |
req.cnx.commit() |
5865
af414723598d
[publisher] avoid useless rollback after successful commit, which clutters debug logs and may also not be cost-free
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5721
diff
changeset
|
375 |
commited = True |
0 | 376 |
except (StatusResponse, DirectResponse): |
5244
5467674ad101
[web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
377 |
if req.cnx: |
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:
5174
diff
changeset
|
378 |
req.cnx.commit() |
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:
5174
diff
changeset
|
379 |
raise |
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:
5174
diff
changeset
|
380 |
except (AuthenticationError, LogOut): |
0 | 381 |
raise |
382 |
except Redirect: |
|
383 |
# redirect is raised by edit controller when everything went fine, |
|
384 |
# so try to commit |
|
385 |
try: |
|
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:
7348
diff
changeset
|
386 |
if req.cnx: |
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:
7348
diff
changeset
|
387 |
txuuid = req.cnx.commit() |
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:
7348
diff
changeset
|
388 |
if txuuid is not None: |
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:
7348
diff
changeset
|
389 |
msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' %( |
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:
7348
diff
changeset
|
390 |
req.build_url('undo', txuuid=txuuid), req._('undo')) |
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:
7348
diff
changeset
|
391 |
req.append_to_redirect_message(msg) |
0 | 392 |
except ValidationError, ex: |
393 |
self.validation_error_handler(req, ex) |
|
394 |
except Unauthorized, ex: |
|
395 |
req.data['errmsg'] = req._('You\'re not authorized to access this page. ' |
|
396 |
'If you think you should, please contact the site administrator.') |
|
397 |
self.error_handler(req, ex, tb=False) |
|
398 |
except Exception, ex: |
|
399 |
self.error_handler(req, ex, tb=True) |
|
400 |
else: |
|
401 |
# delete validation errors which may have been previously set |
|
402 |
if '__errorurl' in req.form: |
|
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:
5174
diff
changeset
|
403 |
req.session.data.pop(req.form['__errorurl'], None) |
0 | 404 |
raise |
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:
5174
diff
changeset
|
405 |
except RemoteCallFailed, ex: |
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:
5174
diff
changeset
|
406 |
req.set_header('content-type', 'application/json') |
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:
5174
diff
changeset
|
407 |
raise StatusResponse(500, ex.dumps()) |
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:
5174
diff
changeset
|
408 |
except NotFound: |
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:
5174
diff
changeset
|
409 |
raise StatusResponse(404, self.notfound_content(req)) |
0 | 410 |
except ValidationError, ex: |
411 |
self.validation_error_handler(req, ex) |
|
7348
dce4fa28ae49
[web server] #1642893 issue a 403 instead of a 500 on Unauthorized exception
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7083
diff
changeset
|
412 |
except Unauthorized, ex: |
dce4fa28ae49
[web server] #1642893 issue a 403 instead of a 500 on Unauthorized exception
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7083
diff
changeset
|
413 |
self.error_handler(req, ex, tb=False, code=403) |
dce4fa28ae49
[web server] #1642893 issue a 403 instead of a 500 on Unauthorized exception
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7083
diff
changeset
|
414 |
except (BadRQLQuery, RequestError), ex: |
0 | 415 |
self.error_handler(req, ex, tb=False) |
6272
bb546568e466
[publisher] with python>=2.5, we should catch BaseException here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
416 |
except BaseException, ex: |
0 | 417 |
self.error_handler(req, ex, tb=True) |
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5326
diff
changeset
|
418 |
except: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5326
diff
changeset
|
419 |
self.critical('Catch all triggered!!!') |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5326
diff
changeset
|
420 |
self.exception('this is what happened') |
6273
8dbdd4cc7938
[publisher] avoid extra Name error on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6272
diff
changeset
|
421 |
result = 'oops' |
0 | 422 |
finally: |
5865
af414723598d
[publisher] avoid useless rollback after successful commit, which clutters debug logs and may also not be cost-free
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5721
diff
changeset
|
423 |
if req.cnx and not commited: |
0 | 424 |
try: |
425 |
req.cnx.rollback() |
|
426 |
except: |
|
427 |
pass # ignore rollback error at this point |
|
428 |
self.info('query %s executed in %s sec', req.relative_path(), clock() - tstart) |
|
429 |
return result |
|
430 |
||
431 |
def validation_error_handler(self, req, ex): |
|
432 |
ex.errors = dict((k, v) for k, v in ex.errors.items()) |
|
433 |
if '__errorurl' in req.form: |
|
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3408
diff
changeset
|
434 |
forminfo = {'error': ex, |
0 | 435 |
'values': req.form, |
436 |
'eidmap': req.data.get('eidmap', {}) |
|
437 |
} |
|
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:
5174
diff
changeset
|
438 |
req.session.data[req.form['__errorurl']] = forminfo |
4679
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
439 |
# XXX form session key / __error_url should be differentiated: |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
440 |
# session key is 'url + #<form dom id', though we usually don't want |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
441 |
# the browser to move to the form since it hides the global |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
442 |
# messages. |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
443 |
raise Redirect(req.form['__errorurl'].rsplit('#', 1)[0]) |
0 | 444 |
self.error_handler(req, ex, tb=False) |
1426 | 445 |
|
7348
dce4fa28ae49
[web server] #1642893 issue a 403 instead of a 500 on Unauthorized exception
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7083
diff
changeset
|
446 |
def error_handler(self, req, ex, tb=False, code=500): |
0 | 447 |
excinfo = sys.exc_info() |
448 |
self.exception(repr(ex)) |
|
449 |
req.set_header('Cache-Control', 'no-cache') |
|
450 |
req.remove_header('Etag') |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
451 |
req.reset_message() |
0 | 452 |
req.reset_headers() |
4709
6a71fc0b4274
[web] fix #724769: Use RemoteCallFailed in the publisher's error_handler
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4679
diff
changeset
|
453 |
if req.json_request: |
6a71fc0b4274
[web] fix #724769: Use RemoteCallFailed in the publisher's error_handler
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4679
diff
changeset
|
454 |
raise RemoteCallFailed(unicode(ex)) |
0 | 455 |
try: |
456 |
req.data['ex'] = ex |
|
457 |
if tb: |
|
458 |
req.data['excinfo'] = excinfo |
|
459 |
req.form['vid'] = 'error' |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
460 |
errview = self.vreg['views'].select('error', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
461 |
template = self.main_template_id(req) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
462 |
content = self.vreg['views'].main_template(req, template, view=errview) |
0 | 463 |
except: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
464 |
content = self.vreg['views'].main_template(req, 'error-template') |
7348
dce4fa28ae49
[web server] #1642893 issue a 403 instead of a 500 on Unauthorized exception
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7083
diff
changeset
|
465 |
raise StatusResponse(code, content) |
1426 | 466 |
|
0 | 467 |
def need_login_content(self, req): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
468 |
return self.vreg['views'].main_template(req, 'login') |
1426 | 469 |
|
0 | 470 |
def loggedout_content(self, req): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
471 |
return self.vreg['views'].main_template(req, 'loggedout') |
1426 | 472 |
|
0 | 473 |
def notfound_content(self, req): |
474 |
req.form['vid'] = '404' |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
475 |
view = self.vreg['views'].select('404', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
476 |
template = self.main_template_id(req) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
477 |
return self.vreg['views'].main_template(req, template, view=view) |
0 | 478 |
|
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
479 |
def main_template_id(self, req): |
2263
1f59cd5b710f
accept a __template parameter that specifies a different (main) template
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1977
diff
changeset
|
480 |
template = req.form.get('__template', req.property_value('ui.main-template')) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
481 |
if template not in self.vreg['views']: |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
482 |
template = 'main-template' |
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
483 |
return template |
1426 | 484 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
485 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
486 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6791
diff
changeset
|
487 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 488 |
|
489 |
set_log_methods(CubicWebPublisher, LOGGER) |
|
490 |
set_log_methods(CookieSessionHandler, LOGGER) |