cubicweb/pyramid/session.py
author Laurent Peuch <cortex@worlddomination.be>
Wed, 08 May 2019 21:30:44 +0200
changeset 12589 85f82900f1c3
parent 12588 fa292e905edc
child 12592 7ccf23523670
permissions -rw-r--r--
DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities . See "Upcoming Changes to ISession in Pyramid 2.0" for more information about this change. As describe here https://docs.pylonsproject.org/projects/pyramid/en/1.10-branch/narr/sessions.html#pickle-session-deprecation use a serializer that fallback on pickle if needed to avoid impacting users.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
12108
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    20
"""
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    21
Web session when using pyramid
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    22
------------------------------
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    23
12108
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    24
CubicWeb ``CWSession`` entity type so that sessions can be
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    25
stored in the database, which allows to run a Cubicweb instance
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    26
without having to set up a session storage (like redis or memcache)
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    27
solution.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    28
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    29
However, for production systems, it is greatly advised to use such a
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    30
storage solution for the sessions.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    31
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    32
The handling of the sessions is made by pyramid (see the
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    33
`pyramid's documentation on sessions`_ for more details).
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    34
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    35
For example, to set up a redis based session storage, you need the
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    36
`pyramid-redis-session`_ package, then you must configure pyramid to
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    37
use this backend, by configuring the pyramid configuration file:
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    38
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    39
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    40
.. code-block:: ini
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    41
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    42
   [main]
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    43
   cubicweb.defaults = no # we do not want to load the default cw session handling
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    44
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    45
   cubicweb.auth.authtkt.session.secret = <secret1>
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    46
   cubicweb.auth.authtkt.persistent.secret = <secret2>
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    47
   cubicweb.auth.authtkt.session.secure = yes
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    48
   cubicweb.auth.authtkt.persistent.secure = yes
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    49
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    50
   redis.sessions.secret = <secret3>
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    51
   redis.sessions.prefix = <my-app>:
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    52
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    53
   redis.sessions.url = redis://localhost:6379/0
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    54
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    55
   pyramid.includes =
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    56
           pyramid_redis_sessions
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    57
           cubicweb.pyramid.auth
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    58
           cubicweb.pyramid.login
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    59
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    60
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    61
.. Warning:: If you want to be able to log in a CubicWeb application
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    62
             served by pyramid on a unsecured stream (typically when
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    63
             you start an instance in dev mode using a simple
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    64
             ``cubicweb-ctl pyramid -D -linfo myinstance``), you
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    65
             **must** set ``cubicweb.auth.authtkt.session.secure`` to
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    66
             ``no``.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    67
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    68
Secrets
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    69
~~~~~~~
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    70
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    71
There are a number of secrets to configure in ``pyramid.ini``. They
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    72
should be different one from each other, as explained in `Pyramid's
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    73
documentation`_.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    74
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    75
For the record, regarding session handling:
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    76
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    77
:cubicweb.session.secret: This secret is used to encrypt the session's
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    78
   data ID (data themselved are stored in the backend, database or
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    79
   redis) when using the integrated (``CWSession`` based) session data
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    80
   storage.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    81
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    82
:redis.session.secret: This secret is used to encrypt the session's
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    83
   data ID (data themselved are stored in the backend, database or
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    84
   redis) when using redis as backend.
1a5fc93c81db [doc] Move content of former README.pyramid into relevant modules
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11993
diff changeset
    85
"""
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11896
diff changeset
    86
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    87
import warnings
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    88
import logging
11629
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
    89
from contextlib import contextmanager
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    91
from pyramid.compat import pickle
12589
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
    92
from pyramid.session import SignedCookieSessionFactory, JSONSerializer, PickleSerializer
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    93
12278
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
    94
from cubicweb import (
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
    95
    Binary,
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
    96
    UnknownEid,
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
    97
)
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    98
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    99
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   100
log = logging.getLogger(__name__)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   101
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   102
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   103
def logerrors(logger):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   104
    def wrap(fn):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   105
        def newfn(*args, **kw):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   106
            try:
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   107
                return fn(*args, **kw)
