author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 20 Apr 2010 12:35:23 +0200 | |
branch | stable |
changeset 5344 | ed4cd1ca96d6 |
parent 4835 | 13b0b96d7982 |
child 5174 | 78438ad513ca |
child 5421 | 8167de96c523 |
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 |
""" |
|
4402
360c532070f5
[entities] fix CWUser.is_in_group implementation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4246
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): |
96 |
try: |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
97 |
return self._cw.execute( |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
98 |
'Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s', |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
99 |
{'x': eid, 'u': self.eid}, 'x') |
0 | 100 |
except Unauthorized: |
101 |
return False |
|
102 |
owns = cached(owns, keyarg=1) |
|
103 |
||
104 |
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
|
105 |
rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\ |
0 | 106 |
'P name %(pname)s, P require_group G' |
107 |
kwargs = {'pname': pname, 'u': self.eid} |
|
108 |
cachekey = None |
|
109 |
if contexteid is not None: |
|
110 |
rql += ', X require_permission P, X eid %(x)s' |
|
111 |
kwargs['x'] = contexteid |
|
112 |
cachekey = 'x' |
|
113 |
try: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
114 |
return self._cw.execute(rql, kwargs, cachekey) |
0 | 115 |
except Unauthorized: |
116 |
return False |
|
1553 | 117 |
|
0 | 118 |
# presentation utilities ################################################## |
1553 | 119 |
|
0 | 120 |
def name(self): |
121 |
"""construct a name using firstname / surname or login if not defined""" |
|
1553 | 122 |
|
0 | 123 |
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
|
124 |
return self._cw._('%(firstname)s %(surname)s') % { |
0 | 125 |
'firstname': self.firstname, 'surname' : self.surname} |
126 |
if self.firstname: |
|
127 |
return self.firstname |
|
128 |
return self.login |
|
129 |
||
130 |
def dc_title(self): |
|
131 |
return self.login |
|
132 |
||
133 |
dc_long_title = name |
|
134 |
||
135 |
def db_key_name(self): |
|
136 |
"""XXX goa specific""" |
|
137 |
return self.get('login') |
|
138 |
||
139 |
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
|
140 |
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
|
141 |
EGroup = class_renamed('EGroup', CWGroup) |