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