server/session.py
changeset 8767 a75670ef2d87
parent 8766 db80ffb2f71c
child 8768 3d105e270abc
equal deleted inserted replaced
8766:db80ffb2f71c 8767:a75670ef2d87
   141     def __exit__(self, exctype, exc, traceback):
   141     def __exit__(self, exctype, exc, traceback):
   142         self.session.reset_security(self.oldread, self.oldwrite)
   142         self.session.reset_security(self.oldread, self.oldwrite)
   143 
   143 
   144 HOOKS_ALLOW_ALL = object()
   144 HOOKS_ALLOW_ALL = object()
   145 HOOKS_DENY_ALL = object()
   145 HOOKS_DENY_ALL = object()
       
   146 DEFAULT_SECURITY = object() # evaluated to true by design
   146 
   147 
   147 class Transaction(object):
   148 class Transaction(object):
   148     """Repository Transaction
   149     """Repository Transaction
   149 
   150 
   150     Holds all transaction related data
   151     Holds all transaction related data
   587         objtype = self.repo.type_and_source_from_eid(eidto, self)[0]
   588         objtype = self.repo.type_and_source_from_eid(eidto, self)[0]
   588         return self.vreg.schema.rschema(rtype).rdefs[(subjtype, objtype)]
   589         return self.vreg.schema.rschema(rtype).rdefs[(subjtype, objtype)]
   589 
   590 
   590     # security control #########################################################
   591     # security control #########################################################
   591 
   592 
   592     DEFAULT_SECURITY = object() # evaluated to true by design
       
   593 
   593 
   594     def security_enabled(self, read=None, write=None):
   594     def security_enabled(self, read=None, write=None):
   595         return security_enabled(self, read=read, write=write)
   595         return security_enabled(self, read=read, write=write)
   596 
   596 
   597     def init_security(self, read, write):
   597     def init_security(self, read, write):
   620     @property
   620     @property
   621     def read_security(self):
   621     def read_security(self):
   622         """return a boolean telling if read security is activated or not"""
   622         """return a boolean telling if read security is activated or not"""
   623         txstore = self._threaddata
   623         txstore = self._threaddata
   624         if txstore is None:
   624         if txstore is None:
   625             return self.DEFAULT_SECURITY
   625             return DEFAULT_SECURITY
   626         try:
   626         try:
   627             return txstore.read_security
   627             return txstore.read_security
   628         except AttributeError:
   628         except AttributeError:
   629             txstore.read_security = self.DEFAULT_SECURITY
   629             txstore.read_security = DEFAULT_SECURITY
   630             return txstore.read_security
   630             return txstore.read_security
   631 
   631 
   632     def set_read_security(self, activated):
   632     def set_read_security(self, activated):
   633         """[de]activate read security, returning the previous value set for
   633         """[de]activate read security, returning the previous value set for
   634         later restoration.
   634         later restoration.
   636         you should usually use the `security_enabled` context manager instead
   636         you should usually use the `security_enabled` context manager instead
   637         of this to change security settings.
   637         of this to change security settings.
   638         """
   638         """
   639         txstore = self._threaddata
   639         txstore = self._threaddata
   640         if txstore is None:
   640         if txstore is None:
   641             return self.DEFAULT_SECURITY
   641             return DEFAULT_SECURITY
   642         oldmode = getattr(txstore, 'read_security', self.DEFAULT_SECURITY)
   642         oldmode = getattr(txstore, 'read_security', DEFAULT_SECURITY)
   643         txstore.read_security = activated
   643         txstore.read_security = activated
   644         # dbapi_query used to detect hooks triggered by a 'dbapi' query (eg not
   644         # dbapi_query used to detect hooks triggered by a 'dbapi' query (eg not
   645         # issued on the session). This is tricky since we the execution model of
   645         # issued on the session). This is tricky since we the execution model of
   646         # a (write) user query is:
   646         # a (write) user query is:
   647         #
   647         #
   655         # so we can't rely on simply checking session.read_security, but
   655         # so we can't rely on simply checking session.read_security, but
   656         # recalling the first transition from DEFAULT_SECURITY to something
   656         # recalling the first transition from DEFAULT_SECURITY to something
   657         # else (False actually) is not perfect but should be enough
   657         # else (False actually) is not perfect but should be enough
   658         #
   658         #
   659         # also reset dbapi_query to true when we go back to DEFAULT_SECURITY
   659         # also reset dbapi_query to true when we go back to DEFAULT_SECURITY
   660         txstore.dbapi_query = (oldmode is self.DEFAULT_SECURITY
   660         txstore.dbapi_query = (oldmode is DEFAULT_SECURITY
   661                                or activated is self.DEFAULT_SECURITY)
   661                                or activated is DEFAULT_SECURITY)
   662         return oldmode
   662         return oldmode
   663 
   663 
   664     @property
   664     @property
   665     def write_security(self):
   665     def write_security(self):
   666         """return a boolean telling if write security is activated or not"""
   666         """return a boolean telling if write security is activated or not"""
   667         txstore = self._threaddata
   667         txstore = self._threaddata
   668         if txstore is None:
   668         if txstore is None:
   669             return self.DEFAULT_SECURITY
   669             return DEFAULT_SECURITY
   670         try:
   670         try:
   671             return txstore.write_security
   671             return txstore.write_security
   672         except AttributeError:
   672         except AttributeError:
   673             txstore.write_security = self.DEFAULT_SECURITY
   673             txstore.write_security = DEFAULT_SECURITY
   674             return txstore.write_security
   674             return txstore.write_security
   675 
   675 
   676     def set_write_security(self, activated):
   676     def set_write_security(self, activated):
   677         """[de]activate write security, returning the previous value set for
   677         """[de]activate write security, returning the previous value set for
   678         later restoration.
   678         later restoration.
   680         you should usually use the `security_enabled` context manager instead
   680         you should usually use the `security_enabled` context manager instead
   681         of this to change security settings.
   681         of this to change security settings.
   682         """
   682         """
   683         txstore = self._threaddata
   683         txstore = self._threaddata
   684         if txstore is None:
   684         if txstore is None:
   685             return self.DEFAULT_SECURITY
   685             return DEFAULT_SECURITY
   686         oldmode = getattr(txstore, 'write_security', self.DEFAULT_SECURITY)
   686         oldmode = getattr(txstore, 'write_security', DEFAULT_SECURITY)
   687         txstore.write_security = activated
   687         txstore.write_security = activated
   688         return oldmode
   688         return oldmode
   689 
   689 
   690     @property
   690     @property
   691     def running_dbapi_query(self):
   691     def running_dbapi_query(self):
  1269     # only defining here to prevent pylint from complaining
  1269     # only defining here to prevent pylint from complaining
  1270     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
  1270     info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
  1271 
  1271 
  1272 Session.HOOKS_ALLOW_ALL = HOOKS_ALLOW_ALL
  1272 Session.HOOKS_ALLOW_ALL = HOOKS_ALLOW_ALL
  1273 Session.HOOKS_DENY_ALL = HOOKS_DENY_ALL
  1273 Session.HOOKS_DENY_ALL = HOOKS_DENY_ALL
       
  1274 Session.DEFAULT_SECURITY = DEFAULT_SECURITY
  1274 
  1275 
  1275 
  1276 
  1276 
  1277 
  1277 class InternalSession(Session):
  1278 class InternalSession(Session):
  1278     """special session created internaly by the repository"""
  1279     """special session created internaly by the repository"""