12225
a8ed10f80a85 [pyramid] Get rid of a couple of bare "except"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12108
diff changeset
   108
            except Exception:
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   109
                logger.exception("Error in %s" % fn.__name__)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   110
        return newfn
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   111
    return wrap
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   112
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   113
11629
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   114
@contextmanager
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   115
def unsafe_cnx_context_manager(request):
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   116
    """Return a connection for use as a context manager, with security disabled
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   117
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   118
    If request has an attached connection, its security will be deactived in the context manager's
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   119
    scope, else a new internal connection is returned.
11896
327585fd7670 [pyramid] Don't use unsafe_cnx_context_manager for write queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
   120
327585fd7670 [pyramid] Don't use unsafe_cnx_context_manager for write queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
   121
    This should be used for read-only queries, not if you intend to commit/rollback some data.
11629
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   122
    """
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   123
    cnx = request.cw_cnx
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   124
    if cnx is None:
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   125
        with request.registry['cubicweb.repository'].internal_cnx() as cnx:
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   126
            yield cnx
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   127
    else:
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   128
        with cnx.security_enabled(read=False, write=False):
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   129
            yield cnx
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   130
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   131
12589
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   132
class JSONSerializerWithPickleFallback(object):
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   133
    def __init__(self):
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   134
        self.json = JSONSerializer()
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   135
        self.pickle = PickleSerializer()
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   136
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   137
    def dumps(self, value):
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   138
        # maybe catch serialization errors here and keep using pickle
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   139
        # while finding spots in your app that are not storing
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   140
        # JSON-serializable objects, falling back to pickle
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   141
        return self.json.dumps(value)
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   142
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   143
    def loads(self, value):
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   144
        try:
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   145
            return self.json.loads(value)
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   146
        except ValueError:
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   147
            return self.pickle.loads(value)
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   148
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   149
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   150
def CWSessionFactory(
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   151
        secret,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   152
        cookie_name='session',
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   153
        max_age=None,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   154
        path='/',
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   155
        domain=None,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   156
        secure=False,
11579
78ba3e88a549 set httponly on session cookie
Julien Cristau <julien.cristau@logilab.fr>
parents: 11537
diff changeset
   157
        httponly=True,
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   158
        set_on_exception=True,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   159
        timeout=1200,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   160
        reissue_time=120,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   161
        hashalg='sha512',
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   162
        salt='pyramid.session.',
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   163
        serializer=None):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   164
    """ A pyramid session factory that store session data in the CubicWeb
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   165
    database.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   166
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   167
    Storage is done with the 'CWSession' entity, which is provided by the
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   168
    'pyramid' cube.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   169
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   170
    .. warning::
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   171
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   172
        Although it provides a sane default behavior, this session storage has
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   173
        a serious overhead because it uses RQL to access the database.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   174
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   175
        Using pure SQL would improve a bit (it is roughly twice faster), but it
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   176
        is still pretty slow and thus not an immediate priority.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   177
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   178
        It is recommended to use faster session factory
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   179
        (pyramid_redis_sessions_ for example) if you need speed.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   180
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   181
    .. _pyramid_redis_sessions: http://pyramid-redis-sessions.readthedocs.org/
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   182
                                en/latest/index.html
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   183
    """
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   184
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   185
    SignedCookieSession = SignedCookieSessionFactory(
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   186
        secret,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   187
        cookie_name=cookie_name,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   188
        max_age=max_age,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   189
        path=path,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   190
        domain=domain,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   191
        secure=secure,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   192
        httponly=httponly,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   193
        set_on_exception=set_on_exception,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   194
        timeout=timeout,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   195
        reissue_time=reissue_time,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   196
        hashalg=hashalg,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   197
        salt=salt,
12589
85f82900f1c3 DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
Laurent Peuch <cortex@worlddomination.be>
parents: 12588
diff changeset
   198
        serializer=serializer if serializer else JSONSerializerWithPickleFallback())
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   199
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   200
    class CWSession(SignedCookieSession):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   201
        def __init__(self, request):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   202
            # _set_accessed will be called by the super __init__.
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   203
            # Setting _loaded to True inhibates it.
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   204
            self._loaded = True
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   205
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   206
            # the super __init__ will load a single value in the dictionnary,
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   207
            # the session id.
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   208
            super(CWSession, self).__init__(request)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   209
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   210
            # Remove the session id from the dict
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   211
            self.sessioneid = self.pop('sessioneid', None)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   212
            self.repo = request.registry['cubicweb.repository']
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   213
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   214
            # We need to lazy-load only for existing sessions
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   215
            self._loaded = self.sessioneid is None
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   216
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   217
        @logerrors(log)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   218
        def _set_accessed(self, value):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   219
            self._accessed = value
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   220
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   221
            if self._loaded:
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   222
                return
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   223
11629
0459094d9728 Use opened connections as much as possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11625
diff changeset
   224
            with unsafe_cnx_context_manager(self.request) as cnx:
