server/session.py
changeset 9026 84c5b01ebb3e
parent 9025 768eb9a6a2db
child 9027 b6b96d61e055
equal deleted inserted replaced
9025:768eb9a6a2db 9026:84c5b01ebb3e
   112 
   112 
   113        with _hooks_control(cnx, HOOKS_DENY_ALL, 'integrity'):
   113        with _hooks_control(cnx, HOOKS_DENY_ALL, 'integrity'):
   114            # ... do stuff with none but 'integrity' hooks activated
   114            # ... do stuff with none but 'integrity' hooks activated
   115 
   115 
   116     This is an internal api, you should rather use
   116     This is an internal api, you should rather use
   117     :meth:`~cubicweb.server.session.Session.deny_all_hooks_but` or
   117     :meth:`~cubicweb.server.session.Connection.deny_all_hooks_but` or
   118     :meth:`~cubicweb.server.session.Session.allow_all_hooks_but` session
   118     :meth:`~cubicweb.server.session.Connection.allow_all_hooks_but`
   119     methods.
   119     Transaction methods.
   120     """
   120     """
   121     def __init__(self, session, mode, *categories):
   121     def __init__(self, cnx, mode, *categories):
   122         assert mode in (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
   122         assert mode in (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
   123         self.session = session
   123         self.cnx = cnx
   124         self.cnx = session._cnx
       
   125         self.mode = mode
   124         self.mode = mode
   126         self.categories = categories
   125         self.categories = categories
   127         self.oldmode = None
   126         self.oldmode = None
   128         self.changes = ()
   127         self.changes = ()
   129 
   128 
   136             self.changes = self.cnx.disable_hook_categories(*self.categories)
   135             self.changes = self.cnx.disable_hook_categories(*self.categories)
   137         self.cnx.ctx_count += 1
   136         self.cnx.ctx_count += 1
   138 
   137 
   139     def __exit__(self, exctype, exc, traceback):
   138     def __exit__(self, exctype, exc, traceback):
   140         self.cnx.ctx_count -= 1
   139         self.cnx.ctx_count -= 1
       
   140         try:
       
   141             if self.categories:
       
   142                 if self.mode is HOOKS_DENY_ALL:
       
   143                     self.cnx.disable_hook_categories(*self.categories)
       
   144                 else:
       
   145                     self.cnx.enable_hook_categories(*self.categories)
       
   146         finally:
       
   147             self.cnx.hooks_mode = self.oldmode
       
   148 
       
   149 class _session_hooks_control(_hooks_control):
       
   150     """hook control context manager for session
       
   151 
       
   152     Necessary To handle some unholy transaction scope logic."""
       
   153 
       
   154 
       
   155     def __init__(self, session, mode, *categories):
       
   156         self.session = session
       
   157         super_init = super(_session_hooks_control, self).__init__
       
   158         return super_init(session._cnx, mode, *categories)
       
   159 
       
   160     def __exit__(self, exctype, exc, traceback):
       
   161         super_exit = super(_session_hooks_control, self).__exit__
       
   162         ret = super_exit(exctype, exc, traceback)
   141         if self.cnx.ctx_count == 0:
   163         if self.cnx.ctx_count == 0:
   142             self.session._clear_thread_storage(self.cnx)
   164             self.session._clear_thread_storage(self.cnx)
   143         else:
   165         return ret
   144             try:
       
   145                 if self.categories:
       
   146                     if self.mode is HOOKS_DENY_ALL:
       
   147                         self.cnx.disable_hook_categories(*self.categories)
       
   148                     else:
       
   149                         self.cnx.enable_hook_categories(*self.categories)
       
   150             finally:
       
   151                 self.cnx.hooks_mode = self.oldmode
       
   152 
   166 
   153 @deprecated('[3.17] use <object>.security_enabled instead')
   167 @deprecated('[3.17] use <object>.security_enabled instead')
   154 def security_enabled(obj, *args, **kwargs):
   168 def security_enabled(obj, *args, **kwargs):
   155     return obj.security_enabled(*args, **kwargs)
   169     return obj.security_enabled(*args, **kwargs)
   156 
   170 
   550             self.pending_operations.append(operation)
   564             self.pending_operations.append(operation)
   551         else:
   565         else:
   552             self.pending_operations.insert(index, operation)
   566             self.pending_operations.insert(index, operation)
   553 
   567 
   554     # Hooks control ###########################################################
   568     # Hooks control ###########################################################
       
   569 
       
   570     def allow_all_hooks_but(self, *categories):
       
   571         return _hooks_control(self, HOOKS_ALLOW_ALL, *categories)
       
   572 
       
   573     def deny_all_hooks_but(self, *categories):
       
   574         return _hooks_control(self, HOOKS_DENY_ALL, *categories)
   555 
   575 
   556     def disable_hook_categories(self, *categories):
   576     def disable_hook_categories(self, *categories):
   557         """disable the given hook categories:
   577         """disable the given hook categories:
   558 
   578 
   559         - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
   579         - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
  1046 
  1066 
  1047     # hooks activation control #################################################
  1067     # hooks activation control #################################################
  1048     # all hooks should be activated during normal execution
  1068     # all hooks should be activated during normal execution
  1049 
  1069 
  1050     def allow_all_hooks_but(self, *categories):
  1070     def allow_all_hooks_but(self, *categories):
  1051         return _hooks_control(self, HOOKS_ALLOW_ALL, *categories)
  1071         return _session_hooks_control(self, HOOKS_ALLOW_ALL, *categories)
  1052     def deny_all_hooks_but(self, *categories):
  1072     def deny_all_hooks_but(self, *categories):
  1053         return _hooks_control(self, HOOKS_DENY_ALL, *categories)
  1073         return _session_hooks_control(self, HOOKS_DENY_ALL, *categories)
  1054 
  1074 
  1055     hooks_mode = cnx_attr('hooks_mode')
  1075     hooks_mode = cnx_attr('hooks_mode')
  1056 
  1076 
  1057     disabled_hook_categories = cnx_attr('disabled_hook_cats')
  1077     disabled_hook_categories = cnx_attr('disabled_hook_cats')
  1058     enabled_hook_categories = cnx_attr('enabled_hook_cats')
  1078     enabled_hook_categories = cnx_attr('enabled_hook_cats')