author | sylvain.thenault@logilab.fr |
Tue, 05 May 2009 19:29:59 +0200 | |
branch | tls-sprint |
changeset 1696 | ee0bea49e0e1 |
parent 1553 | 3f91ef2397d0 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
583
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
1 |
"""entity classes user and group entities |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
2 |
|
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
3 |
:organization: Logilab |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
6 |
""" |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
7 |
__docformat__ = "restructuredtext en" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
8 |
|
0 | 9 |
from logilab.common.decorators import cached |
10 |
||
11 |
from cubicweb import Unauthorized |
|
12 |
from cubicweb.entities import AnyEntity, fetch_config |
|
13 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
14 |
class CWGroup(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
15 |
id = 'CWGroup' |
0 | 16 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1343
659d3dc42e68
sort on EUser.login/EGroup.name in vocabulary
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
17 |
fetch_unrelated_order = fetch_order |
0 | 18 |
|
19 |
def db_key_name(self): |
|
20 |
"""XXX goa specific""" |
|
21 |
return self.get('name') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
22 |
|
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
23 |
class CWUser(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
24 |
id = 'CWUser' |
0 | 25 |
fetch_attrs, fetch_order = fetch_config(['login', 'firstname', 'surname']) |
1343
659d3dc42e68
sort on EUser.login/EGroup.name in vocabulary
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
26 |
fetch_unrelated_order = fetch_order |
1553 | 27 |
|
0 | 28 |
# used by repository to check if the user can log in or not |
29 |
AUTHENTICABLE_STATES = ('activated',) |
|
30 |
||
31 |
# low level utilities ##################################################### |
|
32 |
def __init__(self, *args, **kwargs): |
|
33 |
groups = kwargs.pop('groups', None) |
|
34 |
properties = kwargs.pop('properties', None) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
35 |
super(CWUser, self).__init__(*args, **kwargs) |
0 | 36 |
if groups is not None: |
37 |
self._groups = groups |
|
38 |
if properties is not None: |
|
39 |
self._properties = properties |
|
1553 | 40 |
|
0 | 41 |
@property |
42 |
def groups(self): |
|
43 |
try: |
|
44 |
return self._groups |
|
45 |
except AttributeError: |
|
46 |
self._groups = set(g.name for g in self.in_group) |
|
47 |
return self._groups |
|
1553 | 48 |
|
0 | 49 |
@property |
50 |
def properties(self): |
|
51 |
try: |
|
52 |
return self._properties |
|
53 |
except AttributeError: |
|
54 |
self._properties = dict((p.pkey, p.value) for p in self.reverse_for_user) |
|
55 |
return self._properties |
|
56 |
||
57 |
def property_value(self, key): |
|
58 |
try: |
|
59 |
# properties stored on the user aren't correctly typed |
|
60 |
# (e.g. all values are unicode string) |
|
61 |
return self.vreg.typed_value(key, self.properties[key]) |
|
62 |
except KeyError: |
|
63 |
pass |
|
64 |
except ValueError: |
|
65 |
self.warning('incorrect value for eproperty %s of user %s', key, self.login) |
|
66 |
return self.vreg.property_value(key) |
|
1553 | 67 |
|
0 | 68 |
def matching_groups(self, groups): |
69 |
"""return the number of the given group(s) in which the user is |
|
70 |
||
71 |
:type groups: str or iterable(str) |
|
72 |
:param groups: a group name or an iterable on group names |
|
73 |
""" |
|
74 |
if isinstance(groups, basestring): |
|
75 |
groups = frozenset((groups,)) |
|
76 |
elif isinstance(groups, (tuple, list)): |
|
77 |
groups = frozenset(groups) |
|
78 |
return len(groups & self.groups) |
|
79 |
||
80 |
def is_in_group(self, group): |
|
81 |
"""convience / shortcut method to test if the user belongs to `group` |
|
82 |
""" |
|
83 |
return self.matching_groups(group) == 1 |
|
84 |
||
590 | 85 |
def is_anonymous(self): |
86 |
""" checks if user is an anonymous user""" |
|
87 |
#FIXME on the web-side anonymous user is detected according |
|
88 |
# to config['anonymous-user'], we don't have this info on |
|
1553 | 89 |
# the server side. |
590 | 90 |
return self.groups == frozenset(('guests', )) |
91 |
||
0 | 92 |
def owns(self, eid): |
93 |
if hasattr(self.req, 'unsafe_execute'): |
|
94 |
# use unsafe_execute on the repository side, in case |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
95 |
# session's user doesn't have access to CWUser |
0 | 96 |
execute = self.req.unsafe_execute |
97 |
else: |
|
98 |
execute = self.req.execute |
|
99 |
try: |
|
100 |
return execute('Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s', |
|
101 |
{'x': eid, 'u': self.eid}, 'x') |
|
102 |
except Unauthorized: |
|
103 |
return False |
|
104 |
owns = cached(owns, keyarg=1) |
|
105 |
||
106 |
def has_permission(self, pname, contexteid=None): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
107 |
rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\ |
0 | 108 |
'P name %(pname)s, P require_group G' |
109 |
kwargs = {'pname': pname, 'u': self.eid} |
|
110 |
cachekey = None |
|
111 |
if contexteid is not None: |
|
112 |
rql += ', X require_permission P, X eid %(x)s' |
|
113 |
kwargs['x'] = contexteid |
|
114 |
cachekey = 'x' |
|
115 |
try: |
|
116 |
return self.req.execute(rql, kwargs, cachekey) |
|
117 |
except Unauthorized: |
|
118 |
return False |
|
1553 | 119 |
|
0 | 120 |
# presentation utilities ################################################## |
1553 | 121 |
|
0 | 122 |
def name(self): |
123 |
"""construct a name using firstname / surname or login if not defined""" |
|
1553 | 124 |
|
0 | 125 |
if self.firstname and self.surname: |
126 |
return self.req._('%(firstname)s %(surname)s') % { |
|
127 |
'firstname': self.firstname, 'surname' : self.surname} |
|
128 |
if self.firstname: |
|
129 |
return self.firstname |
|
130 |
return self.login |
|
131 |
||
132 |
def dc_title(self): |
|
133 |
return self.login |
|
134 |
||
135 |
dc_long_title = name |
|
136 |
||
137 |
def db_key_name(self): |
|
138 |
"""XXX goa specific""" |
|
139 |
return self.get('login') |
|
140 |
||
141 |
from logilab.common.deprecation import class_renamed |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
142 |
EUser = class_renamed('EUser', CWUser) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
143 |
EGroup = class_renamed('EGroup', CWGroup) |