author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 05 May 2010 18:10:33 +0200 | |
changeset 5473 | ee87c5352e63 |
parent 5426 | 0d4853a6e5ee |
child 5556 | 9ab2b4c74baf |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
17 |
# with CubicWeb. If not, see <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
|
18 |
"""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
|
19 |
|
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
20 |
""" |
d0c6f5efb837
fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
21 |
__docformat__ = "restructuredtext en" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
22 |
|
0 | 23 |
from logilab.common.decorators import cached |
24 |
||
25 |
from cubicweb import Unauthorized |
|
26 |
from cubicweb.entities import AnyEntity, fetch_config |
|
27 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
28 |
class CWGroup(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2784
diff
changeset
|
29 |
__regid__ = 'CWGroup' |
0 | 30 |
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
|
31 |
fetch_unrelated_order = fetch_order |
0 | 32 |
|
33 |
def db_key_name(self): |
|
34 |
"""XXX goa specific""" |
|
35 |
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
|
36 |
|
4246
c95b8c7e5fb2
don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
37 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1343
diff
changeset
|
38 |
class CWUser(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2784
diff
changeset
|
39 |
__regid__ = 'CWUser' |
0 | 40 |
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
|
41 |
fetch_unrelated_order = fetch_order |
1553 | 42 |
|
0 | 43 |
# used by repository to check if the user can log in or not |
44 |
AUTHENTICABLE_STATES = ('activated',) |
|
45 |
||
46 |
# low level utilities ##################################################### |
|
47 |
def __init__(self, *args, **kwargs): |
|
48 |
groups = kwargs.pop('groups', None) |
|
49 |
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
|
50 |
super(CWUser, self).__init__(*args, **kwargs) |
0 | 51 |
if groups is not None: |
52 |
self._groups = groups |
|
53 |
if properties is not None: |
|
54 |
self._properties = properties |
|
1553 | 55 |
|
0 | 56 |
@property |
57 |
def groups(self): |
|
58 |
try: |
|
59 |
return self._groups |
|
60 |
except AttributeError: |
|
61 |
self._groups = set(g.name for g in self.in_group) |
|
62 |
return self._groups |
|
1553 | 63 |
|
0 | 64 |
@property |
65 |
def properties(self): |
|
66 |
try: |
|
67 |
return self._properties |
|
68 |
except AttributeError: |
|
69 |
self._properties = dict((p.pkey, p.value) for p in self.reverse_for_user) |
|
70 |
return self._properties |
|
71 |
||
72 |
def property_value(self, key): |
|
73 |
try: |
|
74 |
# properties stored on the user aren't correctly typed |
|
75 |
# (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
|
76 |
return self._cw.vreg.typed_value(key, self.properties[key]) |
0 | 77 |
except KeyError: |
78 |
pass |
|
79 |
except ValueError: |
|
3380
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
80 |
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
|
81 |
key, self.login) |
3be33dc83d8b
use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3378
diff
changeset
|
82 |
return self._cw.vreg.property_value(key) |
1553 | 83 |
|
0 | 84 |
def matching_groups(self, groups): |
85 |
"""return the number of the given group(s) in which the user is |
|
86 |
||
87 |
:type groups: str or iterable(str) |
|
88 |
:param groups: a group name or an iterable on group names |
|
89 |
""" |
|
90 |
if isinstance(groups, basestring): |
|
91 |
groups = frozenset((groups,)) |
|
92 |
elif isinstance(groups, (tuple, list)): |
|
93 |
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
|
94 |
return len(groups & self.groups) # XXX return the resulting set instead of its size |
0 | 95 |
|
96 |
def is_in_group(self, group): |
|
97 |
"""convience / shortcut method to test if the user belongs to `group` |
|
98 |
""" |
|
4402
360c532070f5
[entities] fix CWUser.is_in_group implementation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4246
diff
changeset
|
99 |
return group in self.groups |
0 | 100 |
|
590 | 101 |
def is_anonymous(self): |
102 |
""" checks if user is an anonymous user""" |
|
103 |
#FIXME on the web-side anonymous user is detected according |
|
104 |
# to config['anonymous-user'], we don't have this info on |
|
1553 | 105 |
# the server side. |
590 | 106 |
return self.groups == frozenset(('guests', )) |
107 |
||
0 | 108 |
def owns(self, eid): |
109 |
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
|
110 |
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
|
111 |
'Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s', |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
112 |
{'x': eid, 'u': self.eid}) |
0 | 113 |
except Unauthorized: |
114 |
return False |
|
115 |
owns = cached(owns, keyarg=1) |
|
116 |
||
117 |
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
|
118 |
rql = 'Any P WHERE P is CWPermission, U eid %(u)s, U in_group G, '\ |
0 | 119 |
'P name %(pname)s, P require_group G' |
120 |
kwargs = {'pname': pname, 'u': self.eid} |
|
121 |
if contexteid is not None: |
|
122 |
rql += ', X require_permission P, X eid %(x)s' |
|
123 |
kwargs['x'] = contexteid |
|
124 |
try: |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
125 |
return self._cw.execute(rql, kwargs) |
0 | 126 |
except Unauthorized: |
127 |
return False |
|
1553 | 128 |
|
0 | 129 |
# presentation utilities ################################################## |
1553 | 130 |
|
0 | 131 |
def name(self): |
132 |
"""construct a name using firstname / surname or login if not defined""" |
|
1553 | 133 |
|
0 | 134 |
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
|
135 |
return self._cw._('%(firstname)s %(surname)s') % { |
0 | 136 |
'firstname': self.firstname, 'surname' : self.surname} |
137 |
if self.firstname: |
|
138 |
return self.firstname |
|
139 |
return self.login |
|
140 |
||
141 |
def dc_title(self): |
|
142 |
return self.login |
|
143 |
||
144 |
dc_long_title = name |
|
145 |
||
146 |
def db_key_name(self): |
|
147 |
"""XXX goa specific""" |
|
148 |
return self.get('login') |
|
149 |
||
150 |
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
|
151 |
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
|
152 |
EGroup = class_renamed('EGroup', CWGroup) |