23 |
23 |
24 from logilab.common.decorators import cached |
24 from logilab.common.decorators import cached |
25 |
25 |
26 from cubicweb import Unauthorized |
26 from cubicweb import Unauthorized |
27 from cubicweb.entities import AnyEntity, fetch_config |
27 from cubicweb.entities import AnyEntity, fetch_config |
|
28 |
|
29 |
|
30 def user_session_cache_key(user_eid, data_name): |
|
31 return '{0}-{1}'.format(user_eid, data_name) |
|
32 |
28 |
33 |
29 class CWGroup(AnyEntity): |
34 class CWGroup(AnyEntity): |
30 __regid__ = 'CWGroup' |
35 __regid__ = 'CWGroup' |
31 fetch_attrs, cw_fetch_order = fetch_config(['name']) |
36 fetch_attrs, cw_fetch_order = fetch_config(['name']) |
32 cw_fetch_unrelated_order = cw_fetch_order |
37 cw_fetch_unrelated_order = cw_fetch_order |
52 |
57 |
53 # used by repository to check if the user can log in or not |
58 # used by repository to check if the user can log in or not |
54 AUTHENTICABLE_STATES = ('activated',) |
59 AUTHENTICABLE_STATES = ('activated',) |
55 |
60 |
56 # low level utilities ##################################################### |
61 # low level utilities ##################################################### |
57 def __init__(self, *args, **kwargs): |
|
58 groups = kwargs.pop('groups', None) |
|
59 properties = kwargs.pop('properties', None) |
|
60 super(CWUser, self).__init__(*args, **kwargs) |
|
61 if groups is not None: |
|
62 self._groups = groups |
|
63 if properties is not None: |
|
64 self._properties = properties |
|
65 |
62 |
66 @property |
63 @property |
67 def groups(self): |
64 def groups(self): |
|
65 key = user_session_cache_key(self.eid, 'groups') |
68 try: |
66 try: |
69 return self._groups |
67 return self._cw.data[key] |
70 except AttributeError: |
68 except KeyError: |
71 self._groups = set(g.name for g in self.in_group) |
69 with self._cw.security_enabled(read=False): |
72 return self._groups |
70 groups = set(group for group, in self._cw.execute( |
|
71 'Any GN WHERE U in_group G, G name GN, U eid %(userid)s', |
|
72 {'userid': self.eid})) |
|
73 self._cw.data[key] = groups |
|
74 return groups |
73 |
75 |
74 @property |
76 @property |
75 def properties(self): |
77 def properties(self): |
|
78 key = user_session_cache_key(self.eid, 'properties') |
76 try: |
79 try: |
77 return self._properties |
80 return self._cw.data[key] |
78 except AttributeError: |
81 except KeyError: |
79 self._properties = dict( |
82 with self._cw.security_enabled(read=False): |
80 self._cw.execute( |
83 properties = dict(self._cw.execute( |
81 'Any K, V WHERE P for_user U, U eid %(userid)s, ' |
84 'Any K, V WHERE P for_user U, U eid %(userid)s, ' |
82 'P pkey K, P value V', |
85 'P pkey K, P value V', {'userid': self.eid})) |
83 {'userid': self.eid})) |
86 self._cw.data[key] = properties |
84 return self._properties |
87 return properties |
85 |
88 |
86 def prefered_language(self, language=None): |
89 def prefered_language(self, language=None): |
87 """return language used by this user, if explicitly defined (eg not |
90 """return language used by this user, if explicitly defined (eg not |
88 using http negociation) |
91 using http negociation) |
89 """ |
92 """ |