author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:15:52 +0200 | |
branch | oldstable |
changeset 5424 | 8ecbcbff9777 |
parent 5421 | 8167de96c523 |
child 5426 | 0d4853a6e5ee |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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/>. |
0 | 18 |
"""CubicWeb web client application object |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import sys |
|
24 |
from time import clock, time |
|
25 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
26 |
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
|
27 |
|
0 | 28 |
from rql import BadRQLQuery |
29 |
||
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
|
30 |
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
|
31 |
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
|
32 |
ValidationError, Unauthorized, AuthenticationError, NoSelectableObject, |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
33 |
RepositoryError, CW_EVENT_MANAGER) |
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
|
34 |
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
|
35 |
from cubicweb.web 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
|
36 |
StatusResponse, DirectResponse, Redirect, NotFound, |
2293 | 37 |
RemoteCallFailed, ExplicitLogin, InvalidSession, RequestError) |
0 | 38 |
|
39 |
# make session manager available through a global variable so the debug view can |
|
40 |
# print information about web session |
|
41 |
SESSION_MANAGER = None |
|
42 |
||
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
|
43 |
class AbstractSessionManager(component.Component): |
0 | 44 |
"""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
|
45 |
__regid__ = 'sessionmanager' |
1426 | 46 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
47 |
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
|
48 |
self.session_time = vreg.config['http-session-time'] or None |
5283
9ad0eaa09d34
[config] better *-session-time documentation and usage in session handler
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5000
diff
changeset
|
49 |
if self.session_time is not None: |
9ad0eaa09d34
[config] better *-session-time documentation and usage in session handler
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5000
diff
changeset
|
50 |
assert self.session_time > 0 |
9ad0eaa09d34
[config] better *-session-time documentation and usage in session handler
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5000
diff
changeset
|
51 |
self.cleanup_session_time = self.session_time |
9ad0eaa09d34
[config] better *-session-time documentation and usage in session handler
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5000
diff
changeset
|
52 |
else: |
5326
0d9054eb3bd1
[config] properly use time type for options representing a time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5325
diff
changeset
|
53 |
self.cleanup_session_time = vreg.config['cleanup-session-time'] or 1440 * 60 |
5283
9ad0eaa09d34
[config] better *-session-time documentation and usage in session handler
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5000
diff
changeset
|
54 |
assert self.cleanup_session_time > 0 |
5326
0d9054eb3bd1
[config] properly use time type for options representing a time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5325
diff
changeset
|
55 |
self.cleanup_anon_session_time = vreg.config['cleanup-anonymous-session-time'] or 5 * 60 |
0 | 56 |
assert self.cleanup_anon_session_time > 0 |
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
57 |
self.authmanager = vreg['components'].select('authmanager', vreg=vreg) |
5325
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
58 |
if vreg.config.anonymous_user() is not None: |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
59 |
self.clean_sessions_interval = min( |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
60 |
5 * 60, |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
61 |
self.cleanup_session_time / 2., |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
62 |
self.cleanup_anon_session_time / 2.) |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
63 |
else: |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
64 |
self.clean_sessions_interval = min( |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
65 |
5 * 60, |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
66 |
self.cleanup_session_time / 2.) |
1426 | 67 |
|
0 | 68 |
def clean_sessions(self): |
69 |
"""cleanup sessions which has not been unused since a given amount of |
|
70 |
time. Return the number of sessions which have been closed. |
|
71 |
""" |
|
72 |
self.debug('cleaning http sessions') |
|
73 |
closed, total = 0, 0 |
|
74 |
for session in self.current_sessions(): |
|
75 |
no_use_time = (time() - session.last_usage_time) |
|
76 |
total += 1 |
|
77 |
if session.anonymous_connection: |
|
78 |
if no_use_time >= self.cleanup_anon_session_time: |
|
79 |
self.close_session(session) |
|
80 |
closed += 1 |
|
81 |
elif no_use_time >= self.cleanup_session_time: |
|
82 |
self.close_session(session) |
|
83 |
closed += 1 |
|
84 |
return closed, total - closed |
|
1426 | 85 |
|
0 | 86 |
def has_expired(self, session): |
87 |
"""return True if the web session associated to the session is expired |
|
88 |
""" |
|
89 |
return not (self.session_time is None or |
|
90 |
time() < session.last_usage_time + self.session_time) |
|
1426 | 91 |
|
0 | 92 |
def current_sessions(self): |
93 |
"""return currently open sessions""" |
|
94 |
raise NotImplementedError() |
|
1426 | 95 |
|
0 | 96 |
def get_session(self, req, sessionid): |
97 |
"""return existing session for the given session identifier""" |
|
98 |
raise NotImplementedError() |
|
99 |
||
100 |
def open_session(self, req): |
|
101 |
"""open and return a new session for the given request |
|
1426 | 102 |
|
0 | 103 |
:raise ExplicitLogin: if authentication is required |
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 |
|
122 |
def authenticate(self, req): |
|
123 |
"""authenticate user and return corresponding user object |
|
1426 | 124 |
|
0 | 125 |
:raise ExplicitLogin: if authentication is required (no authentication |
126 |
info found or wrong user/password) |
|
127 |
""" |
|
128 |
raise NotImplementedError() |
|
129 |
||
1426 | 130 |
|
0 | 131 |
class CookieSessionHandler(object): |
132 |
"""a session handler using a cookie to store the session identifier |
|
133 |
||
134 |
:cvar SESSION_VAR: |
|
135 |
string giving the name of the variable used to store the session |
|
136 |
identifier |
|
137 |
""" |
|
138 |
SESSION_VAR = '__session' |
|
1426 | 139 |
|
0 | 140 |
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
|
141 |
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
|
142 |
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
|
143 |
vreg=self.vreg) |
0 | 144 |
global SESSION_MANAGER |
145 |
SESSION_MANAGER = self.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
|
146 |
if not 'last_login_time' in self.vreg.schema: |
0 | 147 |
self._update_last_login_time = lambda x: None |
5000
f1a10b41417a
[test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
148 |
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
|
149 |
# 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
|
150 |
# 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
|
151 |
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
|
152 |
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
|
153 |
|
09baf5175196
[web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
SESSION_MANAGER = self.session_manager |
0 | 161 |
|
5325
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
162 |
@property |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
163 |
def clean_sessions_interval(self): |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5283
diff
changeset
|
164 |
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
|
165 |
|
0 | 166 |
def clean_sessions(self): |
167 |
"""cleanup sessions which has not been unused since a given amount of |
|
168 |
time |
|
169 |
""" |
|
170 |
self.session_manager.clean_sessions() |
|
1426 | 171 |
|
0 | 172 |
def set_session(self, req): |
173 |
"""associate a session to the request |
|
174 |
||
175 |
Session id is searched from : |
|
176 |
- # form variable |
|
177 |
- cookie |
|
178 |
||
179 |
if no session id is found, open a new session for the connected user |
|
180 |
or request authentification as needed |
|
181 |
||
1426 | 182 |
:raise Redirect: if authentication has occured and succeed |
0 | 183 |
""" |
184 |
assert req.cnx is None # at this point no cnx should be set on the request |
|
185 |
cookie = req.get_cookie() |
|
186 |
try: |
|
187 |
sessionid = str(cookie[self.SESSION_VAR].value) |
|
188 |
except KeyError: # no session cookie |
|
189 |
session = self.open_session(req) |
|
190 |
else: |
|
191 |
try: |
|
192 |
session = self.get_session(req, sessionid) |
|
193 |
except InvalidSession: |
|
194 |
try: |
|
195 |
session = self.open_session(req) |
|
196 |
except ExplicitLogin: |
|
197 |
req.remove_cookie(cookie, self.SESSION_VAR) |
|
198 |
raise |
|
199 |
# remember last usage time for web session tracking |
|
200 |
session.last_usage_time = time() |
|
201 |
||
202 |
def get_session(self, req, sessionid): |
|
203 |
return self.session_manager.get_session(req, sessionid) |
|
1426 | 204 |
|
0 | 205 |
def open_session(self, req): |
206 |
session = self.session_manager.open_session(req) |
|
207 |
cookie = req.get_cookie() |
|
208 |
cookie[self.SESSION_VAR] = session.sessionid |
|
209 |
req.set_cookie(cookie, self.SESSION_VAR, maxage=None) |
|
210 |
# remember last usage time for web session tracking |
|
211 |
session.last_usage_time = time() |
|
212 |
if not session.anonymous_connection: |
|
213 |
self._postlogin(req) |
|
214 |
return session |
|
215 |
||
216 |
def _update_last_login_time(self, req): |
|
217 |
try: |
|
218 |
req.execute('SET X last_login_time NOW WHERE X eid %(x)s', |
|
219 |
{'x' : req.user.eid}, 'x') |
|
220 |
req.cnx.commit() |
|
221 |
except (RepositoryError, Unauthorized): |
|
222 |
# ldap user are not writeable for instance |
|
223 |
req.cnx.rollback() |
|
224 |
except: |
|
225 |
req.cnx.rollback() |
|
226 |
raise |
|
1426 | 227 |
|
0 | 228 |
def _postlogin(self, req): |
229 |
"""postlogin: the user has been authenticated, redirect to the original |
|
230 |
page (index by default) with a welcome message |
|
231 |
""" |
|
232 |
# Update last connection date |
|
233 |
# XXX: this should be in a post login hook in the repository, but there |
|
234 |
# we can't differentiate actual login of automatic session |
|
235 |
# reopening. Is it actually a problem? |
|
236 |
self._update_last_login_time(req) |
|
237 |
args = req.form |
|
4639
82afdc7d8cd8
cleanup internal forms parameters in postlogin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
238 |
for forminternal_key in ('__form_id', '__domid', '__errorurl'): |
82afdc7d8cd8
cleanup internal forms parameters in postlogin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4490
diff
changeset
|
239 |
args.pop(forminternal_key, None) |
0 | 240 |
args['__message'] = req._('welcome %s !') % req.user.login |
241 |
if 'vid' in req.form: |
|
242 |
args['vid'] = req.form['vid'] |
|
243 |
if 'rql' in req.form: |
|
244 |
args['rql'] = req.form['rql'] |
|
245 |
path = req.relative_path(False) |
|
246 |
if path == 'login': |
|
247 |
path = 'view' |
|
248 |
raise Redirect(req.build_url(path, **args)) |
|
1426 | 249 |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
250 |
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
|
251 |
"""logout from the instance by cleaning the session and raising |
0 | 252 |
`AuthenticationError` |
253 |
""" |
|
254 |
self.session_manager.close_session(req.cnx) |
|
255 |
req.remove_cookie(req.get_cookie(), self.SESSION_VAR) |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4709
diff
changeset
|
256 |
raise AuthenticationError(url=goto_url) |
0 | 257 |
|
258 |
||
259 |
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
|
260 |
"""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
|
261 |
to publish HTTP request. |
0 | 262 |
""" |
1426 | 263 |
|
0 | 264 |
def __init__(self, config, debug=None, |
265 |
session_handler_fact=CookieSessionHandler, |
|
266 |
vreg=None): |
|
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
267 |
self.info('starting web instance from %s', config.apphome) |
0 | 268 |
if vreg is None: |
2666
c6c832d32936
[webapp] missing renaming
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
269 |
vreg = cwvreg.CubicWebVRegistry(config, debug=debug) |
0 | 270 |
self.vreg = vreg |
4484
d87989d91635
fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
271 |
# connect to the repository and get instance's schema |
0 | 272 |
self.repo = config.repository(vreg) |
273 |
if not vreg.initialized: |
|
274 |
self.config.init_cubes(self.repo.get_cubes()) |
|
275 |
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
|
276 |
vreg.set_schema(self.repo.get_schema()) |
0 | 277 |
# set the correct publish method |
278 |
if config['query-log-file']: |
|
279 |
from threading import Lock |
|
280 |
self._query_log = open(config['query-log-file'], 'a') |
|
281 |
self.publish = self.log_publish |
|
1426 | 282 |
self._logfile_lock = Lock() |
0 | 283 |
else: |
284 |
self._query_log = None |
|
285 |
self.publish = self.main_publish |
|
286 |
# instantiate session and url resolving helpers |
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
|
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2666
diff
changeset
|
291 |
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
|
292 |
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
|
293 |
vreg=self.vreg) |
1426 | 294 |
|
0 | 295 |
def connect(self, req): |
296 |
"""return a connection for a logged user object according to existing |
|
297 |
sessions (i.e. a new connection may be created or an already existing |
|
298 |
one may be reused |
|
299 |
""" |
|
300 |
self.session_handler.set_session(req) |
|
301 |
||
302 |
# publish methods ######################################################### |
|
1426 | 303 |
|
0 | 304 |
def log_publish(self, path, req): |
305 |
"""wrapper around _publish to log all queries executed for a given |
|
306 |
accessed path |
|
307 |
""" |
|
308 |
try: |
|
309 |
return self.main_publish(path, req) |
|
310 |
finally: |
|
311 |
cnx = req.cnx |
|
312 |
self._logfile_lock.acquire() |
|
313 |
try: |
|
314 |
try: |
|
315 |
result = ['\n'+'*'*80] |
|
316 |
result.append(req.url()) |
|
317 |
result += ['%s %s -- (%.3f sec, %.3f CPU sec)' % q for q in cnx.executed_queries] |
|
318 |
cnx.executed_queries = [] |
|
319 |
self._query_log.write('\n'.join(result).encode(req.encoding)) |
|
320 |
self._query_log.flush() |
|
321 |
except Exception: |
|
322 |
self.exception('error while logging queries') |
|
323 |
finally: |
|
324 |
self._logfile_lock.release() |
|
325 |
||
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2706
diff
changeset
|
326 |
@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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
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
|
332 |
|
0 | 333 |
def main_publish(self, path, req): |
334 |
"""method called by the main publisher to process <path> |
|
1426 | 335 |
|
0 | 336 |
should return a string containing the resulting page or raise a |
337 |
`NotFound` exception |
|
338 |
||
339 |
:type path: str |
|
340 |
:param path: the path part of the url to publish |
|
1426 | 341 |
|
0 | 342 |
:type req: `web.Request` |
343 |
:param req: the request object |
|
344 |
||
345 |
:rtype: str |
|
346 |
:return: the result of the pusblished url |
|
347 |
""" |
|
348 |
path = path or 'view' |
|
349 |
# don't log form values they may contains sensitive information |
|
350 |
self.info('publish "%s" (form params: %s)', path, req.form.keys()) |
|
351 |
# remove user callbacks on a new request (except for json controllers |
|
352 |
# to avoid callbacks being unregistered before they could be called) |
|
353 |
tstart = clock() |
|
354 |
try: |
|
355 |
try: |
|
356 |
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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
req.update_search_state() |
0 | 363 |
result = controller.publish(rset=rset) |
364 |
if req.cnx is not None: |
|
365 |
# req.cnx is None if anonymous aren't allowed and we are |
|
366 |
# displaying the cookie authentication form |
|
367 |
req.cnx.commit() |
|
368 |
except (StatusResponse, DirectResponse): |
|
369 |
req.cnx.commit() |
|
370 |
raise |
|
371 |
except Redirect: |
|
372 |
# redirect is raised by edit controller when everything went fine, |
|
373 |
# so try to commit |
|
374 |
try: |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
375 |
txuuid = req.cnx.commit() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
376 |
if txuuid is not None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
377 |
msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' %( |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
378 |
req.build_url('undo', txuuid=txuuid), req._('undo')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4897
diff
changeset
|
379 |
req.append_to_redirect_message(msg) |
0 | 380 |
except ValidationError, ex: |
381 |
self.validation_error_handler(req, ex) |
|
382 |
except Unauthorized, ex: |
|
383 |
req.data['errmsg'] = req._('You\'re not authorized to access this page. ' |
|
384 |
'If you think you should, please contact the site administrator.') |
|
385 |
self.error_handler(req, ex, tb=False) |
|
386 |
except Exception, ex: |
|
387 |
self.error_handler(req, ex, tb=True) |
|
388 |
else: |
|
389 |
# delete validation errors which may have been previously set |
|
390 |
if '__errorurl' in req.form: |
|
391 |
req.del_session_data(req.form['__errorurl']) |
|
392 |
raise |
|
393 |
except (AuthenticationError, NotFound, RemoteCallFailed): |
|
394 |
raise |
|
395 |
except ValidationError, ex: |
|
396 |
self.validation_error_handler(req, ex) |
|
2272
f27a3a75be0d
no tb for RequestError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2263
diff
changeset
|
397 |
except (Unauthorized, BadRQLQuery, RequestError), ex: |
0 | 398 |
self.error_handler(req, ex, tb=False) |
399 |
except Exception, ex: |
|
400 |
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
|
401 |
except: |
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5326
diff
changeset
|
402 |
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
|
403 |
self.exception('this is what happened') |
0 | 404 |
finally: |
405 |
if req.cnx is not None: |
|
406 |
try: |
|
407 |
req.cnx.rollback() |
|
408 |
except: |
|
409 |
pass # ignore rollback error at this point |
|
410 |
self.info('query %s executed in %s sec', req.relative_path(), clock() - tstart) |
|
411 |
return result |
|
412 |
||
413 |
def validation_error_handler(self, req, ex): |
|
414 |
ex.errors = dict((k, v) for k, v in ex.errors.items()) |
|
415 |
if '__errorurl' in req.form: |
|
4224
5998df006968
refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3408
diff
changeset
|
416 |
forminfo = {'error': ex, |
0 | 417 |
'values': req.form, |
418 |
'eidmap': req.data.get('eidmap', {}) |
|
419 |
} |
|
420 |
req.set_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
|
421 |
# 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
|
422 |
# 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
|
423 |
# 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
|
424 |
# messages. |
d8ad65dab3e9
remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4639
diff
changeset
|
425 |
raise Redirect(req.form['__errorurl'].rsplit('#', 1)[0]) |
0 | 426 |
self.error_handler(req, ex, tb=False) |
1426 | 427 |
|
0 | 428 |
def error_handler(self, req, ex, tb=False): |
429 |
excinfo = sys.exc_info() |
|
430 |
self.exception(repr(ex)) |
|
431 |
req.set_header('Cache-Control', 'no-cache') |
|
432 |
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
|
433 |
req.reset_message() |
0 | 434 |
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
|
435 |
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
|
436 |
raise RemoteCallFailed(unicode(ex)) |
0 | 437 |
try: |
438 |
req.data['ex'] = ex |
|
439 |
if tb: |
|
440 |
req.data['excinfo'] = excinfo |
|
441 |
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
|
442 |
errview = self.vreg['views'].select('error', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
443 |
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
|
444 |
content = self.vreg['views'].main_template(req, template, view=errview) |
0 | 445 |
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
|
446 |
content = self.vreg['views'].main_template(req, 'error-template') |
0 | 447 |
raise StatusResponse(500, content) |
1426 | 448 |
|
0 | 449 |
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
|
450 |
return self.vreg['views'].main_template(req, 'login') |
1426 | 451 |
|
0 | 452 |
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
|
453 |
return self.vreg['views'].main_template(req, 'loggedout') |
1426 | 454 |
|
0 | 455 |
def notfound_content(self, req): |
456 |
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
|
457 |
view = self.vreg['views'].select('404', req) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
458 |
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
|
459 |
return self.vreg['views'].main_template(req, template, view=view) |
0 | 460 |
|
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
461 |
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
|
462 |
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
|
463 |
if template not in self.vreg['views']: |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
464 |
template = 'main-template' |
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
871
diff
changeset
|
465 |
return template |
1426 | 466 |
|
0 | 467 |
|
468 |
set_log_methods(CubicWebPublisher, LOGGER) |
|
469 |
set_log_methods(CookieSessionHandler, LOGGER) |