11625
b23d60a9ea84 retrieve session data in a single query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11606
diff changeset
   225
                value_rset = cnx.execute('Any D WHERE X eid %(x)s, X cwsessiondata D',
b23d60a9ea84 retrieve session data in a single query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11606
diff changeset
   226
                                         {'x': self.sessioneid})
b23d60a9ea84 retrieve session data in a single query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11606
diff changeset
   227
                value = value_rset[0][0]
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   228
                if value:
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   229
                    # Use directly dict.update to avoir _set_accessed to be
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   230
                    # recursively called
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   231
                    dict.update(self, pickle.load(value))
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   232
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   233
            self._loaded = True
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   234
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   235
        def _get_accessed(self):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   236
            return self._accessed
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   237
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   238
        accessed = property(_get_accessed, _set_accessed)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   239
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   240
        @logerrors(log)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   241
        def _set_cookie(self, response):
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   242
            # Save the value in the database
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   243
            data = Binary(pickle.dumps(dict(self)))
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   244
            sessioneid = self.sessioneid
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   245
11896
327585fd7670 [pyramid] Don't use unsafe_cnx_context_manager for write queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
   246
            with self.request.registry['cubicweb.repository'].internal_cnx() as cnx:
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   247
                if not sessioneid:
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   248
                    session = cnx.create_entity(
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   249
                        'CWSession', cwsessiondata=data)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   250
                    sessioneid = session.eid
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   251
                else:
12278
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   252
                    try:
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   253
                        session = cnx.entity_from_eid(sessioneid)
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   254
                    except UnknownEid:
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   255
                        # Might occur if CWSession entity got dropped (e.g.
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   256
                        # the whole db got recreated) while user's cookie is
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   257
                        # still valid. We recreate the CWSession in this case.
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   258
                        sessioneid = cnx.create_entity(
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   259
                            'CWSession', cwsessiondata=data).eid
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   260
                    else:
77a543e7878a [pyramid] Recreate CWSession entity when eid no longer exists
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12243
diff changeset
   261
                        session.cw_set(cwsessiondata=data)
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   262
                cnx.commit()
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   263
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   264
            # Only if needed actually set the cookie
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   265
            if self.new or self.accessed - self.renewed > self._reissue_time:
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   266
                dict.clear(self)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   267
                dict.__setitem__(self, 'sessioneid', sessioneid)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   268
                return super(CWSession, self)._set_cookie(response)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   269
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   270
            return True
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   271
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   272
    return CWSession
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   273
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   274
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   275
def includeme(config):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   276
    """ Activate the CubicWeb session factory.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   277
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11629
diff changeset
   278
    Usually called via ``config.include('cubicweb.pyramid.auth')``.
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   279
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   280
    See also :ref:`defaults_module`
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11506
diff changeset
   281
    """
12588
fa292e905edc [pyramid/enh] generate pyramid.ini "create" and on "pyramid" command if needed
Laurent Peuch <cortex@worlddomination.be>
parents: 12278
diff changeset
   282
    secret = config.registry.settings['cubicweb.session.secret']
11506
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   283
    session_factory = CWSessionFactory(secret)
bfc1aa1dba30 DB-saved session data
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   284
    config.set_session_factory(session_factory)