29 from rql import BadRQLQuery |
29 from rql import BadRQLQuery |
30 |
30 |
31 from cubicweb import set_log_methods, cwvreg |
31 from cubicweb import set_log_methods, cwvreg |
32 from cubicweb import ( |
32 from cubicweb import ( |
33 ValidationError, Unauthorized, AuthenticationError, NoSelectableObject, |
33 ValidationError, Unauthorized, AuthenticationError, NoSelectableObject, |
34 RepositoryError, BadConnectionId, CW_EVENT_MANAGER) |
34 BadConnectionId, CW_EVENT_MANAGER) |
35 from cubicweb.dbapi import DBAPISession |
35 from cubicweb.dbapi import DBAPISession |
36 from cubicweb.web import LOGGER, component |
36 from cubicweb.web import LOGGER, component |
37 from cubicweb.web import ( |
37 from cubicweb.web import ( |
38 StatusResponse, DirectResponse, Redirect, NotFound, LogOut, |
38 StatusResponse, DirectResponse, Redirect, NotFound, LogOut, |
39 RemoteCallFailed, InvalidSession, RequestError) |
39 RemoteCallFailed, InvalidSession, RequestError) |
146 self.vreg = appli.vreg |
146 self.vreg = appli.vreg |
147 self.session_manager = self.vreg['components'].select('sessionmanager', |
147 self.session_manager = self.vreg['components'].select('sessionmanager', |
148 vreg=self.vreg) |
148 vreg=self.vreg) |
149 global SESSION_MANAGER |
149 global SESSION_MANAGER |
150 SESSION_MANAGER = self.session_manager |
150 SESSION_MANAGER = self.session_manager |
151 if not 'last_login_time' in self.vreg.schema: |
|
152 self._update_last_login_time = lambda x: None |
|
153 if self.vreg.config.mode != 'test': |
151 if self.vreg.config.mode != 'test': |
154 # don't try to reset session manager during test, this leads to |
152 # don't try to reset session manager during test, this leads to |
155 # weird failures when running multiple tests |
153 # weird failures when running multiple tests |
156 CW_EVENT_MANAGER.bind('after-registry-reload', |
154 CW_EVENT_MANAGER.bind('after-registry-reload', |
157 self.reset_session_manager) |
155 self.reset_session_manager) |
222 cookie[sessioncookie] = session.sessionid |
220 cookie[sessioncookie] = session.sessionid |
223 if req.https and req.base_url().startswith('https://'): |
221 if req.https and req.base_url().startswith('https://'): |
224 cookie[sessioncookie]['secure'] = True |
222 cookie[sessioncookie]['secure'] = True |
225 req.set_cookie(cookie, sessioncookie, maxage=None) |
223 req.set_cookie(cookie, sessioncookie, maxage=None) |
226 if not session.anonymous_session: |
224 if not session.anonymous_session: |
227 self._postlogin(req) |
225 self.session_manager.postlogin(req) |
228 return session |
226 return session |
229 |
|
230 def _update_last_login_time(self, req): |
|
231 # XXX should properly detect missing permission / non writeable source |
|
232 # and avoid "except (RepositoryError, Unauthorized)" below |
|
233 if req.user.cw_metainformation()['source']['type'] == 'ldapuser': |
|
234 return |
|
235 try: |
|
236 req.execute('SET X last_login_time NOW WHERE X eid %(x)s', |
|
237 {'x' : req.user.eid}) |
|
238 req.cnx.commit() |
|
239 except (RepositoryError, Unauthorized): |
|
240 req.cnx.rollback() |
|
241 except: |
|
242 req.cnx.rollback() |
|
243 raise |
|
244 |
|
245 def _postlogin(self, req): |
|
246 """postlogin: the user has been authenticated, redirect to the original |
|
247 page (index by default) with a welcome message |
|
248 """ |
|
249 # Update last connection date |
|
250 # XXX: this should be in a post login hook in the repository, but there |
|
251 # we can't differentiate actual login of automatic session |
|
252 # reopening. Is it actually a problem? |
|
253 self._update_last_login_time(req) |
|
254 args = req.form |
|
255 for forminternal_key in ('__form_id', '__domid', '__errorurl'): |
|
256 args.pop(forminternal_key, None) |
|
257 args['__message'] = req._('welcome %s !') % req.user.login |
|
258 if 'vid' in req.form: |
|
259 args['vid'] = req.form['vid'] |
|
260 if 'rql' in req.form: |
|
261 args['rql'] = req.form['rql'] |
|
262 path = req.relative_path(False) |
|
263 if path == 'login': |
|
264 path = 'view' |
|
265 raise Redirect(req.build_url(path, **args)) |
|
266 |
227 |
267 def logout(self, req, goto_url): |
228 def logout(self, req, goto_url): |
268 """logout from the instance by cleaning the session and raising |
229 """logout from the instance by cleaning the session and raising |
269 `AuthenticationError` |
230 `AuthenticationError` |
270 """ |
231 """ |