server/session.py
changeset 10345 ef54ea75a642
parent 10301 729f36a1bcfa
child 10346 b926ff4ef4a8
equal deleted inserted replaced
10342:28db21e0c8e5 10345:ef54ea75a642
    32 
    32 
    33 from cubicweb import QueryError, schema, server, ProgrammingError
    33 from cubicweb import QueryError, schema, server, ProgrammingError
    34 from cubicweb.req import RequestSessionBase
    34 from cubicweb.req import RequestSessionBase
    35 from cubicweb.utils import make_uid
    35 from cubicweb.utils import make_uid
    36 from cubicweb.rqlrewrite import RQLRewriter
    36 from cubicweb.rqlrewrite import RQLRewriter
    37 from cubicweb.server import ShuttingDown
       
    38 from cubicweb.server.edition import EditedEntity
    37 from cubicweb.server.edition import EditedEntity
    39 
    38 
    40 
    39 
    41 NO_UNDO_TYPES = schema.SCHEMA_TYPES.copy()
    40 NO_UNDO_TYPES = schema.SCHEMA_TYPES.copy()
    42 NO_UNDO_TYPES.add('CWCache')
    41 NO_UNDO_TYPES.add('CWCache')
   481         #: CnxSetTracker used to report cnxset usage
   480         #: CnxSetTracker used to report cnxset usage
   482         self._cnxset_tracker = session._cnxset_tracker
   481         self._cnxset_tracker = session._cnxset_tracker
   483         #: is this connection from a client or internal to the repo
   482         #: is this connection from a client or internal to the repo
   484         self.running_dbapi_query = True
   483         self.running_dbapi_query = True
   485         # internal (root) session
   484         # internal (root) session
   486         self.is_internal_session = session.is_internal_session
   485         self.is_internal_session = isinstance(session.user, InternalManager)
   487 
   486 
   488         #: dict containing arbitrary data cleared at the end of the transaction
   487         #: dict containing arbitrary data cleared at the end of the transaction
   489         self.transaction_data = {}
   488         self.transaction_data = {}
   490         self._session_data = session.data
   489         self._session_data = session.data
   491         #: ordered list of operations to be processed on commit/rollback
   490         #: ordered list of operations to be processed on commit/rollback
   504         self._read_security = DEFAULT_SECURITY # handled by a property
   503         self._read_security = DEFAULT_SECURITY # handled by a property
   505         self.write_security = DEFAULT_SECURITY
   504         self.write_security = DEFAULT_SECURITY
   506 
   505 
   507         # undo control
   506         # undo control
   508         config = session.repo.config
   507         config = session.repo.config
   509         if config.creating or config.repairing or session.is_internal_session:
   508         if config.creating or config.repairing or self.is_internal_session:
   510             self.undo_actions = False
   509             self.undo_actions = False
   511         else:
   510         else:
   512             self.undo_actions = config['undo-enabled']
   511             self.undo_actions = config['undo-enabled']
   513 
   512 
   514         # RQLRewriter are not thread safe
   513         # RQLRewriter are not thread safe
  1338 
  1337 
  1339 
  1338 
  1340 
  1339 
  1341     """
  1340     """
  1342     is_request = False
  1341     is_request = False
  1343     is_internal_session = False
       
  1344 
  1342 
  1345     def __init__(self, user, repo, cnxprops=None, _id=None):
  1343     def __init__(self, user, repo, cnxprops=None, _id=None):
  1346         super(Session, self).__init__(repo.vreg)
  1344         super(Session, self).__init__(repo.vreg)
  1347         self.sessionid = _id or make_uid(unormalize(user.login).encode('UTF8'))
  1345         self.sessionid = _id or make_uid(unormalize(user.login).encode('UTF8'))
  1348         self.user = user # XXX repoapi: deprecated and store only a login.
  1346         self.user = user # XXX repoapi: deprecated and store only a login.
  1745 Session.HOOKS_ALLOW_ALL = HOOKS_ALLOW_ALL
  1743 Session.HOOKS_ALLOW_ALL = HOOKS_ALLOW_ALL
  1746 Session.HOOKS_DENY_ALL = HOOKS_DENY_ALL
  1744 Session.HOOKS_DENY_ALL = HOOKS_DENY_ALL
  1747 Session.DEFAULT_SECURITY = DEFAULT_SECURITY
  1745 Session.DEFAULT_SECURITY = DEFAULT_SECURITY
  1748 
  1746 
  1749 
  1747 
  1750 
       
  1751 class InternalSession(Session):
       
  1752     """special session created internally by the repository"""
       
  1753     is_internal_session = True
       
  1754     running_dbapi_query = False
       
  1755 
       
  1756     def __init__(self, repo, cnxprops=None, safe=False):
       
  1757         super(InternalSession, self).__init__(InternalManager(), repo, cnxprops,
       
  1758                                               _id='internal')
       
  1759         self.user._cw = self # XXX remove when "vreg = user._cw.vreg" hack in entity.py is gone
       
  1760 
       
  1761     def __enter__(self):
       
  1762         return self
       
  1763 
       
  1764     def __exit__(self, exctype, excvalue, tb):
       
  1765         self.close()
       
  1766 
       
  1767     @property
       
  1768     def cnxset(self):
       
  1769         """connections set, set according to transaction mode for each query"""
       
  1770         if self.repo.shutting_down:
       
  1771             self.free_cnxset(True)
       
  1772             raise ShuttingDown('repository is shutting down')
       
  1773         return self._cnx.cnxset
       
  1774 
       
  1775 
       
  1776 class InternalManager(object):
  1748 class InternalManager(object):
  1777     """a manager user with all access rights used internally for task such as
  1749     """a manager user with all access rights used internally for task such as
  1778     bootstrapping the repository or creating regular users according to
  1750     bootstrapping the repository or creating regular users according to
  1779     repository content
  1751     repository content
  1780     """
  1752     """
       
  1753 
  1781     def __init__(self, lang='en'):
  1754     def __init__(self, lang='en'):
  1782         self.eid = -1
  1755         self.eid = -1
  1783         self.login = u'__internal_manager__'
  1756         self.login = u'__internal_manager__'
  1784         self.properties = {}
  1757         self.properties = {}
  1785         self.groups = set(['managers'])
  1758         self.groups = set(['managers'])