author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 11 Aug 2009 17:27:27 +0200 | |
changeset 2778 | 3f6dfc312760 |
parent 1977 | 606923dff11b |
child 2784 | f395115070c1 |
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: |
|
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
self._groups = set(g.cwdb.name for g in self.cwdb.in_group) |
0 | 48 |
return self._groups |
1553 | 49 |
|
0 | 50 |
@property |
51 |
def properties(self): |
|
52 |
try: |
|
53 |
return self._properties |
|
54 |
except AttributeError: |
|
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
55 |
self._properties = dict((p.cwdb.pkey, p.cwdb.value) |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
for p in self.cwdb.reverse_for_user) |
0 | 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: |
|
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
67 |
self.warning('incorrect value for property %s of user %s', key, |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
68 |
self.cwdb.login) |
0 | 69 |
return self.vreg.property_value(key) |
1553 | 70 |
|
0 | 71 |
def matching_groups(self, groups): |
72 |
"""return the number of the given group(s) in which the user is |
|
73 |
||
74 |
:type groups: str or iterable(str) |
|
75 |
:param groups: a group name or an iterable on group names |
|
76 |
""" |
|
77 |
if isinstance(groups, basestring): |
|
78 |
groups = frozenset((groups,)) |
|
79 |
elif isinstance(groups, (tuple, list)): |
|
80 |
groups = frozenset(groups) |
|
81 |
return len(groups & self.groups) |
|
82 |
||
83 |
def is_in_group(self, group): |
|
84 |
"""convience / shortcut method to test if the user belongs to `group` |
|
85 |
""" |
|
86 |
return self.matching_groups(group) == 1 |
|
87 |
||
590 | 88 |
def is_anonymous(self): |
89 |
""" checks if user is an anonymous user""" |
|
90 |
#FIXME on the web-side anonymous user is detected according |
|
91 |
# to config['anonymous-user'], we don't have this info on |
|
1553 | 92 |
# the server side. |
590 | 93 |
return self.groups == frozenset(('guests', )) |
94 |
||
0 | 95 |
def owns(self, eid): |
96 |
if hasattr(self.req, 'unsafe_execute'): |
|
97 |
# 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
|
98 |
# session's user doesn't have access to CWUser |
0 | 99 |
execute = self.req.unsafe_execute |
100 |
else: |
|
101 |
execute = self.req.execute |
|
102 |
try: |
|
103 |
return execute('Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s', |
|
104 |
{'x': eid, 'u': self.eid}, 'x') |
|
105 |
except Unauthorized: |
|
106 |
return False |
|
107 |
owns = cached(owns, keyarg=1) |
|
108 |
||
109 |
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
|
110 |
rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\ |
0 | 111 |
'P name %(pname)s, P require_group G' |
112 |
kwargs = {'pname': pname, 'u': self.eid} |
|
113 |
cachekey = None |
|
114 |
if contexteid is not None: |
|
115 |
rql += ', X require_permission P, X eid %(x)s' |
|
116 |
kwargs['x'] = contexteid |
|
117 |
cachekey = 'x' |
|
118 |
try: |
|
119 |
return self.req.execute(rql, kwargs, cachekey) |
|
120 |
except Unauthorized: |
|
121 |
return False |
|
1553 | 122 |
|
0 | 123 |
# presentation utilities ################################################## |
1553 | 124 |
|
0 | 125 |
def name(self): |
126 |
"""construct a name using firstname / surname or login if not defined""" |
|
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
127 |
surname = self.get_value('surname') |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
128 |
firstname = self.get_value('firstname') |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
129 |
if firstname and surname: |
0 | 130 |
return self.req._('%(firstname)s %(surname)s') % { |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
131 |
'firstname': firstname, 'surname' : surname} |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
132 |
if firstname or surname: |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
133 |
return surname or firstname |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
134 |
return self.get_value('login') |
0 | 135 |
|
136 |
def dc_title(self): |
|
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
137 |
return self.get_value('login') |
0 | 138 |
|
139 |
dc_long_title = name |
|
140 |
||
141 |
def db_key_name(self): |
|
142 |
"""XXX goa specific""" |
|
143 |
return self.get('login') |
|
144 |
||
145 |
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
|
146 |
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
|
147 |
EGroup = class_renamed('EGroup', CWGroup) |