[pyramid] Recreate CWSession entity when eid no longer exists
As noted in comment, it might occur that we receive a "sessioneid" from
a valid cookie while respective CWSession got dropped (typical case is
db being recreated while users got preserved). In such case, we just
recreate the CWSession entity as if it did not exist.
--- a/cubicweb/pyramid/session.py Thu Feb 22 16:22:55 2018 +0100
+++ b/cubicweb/pyramid/session.py Mon Mar 05 10:32:58 2018 +0100
@@ -91,7 +91,10 @@
from pyramid.compat import pickle
from pyramid.session import SignedCookieSessionFactory
-from cubicweb import Binary
+from cubicweb import (
+ Binary,
+ UnknownEid,
+)
log = logging.getLogger(__name__)
@@ -228,8 +231,16 @@
'CWSession', cwsessiondata=data)
sessioneid = session.eid
else:
- session = cnx.entity_from_eid(sessioneid)
- session.cw_set(cwsessiondata=data)
+ try:
+ session = cnx.entity_from_eid(sessioneid)
+ except UnknownEid:
+ # Might occur if CWSession entity got dropped (e.g.
+ # the whole db got recreated) while user's cookie is
+ # still valid. We recreate the CWSession in this case.
+ sessioneid = cnx.create_entity(
+ 'CWSession', cwsessiondata=data).eid
+ else:
+ session.cw_set(cwsessiondata=data)
cnx.commit()
# Only if needed actually set the cookie