entities/authobjs.py
branchstable
changeset 7780 a1d5365fefc1
parent 7371 233b761201e4
child 7782 40a49f4350a5
equal deleted inserted replaced
7779:3826d8480a68 7780:a1d5365fefc1
    26 
    26 
    27 class CWGroup(AnyEntity):
    27 class CWGroup(AnyEntity):
    28     __regid__ = 'CWGroup'
    28     __regid__ = 'CWGroup'
    29     fetch_attrs, fetch_order = fetch_config(['name'])
    29     fetch_attrs, fetch_order = fetch_config(['name'])
    30     fetch_unrelated_order = fetch_order
    30     fetch_unrelated_order = fetch_order
    31 
       
    32     def grant_permission(self, entity, pname, plabel=None):
       
    33         """grant local `pname` permission on `entity` to this group using
       
    34         :class:`CWPermission`.
       
    35 
       
    36         If a similar permission already exists, add the group to it, else create
       
    37         a new one.
       
    38         """
       
    39         if not self._cw.execute(
       
    40             'SET X require_group G WHERE E eid %(e)s, G eid %(g)s, '
       
    41             'E require_permission X, X name %(name)s, X label %(label)s',
       
    42             {'e': entity.eid, 'g': self.eid,
       
    43              'name': pname, 'label': plabel}):
       
    44             self._cw.create_entity('CWPermission', name=pname, label=plabel,
       
    45                                    require_group=self,
       
    46                                    reverse_require_permission=entity)
       
    47 
    31 
    48 
    32 
    49 class CWUser(AnyEntity):
    33 class CWUser(AnyEntity):
    50     __regid__ = 'CWUser'
    34     __regid__ = 'CWUser'
    51     fetch_attrs, fetch_order = fetch_config(['login', 'firstname', 'surname'])
    35     fetch_attrs, fetch_order = fetch_config(['login', 'firstname', 'surname'])
   137                 {'x': eid, 'u': self.eid})
   121                 {'x': eid, 'u': self.eid})
   138         except Unauthorized:
   122         except Unauthorized:
   139             return False
   123             return False
   140     owns = cached(owns, keyarg=1)
   124     owns = cached(owns, keyarg=1)
   141 
   125 
   142     def has_permission(self, pname, contexteid=None):
       
   143         rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\
       
   144               'P name %(pname)s, P require_group G'
       
   145         kwargs = {'pname': pname, 'u': self.eid}
       
   146         if contexteid is not None:
       
   147             rql += ', X require_permission P, X eid %(x)s'
       
   148             kwargs['x'] = contexteid
       
   149         try:
       
   150             return self._cw.execute(rql, kwargs)
       
   151         except Unauthorized:
       
   152             return False
       
   153 
       
   154     # presentation utilities ##################################################
   126     # presentation utilities ##################################################
   155 
   127 
   156     def name(self):
   128     def name(self):
   157         """construct a name using firstname / surname or login if not defined"""
   129         """construct a name using firstname / surname or login if not defined"""
   158 
   130