cubicweb/hooks/syncsession.py
changeset 11700 41ddaf6802f0
parent 11699 b48020a80dc3
child 11765 9cb215e833b0
equal deleted inserted replaced
11699:b48020a80dc3 11700:41ddaf6802f0
    24 from cubicweb.predicates import is_instance
    24 from cubicweb.predicates import is_instance
    25 from cubicweb.server import hook
    25 from cubicweb.server import hook
    26 from cubicweb.entities.authobjs import user_session_cache_key
    26 from cubicweb.entities.authobjs import user_session_cache_key
    27 
    27 
    28 
    28 
    29 def get_user_sessions(repo, ueid):
    29 # take cnx and not repo because it's needed for other sessions implementation (e.g. pyramid)
    30     for session in repo._sessions.values():
    30 def get_user_sessions(cnx, ueid):
       
    31     for session in cnx.repo._sessions.values():
    31         if ueid == session.user.eid:
    32         if ueid == session.user.eid:
    32             yield session
    33             yield session
    33 
    34 
    34 
    35 
    35 class CachedValueMixin(object):
    36 class CachedValueMixin(object):
   105     def __call__(self):
   106     def __call__(self):
   106         if self.event == 'after_delete_relation':
   107         if self.event == 'after_delete_relation':
   107             opcls = _DeleteGroupOp
   108             opcls = _DeleteGroupOp
   108         else:
   109         else:
   109             opcls = _AddGroupOp
   110             opcls = _AddGroupOp
   110         for session in get_user_sessions(self._cw.repo, self.eidfrom):
   111         for session in get_user_sessions(self._cw, self.eidfrom):
   111             opcls(self._cw, session=session, group_eid=self.eidto)
   112             opcls(self._cw, session=session, group_eid=self.eidto)
   112 
   113 
   113 
   114 
   114 class _CloseSessionOp(hook.Operation):
   115 class _CloseSessionOp(hook.Operation):
   115     """Close user's session when it has been deleted"""
   116     """Close user's session when it has been deleted"""
   129     __regid__ = 'closession'
   130     __regid__ = 'closession'
   130     __select__ = SyncSessionHook.__select__ & is_instance('CWUser')
   131     __select__ = SyncSessionHook.__select__ & is_instance('CWUser')
   131     events = ('after_delete_entity',)
   132     events = ('after_delete_entity',)
   132 
   133 
   133     def __call__(self):
   134     def __call__(self):
   134         for session in get_user_sessions(self._cw.repo, self.entity.eid):
   135         for session in get_user_sessions(self._cw, self.entity.eid):
   135             _CloseSessionOp(self._cw, session=session)
   136             _CloseSessionOp(self._cw, session=session)
   136 
   137 
   137 
   138 
   138 # CWProperty hooks #############################################################
   139 # CWProperty hooks #############################################################
   139 
   140 
   226         except UnknownProperty:
   227         except UnknownProperty:
   227             return
   228             return
   228         except ValueError as ex:
   229         except ValueError as ex:
   229             raise validation_error(entity, {('value', 'subject'): str(ex)})
   230             raise validation_error(entity, {('value', 'subject'): str(ex)})
   230         if entity.for_user:
   231         if entity.for_user:
   231             for session in get_user_sessions(cnx.repo, entity.for_user[0].eid):
   232             for session in get_user_sessions(cnx, entity.for_user[0].eid):
   232                 _ChangeUserCWPropertyOp(cnx, session=session, key=key, value=value)
   233                 _ChangeUserCWPropertyOp(cnx, session=session, key=key, value=value)
   233         else:
   234         else:
   234             _ChangeSiteWideCWPropertyOp(cnx, cwprop=self.entity)
   235             _ChangeSiteWideCWPropertyOp(cnx, cwprop=self.entity)
   235 
   236 
   236 
   237 
   261         key, value = cnx.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V',
   262         key, value = cnx.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V',
   262                                  {'x': eidfrom})[0]
   263                                  {'x': eidfrom})[0]
   263         if cnx.vreg.property_info(key)['sitewide']:
   264         if cnx.vreg.property_info(key)['sitewide']:
   264             msg = _("site-wide property can't be set for user")
   265             msg = _("site-wide property can't be set for user")
   265             raise validation_error(eidfrom, {('for_user', 'subject'): msg})
   266             raise validation_error(eidfrom, {('for_user', 'subject'): msg})
   266         for session in get_user_sessions(cnx.repo, self.eidto):
   267         for session in get_user_sessions(cnx, self.eidto):
   267             _ChangeUserCWPropertyOp(cnx, session=session, key=key, value=value)
   268             _ChangeUserCWPropertyOp(cnx, session=session, key=key, value=value)
   268 
   269 
   269 
   270 
   270 class DelForUserRelationHook(AddForUserRelationHook):
   271 class DelForUserRelationHook(AddForUserRelationHook):
   271     __regid__ = 'delcwpropforuser'
   272     __regid__ = 'delcwpropforuser'
   274     def __call__(self):
   275     def __call__(self):
   275         cnx = self._cw
   276         cnx = self._cw
   276         key = cnx.execute('Any K WHERE P eid %(x)s, P pkey K', {'x': self.eidfrom})[0][0]
   277         key = cnx.execute('Any K WHERE P eid %(x)s, P pkey K', {'x': self.eidfrom})[0][0]
   277         cnx.transaction_data.setdefault('pendingrelations', []).append(
   278         cnx.transaction_data.setdefault('pendingrelations', []).append(
   278             (self.eidfrom, self.rtype, self.eidto))
   279             (self.eidfrom, self.rtype, self.eidto))
   279         for session in get_user_sessions(cnx.repo, self.eidto):
   280         for session in get_user_sessions(cnx, self.eidto):
   280             _DelUserCWPropertyOp(cnx, session=session, key=key)
   281             _DelUserCWPropertyOp(cnx, session=session, key=key)