web/application.py
branchstable
changeset 7428 5338d895b891
parent 7348 dce4fa28ae49
child 7589 d3459fe041f0
equal deleted inserted replaced
7426:254bc099db1a 7428:5338d895b891
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   202             try:
   202             try:
   203                 session = self.get_session(req, sessionid)
   203                 session = self.get_session(req, sessionid)
   204             except InvalidSession:
   204             except InvalidSession:
   205                 # try to open a new session, so we get an anonymous session if
   205                 # try to open a new session, so we get an anonymous session if
   206                 # allowed
   206                 # allowed
   207                 try:
   207                 session = self.open_session(req)
   208                     session = self.open_session(req)
   208             else:
   209                 except AuthenticationError:
   209                 if not session.cnx:
   210                     req.remove_cookie(cookie, sessioncookie)
   210                     # session exists but is not bound to a connection. We should
   211                     raise
   211                     # try to authenticate
       
   212                     loginsucceed = False
       
   213                     try:
       
   214                         if self.open_session(req, allow_no_cnx=False):
       
   215                             loginsucceed = True
       
   216                     except Redirect:
       
   217                         # may be raised in open_session (by postlogin mechanism)
       
   218                         # on successful connection
       
   219                         loginsucceed = True
       
   220                         raise
       
   221                     except AuthenticationError:
       
   222                         # authentication failed, continue to use this session
       
   223                         req.set_session(session)
       
   224                     finally:
       
   225                         if loginsucceed:
       
   226                             # session should be replaced by new session created
       
   227                             # in open_session
       
   228                             self.session_manager.close_session(session)
   212 
   229 
   213     def get_session(self, req, sessionid):
   230     def get_session(self, req, sessionid):
   214         return self.session_manager.get_session(req, sessionid)
   231         return self.session_manager.get_session(req, sessionid)
   215 
   232 
   216     def open_session(self, req):
   233     def open_session(self, req, allow_no_cnx=True):
   217         session = self.session_manager.open_session(req)
   234         session = self.session_manager.open_session(req, allow_no_cnx=allow_no_cnx)
   218         cookie = req.get_cookie()
   235         cookie = req.get_cookie()
   219         sessioncookie = self.session_cookie(req)
   236         sessioncookie = self.session_cookie(req)
   220         cookie[sessioncookie] = session.sessionid
   237         cookie[sessioncookie] = session.sessionid
   221         if req.https and req.base_url().startswith('https://'):
   238         if req.https and req.base_url().startswith('https://'):
   222             cookie[sessioncookie]['secure'] = True
   239             cookie[sessioncookie]['secure'] = True
   277     def connect(self, req):
   294     def connect(self, req):
   278         """return a connection for a logged user object according to existing
   295         """return a connection for a logged user object according to existing
   279         sessions (i.e. a new connection may be created or an already existing
   296         sessions (i.e. a new connection may be created or an already existing
   280         one may be reused
   297         one may be reused
   281         """
   298         """
   282         try:
   299         self.session_handler.set_session(req)
   283             self.session_handler.set_session(req)
       
   284         except AuthenticationError:
       
   285             req.set_session(DBAPISession(None))
       
   286 
   300 
   287     # publish methods #########################################################
   301     # publish methods #########################################################
   288 
   302 
   289     def log_publish(self, path, req):
   303     def log_publish(self, path, req):
   290         """wrapper around _publish to log all queries executed for a given
   304         """wrapper around _publish to log all queries executed for a given
   363                 raise
   377                 raise
   364             except Redirect:
   378             except Redirect:
   365                 # redirect is raised by edit controller when everything went fine,
   379                 # redirect is raised by edit controller when everything went fine,
   366                 # so try to commit
   380                 # so try to commit
   367                 try:
   381                 try:
   368                     txuuid = req.cnx.commit()
   382                     if req.cnx:
   369                     if txuuid is not None:
   383                         txuuid = req.cnx.commit()
   370                         msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' %(
   384                         if txuuid is not None:
   371                             req.build_url('undo', txuuid=txuuid), req._('undo'))
   385                             msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' %(
   372                         req.append_to_redirect_message(msg)
   386                                 req.build_url('undo', txuuid=txuuid), req._('undo'))
       
   387                             req.append_to_redirect_message(msg)
   373                 except ValidationError, ex:
   388                 except ValidationError, ex:
   374                     self.validation_error_handler(req, ex)
   389                     self.validation_error_handler(req, ex)
   375                 except Unauthorized, ex:
   390                 except Unauthorized, ex:
   376                     req.data['errmsg'] = req._('You\'re not authorized to access this page. '
   391                     req.data['errmsg'] = req._('You\'re not authorized to access this page. '
   377                                                'If you think you should, please contact the site administrator.')
   392                                                'If you think you should, please contact the site administrator.')