author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 29 Jan 2010 16:54:49 +0100 | |
branch | stable |
changeset 4408 | 899b426087ab |
parent 4402 | 360c532070f5 |
child 4436 | 294e084f1263 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3813
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
|
4246
c95b8c7e5fb2
don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
24 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
25 |
class CWUser(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
26 |
id = 'CWUser' |
0 | 27 |
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
|
28 |
fetch_unrelated_order = fetch_order |
1553 | 29 |
|
0 | 30 |
# used by repository to check if the user can log in or not |
31 |
AUTHENTICABLE_STATES = ('activated',) |
|
32 |
||
33 |
# low level utilities ##################################################### |
|
34 |
def __init__(self, *args, **kwargs): |
|
35 |
groups = kwargs.pop('groups', None) |
|
36 |
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
|
37 |
super(CWUser, self).__init__(*args, **kwargs) |
0 | 38 |
if groups is not None: |
39 |
self._groups = groups |
|
40 |
if properties is not None: |
|
41 |
self._properties = properties |
|
1553 | 42 |
|
0 | 43 |
@property |
44 |
def groups(self): |
|
45 |
try: |
|
46 |
return self._groups |
|
47 |
except AttributeError: |
|
48 |
self._groups = set(g.name for g in self.in_group) |
|
49 |
return self._groups |
|
1553 | 50 |
|
0 | 51 |
@property |
52 |
def properties(self): |
|
53 |
try: |
|
54 |
return self._properties |
|
55 |
except AttributeError: |
|
56 |
self._properties = dict((p.pkey, p.value) for p in self.reverse_for_user) |
|
57 |
return self._properties |
|
58 |
||
59 |
def property_value(self, key): |
|
60 |
try: |
|
61 |
# properties stored on the user aren't correctly typed |
|
62 |
# (e.g. all values are unicode string) |
|
63 |
return self.vreg.typed_value(key, self.properties[key]) |
|
64 |
except KeyError: |
|
65 |
pass |
|
66 |
except ValueError: |
|
67 |
self.warning('incorrect value for eproperty %s of user %s', key, self.login) |
|
68 |
return self.vreg.property_value(key) |
|
1553 | 69 |
|
0 | 70 |
def matching_groups(self, groups): |
71 |
"""return the number of the given group(s) in which the user is |
|
72 |
||
73 |
:type groups: str or iterable(str) |
|
74 |
:param groups: a group name or an iterable on group names |
|
75 |
""" |
|
76 |
if isinstance(groups, basestring): |
|
77 |
groups = frozenset((groups,)) |
|
78 |
elif isinstance(groups, (tuple, list)): |
|
79 |
groups = frozenset(groups) |
|
4246
c95b8c7e5fb2
don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
80 |
return len(groups & self.groups) # XXX return the resulting set instead of its size |
0 | 81 |
|
82 |
def is_in_group(self, group): |
|
83 |
"""convience / shortcut method to test if the user belongs to `group` |
|
84 |
""" |
|
4402
360c532070f5
[entities] fix CWUser.is_in_group implementation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4246
diff
changeset
|
85 |
return group in self.groups |
0 | 86 |
|
590 | 87 |
def is_anonymous(self): |
88 |
""" checks if user is an anonymous user""" |
|
89 |
#FIXME on the web-side anonymous user is detected according |
|
90 |
# to config['anonymous-user'], we don't have this info on |
|
1553 | 91 |
# the server side. |
590 | 92 |
return self.groups == frozenset(('guests', )) |
93 |
||
0 | 94 |
def owns(self, eid): |
95 |
if hasattr(self.req, 'unsafe_execute'): |
|
96 |
# 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
|
97 |
# session's user doesn't have access to CWUser |
0 | 98 |
execute = self.req.unsafe_execute |
99 |
else: |
|
100 |
execute = self.req.execute |
|
101 |
try: |
|
102 |
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 |
except Unauthorized: |
|
105 |
return False |
|
106 |
owns = cached(owns, keyarg=1) |
|
107 |
||
108 |
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
|
109 |
rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\ |
0 | 110 |
'P name %(pname)s, P require_group G' |
111 |
kwargs = {'pname': pname, 'u': self.eid} |
|
112 |
cachekey = None |
|
113 |
if contexteid is not None: |
|
114 |
rql += ', X require_permission P, X eid %(x)s' |
|
115 |
kwargs['x'] = contexteid |
|
116 |
cachekey = 'x' |
|
117 |
try: |
|
3813
5b6eb3d6bf7c
Backed out changeset 8d0b5de8002b
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3792
diff
changeset
|
118 |
return self.req.execute(rql, kwargs, cachekey) |
0 | 119 |
except Unauthorized: |
120 |
return False |
|
1553 | 121 |
|
0 | 122 |
# presentation utilities ################################################## |
1553 | 123 |
|
0 | 124 |
def name(self): |
125 |
"""construct a name using firstname / surname or login if not defined""" |
|
1553 | 126 |
|
0 | 127 |
if self.firstname and self.surname: |
128 |
return self.req._('%(firstname)s %(surname)s') % { |
|
129 |
'firstname': self.firstname, 'surname' : self.surname} |
|
130 |
if self.firstname: |
|
131 |
return self.firstname |
|
132 |
return self.login |
|
133 |
||
134 |
def dc_title(self): |
|
135 |
return self.login |
|
136 |
||
137 |
dc_long_title = name |
|
138 |
||
139 |
def db_key_name(self): |
|
140 |
"""XXX goa specific""" |
|
141 |
return self.get('login') |
|
142 |
||
143 |
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
|
144 |
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
|
145 |
EGroup = class_renamed('EGroup', CWGroup) |