author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 09:34:46 +0200 | |
changeset 3380 | 3be33dc83d8b |
parent 3378 | 2f25f701301d |
child 3890 | d7a270f50f54 |
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): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2784
diff
changeset
|
16 |
__regid__ = '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): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2784
diff
changeset
|
25 |
__regid__ = '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: |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
47 |
self._groups = set(g.name for g in self.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: |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
55 |
self._properties = dict((p.pkey, p.value) for p in self.reverse_for_user) |
0 | 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) |
|
3380
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
62 |
return self._cw.vreg.typed_value(key, self.properties[key]) |
0 | 63 |
except KeyError: |
64 |
pass |
|
65 |
except ValueError: |
|
3380
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
66 |
self.warning('incorrect value for eproperty %s of user %s', |
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
67 |
key, self.login) |
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
68 |
return self._cw.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) |
|
80 |
return len(groups & self.groups) |
|
81 |
||
82 |
def is_in_group(self, group): |
|
83 |
"""convience / shortcut method to test if the user belongs to `group` |
|
84 |
""" |
|
85 |
return self.matching_groups(group) == 1 |
|
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): |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
95 |
if hasattr(self._cw, 'unsafe_execute'): |
0 | 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 |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
98 |
execute = self._cw.unsafe_execute |
0 | 99 |
else: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
100 |
execute = self._cw.execute |
0 | 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: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
118 |
return self._cw.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""" |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
126 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
127 |
if self.firstname and self.surname: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
128 |
return self._cw._('%(firstname)s %(surname)s') % { |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
129 |
'firstname': self.firstname, 'surname' : self.surname} |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
130 |
if self.firstname: |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
131 |
return self.firstname |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
132 |
return self.login |
0 | 133 |
|
134 |
def dc_title(self): |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
135 |
return self.login |
0 | 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) |