author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 27 Jan 2010 09:56:16 +0100 | |
changeset 4388 | 15c6607c4bda |
parent 4252 | 6c4f109c2b03 |
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): |
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 |
|
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): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2784
diff
changeset
|
26 |
__regid__ = '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) |
|
3380
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
63 |
return self._cw.vreg.typed_value(key, self.properties[key]) |
0 | 64 |
except KeyError: |
65 |
pass |
|
66 |
except ValueError: |
|
3380
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
67 |
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
|
68 |
key, self.login) |
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
69 |
return self._cw.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) |
|
4246
c95b8c7e5fb2
don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
81 |
return len(groups & self.groups) # XXX return the resulting set instead of its size |
0 | 82 |
|
83 |
def is_in_group(self, group): |
|
84 |
"""convience / shortcut method to test if the user belongs to `group` |
|
85 |
""" |
|
4246
c95b8c7e5fb2
don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
86 |
return group in self._groups |
0 | 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): |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
96 |
if hasattr(self._cw, 'unsafe_execute'): |
0 | 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 |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
99 |
execute = self._cw.unsafe_execute |
0 | 100 |
else: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
101 |
execute = self._cw.execute |
0 | 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: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
119 |
return self._cw.execute(rql, kwargs, cachekey) |
0 | 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""" |
|
1553 | 127 |
|
0 | 128 |
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
|
129 |
return self._cw._('%(firstname)s %(surname)s') % { |
0 | 130 |
'firstname': self.firstname, 'surname' : self.surname} |
131 |
if self.firstname: |
|
132 |
return self.firstname |
|
133 |
return self.login |
|
134 |
||
135 |
def dc_title(self): |
|
136 |
return self.login |
|
137 |
||
138 |
dc_long_title = name |
|
139 |
||
140 |
def db_key_name(self): |
|
141 |
"""XXX goa specific""" |
|
142 |
return self.get('login') |
|
143 |
||
144 |
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
|
145 |
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
|
146 |
EGroup = class_renamed('EGroup', CWGroup) |