[cwpermission, cwgroup] convenience method on CWGroup to grant a local permission (ie CWPermission) on an entity stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 11 May 2011 17:06:41 +0200
branchstable
changeset 7371 233b761201e4
parent 7370 96923d75610b
child 7372 65bd75f16d65
[cwpermission, cwgroup] convenience method on CWGroup to grant a local permission (ie CWPermission) on an entity
entities/authobjs.py
--- a/entities/authobjs.py	Wed May 11 17:05:59 2011 +0200
+++ b/entities/authobjs.py	Wed May 11 17:06:41 2011 +0200
@@ -29,6 +29,21 @@
     fetch_attrs, fetch_order = fetch_config(['name'])
     fetch_unrelated_order = fetch_order
 
+    def grant_permission(self, entity, pname, plabel=None):
+        """grant local `pname` permission on `entity` to this group using
+        :class:`CWPermission`.
+
+        If a similar permission already exists, add the group to it, else create
+        a new one.
+        """
+        if not self._cw.execute(
+            'SET X require_group G WHERE E eid %(e)s, G eid %(g)s, '
+            'E require_permission X, X name %(name)s, X label %(label)s',
+            {'e': entity.eid, 'g': self.eid,
+             'name': pname, 'label': plabel}):
+            self._cw.create_entity('CWPermission', name=pname, label=plabel,
+                                   require_group=self,
+                                   reverse_require_permission=entity)
 
 
 class CWUser(AnyEntity):