entities/authobjs.py
brancholdstable
changeset 4985 02b52bf9f5f8
parent 4436 294e084f1263
child 4835 13b0b96d7982
equal deleted inserted replaced
4563:c25da7573ebd 4985:02b52bf9f5f8
    11 
    11 
    12 from cubicweb import Unauthorized
    12 from cubicweb import Unauthorized
    13 from cubicweb.entities import AnyEntity, fetch_config
    13 from cubicweb.entities import AnyEntity, fetch_config
    14 
    14 
    15 class CWGroup(AnyEntity):
    15 class CWGroup(AnyEntity):
    16     id = 'CWGroup'
    16     __regid__ = 'CWGroup'
    17     fetch_attrs, fetch_order = fetch_config(['name'])
    17     fetch_attrs, fetch_order = fetch_config(['name'])
    18     fetch_unrelated_order = fetch_order
    18     fetch_unrelated_order = fetch_order
    19 
    19 
    20     def db_key_name(self):
    20     def db_key_name(self):
    21         """XXX goa specific"""
    21         """XXX goa specific"""
    22         return self.get('name')
    22         return self.get('name')
    23 
    23 
    24 
    24 
    25 class CWUser(AnyEntity):
    25 class CWUser(AnyEntity):
    26     id = 'CWUser'
    26     __regid__ = 'CWUser'
    27     fetch_attrs, fetch_order = fetch_config(['login', 'firstname', 'surname'])
    27     fetch_attrs, fetch_order = fetch_config(['login', 'firstname', 'surname'])
    28     fetch_unrelated_order = fetch_order
    28     fetch_unrelated_order = fetch_order
    29 
    29 
    30     # used by repository to check if  the user can log in or not
    30     # used by repository to check if  the user can log in or not
    31     AUTHENTICABLE_STATES = ('activated',)
    31     AUTHENTICABLE_STATES = ('activated',)
    58 
    58 
    59     def property_value(self, key):
    59     def property_value(self, key):
    60         try:
    60         try:
    61             # properties stored on the user aren't correctly typed
    61             # properties stored on the user aren't correctly typed
    62             # (e.g. all values are unicode string)
    62             # (e.g. all values are unicode string)
    63             return self.vreg.typed_value(key, self.properties[key])
    63             return self._cw.vreg.typed_value(key, self.properties[key])
    64         except KeyError:
    64         except KeyError:
    65             pass
    65             pass
    66         except ValueError:
    66         except ValueError:
    67             self.warning('incorrect value for eproperty %s of user %s', key, self.login)
    67             self.warning('incorrect value for eproperty %s of user %s',
    68         return self.vreg.property_value(key)
    68                          key, self.login)
       
    69         return self._cw.vreg.property_value(key)
    69 
    70 
    70     def matching_groups(self, groups):
    71     def matching_groups(self, groups):
    71         """return the number of the given group(s) in which the user is
    72         """return the number of the given group(s) in which the user is
    72 
    73 
    73         :type groups: str or iterable(str)
    74         :type groups: str or iterable(str)
    90         # to config['anonymous-user'], we don't have this info on
    91         # to config['anonymous-user'], we don't have this info on
    91         # the server side.
    92         # the server side.
    92         return self.groups == frozenset(('guests', ))
    93         return self.groups == frozenset(('guests', ))
    93 
    94 
    94     def owns(self, eid):
    95     def owns(self, eid):
    95         if hasattr(self.req, 'unsafe_execute'):
    96         if hasattr(self._cw, 'unsafe_execute'):
    96             # use unsafe_execute on the repository side, in case
    97             # use unsafe_execute on the repository side, in case
    97             # session's user doesn't have access to CWUser
    98             # session's user doesn't have access to CWUser
    98             execute = self.req.unsafe_execute
    99             execute = self._cw.unsafe_execute
    99         else:
   100         else:
   100             execute = self.req.execute
   101             execute = self._cw.execute
   101         try:
   102         try:
   102             return execute('Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s',
   103             return execute('Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s',
   103                            {'x': eid, 'u': self.eid}, 'x')
   104                            {'x': eid, 'u': self.eid}, 'x')
   104         except Unauthorized:
   105         except Unauthorized:
   105             return False
   106             return False
   113         if contexteid is not None:
   114         if contexteid is not None:
   114             rql += ', X require_permission P, X eid %(x)s'
   115             rql += ', X require_permission P, X eid %(x)s'
   115             kwargs['x'] = contexteid
   116             kwargs['x'] = contexteid
   116             cachekey = 'x'
   117             cachekey = 'x'
   117         try:
   118         try:
   118             return self.req.execute(rql, kwargs, cachekey)
   119             return self._cw.execute(rql, kwargs, cachekey)
   119         except Unauthorized:
   120         except Unauthorized:
   120             return False
   121             return False
   121 
   122 
   122     # presentation utilities ##################################################
   123     # presentation utilities ##################################################
   123 
   124 
   124     def name(self):
   125     def name(self):
   125         """construct a name using firstname / surname or login if not defined"""
   126         """construct a name using firstname / surname or login if not defined"""
   126 
   127 
   127         if self.firstname and self.surname:
   128         if self.firstname and self.surname:
   128             return self.req._('%(firstname)s %(surname)s') % {
   129             return self._cw._('%(firstname)s %(surname)s') % {
   129                 'firstname': self.firstname, 'surname' : self.surname}
   130                 'firstname': self.firstname, 'surname' : self.surname}
   130         if self.firstname:
   131         if self.firstname:
   131             return self.firstname
   132             return self.firstname
   132         return self.login
   133         return self.login
   133 
   134