author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 15 Feb 2010 15:05:15 +0100 | |
branch | stable |
changeset 4569 | 1acd90d0cb59 |
parent 4252 | 6c4f109c2b03 |
child 4570 | ede247bbbf62 |
permissions | -rw-r--r-- |
0 | 1 |
"""Security hooks: check permissions to add/delete/update entities according to |
2 |
the user connected to a session |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from cubicweb import Unauthorized |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
12 |
from cubicweb.server import BEFORE_ADD_RELATIONS, ON_COMMIT_ADD_RELATIONS, hook |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
13 |
|
0 | 14 |
|
15 |
def check_entity_attributes(session, entity): |
|
16 |
eid = entity.eid |
|
17 |
eschema = entity.e_schema |
|
18 |
# ._default_set is only there on entity creation to indicate unspecified |
|
19 |
# attributes which has been set to a default value defined in the schema |
|
20 |
defaults = getattr(entity, '_default_set', ()) |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
21 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
editedattrs = entity.edited_attributes |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
23 |
except AttributeError: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
24 |
editedattrs = entity |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
25 |
for attr in editedattrs: |
0 | 26 |
if attr in defaults: |
27 |
continue |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
28 |
rdef = eschema.rdef(attr) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
29 |
if rdef.final: # non final relation are checked by other hooks |
0 | 30 |
# add/delete should be equivalent (XXX: unify them into 'update' ?) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
31 |
rdef.check_perm(session, 'add', eid=eid) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
32 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
33 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
34 |
class _CheckEntityPermissionOp(hook.LateOperation): |
0 | 35 |
def precommit_event(self): |
36 |
#print 'CheckEntityPermissionOp', self.session.user, self.entity, self.action |
|
37 |
self.entity.check_perm(self.action) |
|
38 |
check_entity_attributes(self.session, self.entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
39 |
|
0 | 40 |
def commit_event(self): |
41 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
42 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
43 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
44 |
class _CheckRelationPermissionOp(hook.LateOperation): |
0 | 45 |
def precommit_event(self): |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
46 |
rdef = self.rschema.rdef(self.session.describe(self.eidfrom)[0], |
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
47 |
self.session.describe(self.eidto)[0]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
48 |
rdef.check_perm(self.session, self.action, |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
49 |
fromeid=self.eidfrom, toeid=self.eidto) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
50 |
|
0 | 51 |
def commit_event(self): |
52 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
53 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
54 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
55 |
class SecurityHook(hook.Hook): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
56 |
__abstract__ = True |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
57 |
category = 'security' |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
58 |
__select__ = hook.Hook.__select__ & hook.regular_session() |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
59 |
|
0 | 60 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
61 |
class AfterAddEntitySecurityHook(SecurityHook): |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
62 |
__regid__ = 'securityafteraddentity' |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
63 |
events = ('after_add_entity',) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
64 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
65 |
def __call__(self): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
66 |
_CheckEntityPermissionOp(self._cw, entity=self.entity, action='add') |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
67 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
68 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
69 |
class AfterUpdateEntitySecurityHook(SecurityHook): |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
70 |
__regid__ = 'securityafterupdateentity' |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
71 |
events = ('after_update_entity',) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
72 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
73 |
def __call__(self): |
0 | 74 |
try: |
75 |
# check user has permission right now, if not retry at commit time |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
76 |
self.entity.check_perm('update') |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
77 |
check_entity_attributes(self._cw, self.entity) |
0 | 78 |
except Unauthorized: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
79 |
self.entity.clear_local_perm_cache('update') |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
80 |
_CheckEntityPermissionOp(self._cw, entity=self.entity, action='update') |
0 | 81 |
|
82 |
||
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
83 |
class BeforeDelEntitySecurityHook(SecurityHook): |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
84 |
__regid__ = 'securitybeforedelentity' |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
85 |
events = ('before_delete_entity',) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
86 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
87 |
def __call__(self): |
2895
903bd3f89f80
should directly use entity.check_perm now that we've an entity instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2847
diff
changeset
|
88 |
self.entity.check_perm('delete') |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
89 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
90 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
91 |
class BeforeAddRelationSecurityHook(SecurityHook): |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
92 |
__regid__ = 'securitybeforeaddrelation' |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
93 |
events = ('before_add_relation',) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
94 |
|
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
95 |
def __call__(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
96 |
if self.rtype in BEFORE_ADD_RELATIONS: |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
97 |
nocheck = self._cw.transaction_data.get('skip-security', ()) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
98 |
if (self.eidfrom, self.rtype, self.eidto) in nocheck: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
99 |
return |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
100 |
rschema = self._cw.repo.schema[self.rtype] |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
101 |
rdef = rschema.rdef(self._cw.describe(self.eidfrom)[0], |
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
102 |
self._cw.describe(self.eidto)[0]) |
4190
742e3eb16f81
fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4048
diff
changeset
|
103 |
rdef.check_perm(self._cw, 'add', fromeid=self.eidfrom, toeid=self.eidto) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
104 |
|
0 | 105 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
106 |
class AfterAddRelationSecurityHook(SecurityHook): |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
107 |
__regid__ = 'securityafteraddrelation' |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
108 |
events = ('after_add_relation',) |
0 | 109 |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
110 |
def __call__(self): |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
111 |
if not self.rtype in BEFORE_ADD_RELATIONS: |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
112 |
nocheck = self._cw.transaction_data.get('skip-security', ()) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
113 |
if (self.eidfrom, self.rtype, self.eidto) in nocheck: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
114 |
return |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
115 |
rschema = self._cw.repo.schema[self.rtype] |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
116 |
if self.rtype in ON_COMMIT_ADD_RELATIONS: |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
117 |
_CheckRelationPermissionOp(self._cw, action='add', |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
118 |
rschema=rschema, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
119 |
eidfrom=self.eidfrom, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
120 |
eidto=self.eidto) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
121 |
else: |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
122 |
rdef = rschema.rdef(self._cw.describe(self.eidfrom)[0], |
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
123 |
self._cw.describe(self.eidto)[0]) |
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
124 |
rdef.check_perm(self._cw, 'add', fromeid=self.eidfrom, toeid=self.eidto) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
125 |
|
4048
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
126 |
|
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
127 |
class BeforeDeleteRelationSecurityHook(SecurityHook): |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
128 |
__regid__ = 'securitybeforedelrelation' |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
129 |
events = ('before_delete_relation',) |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
130 |
|
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
131 |
def __call__(self): |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
132 |
nocheck = self._cw.transaction_data.get('skip-security', ()) |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
133 |
if (self.eidfrom, self.rtype, self.eidto) in nocheck: |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
134 |
return |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
135 |
rschema = self._cw.repo.schema[self.rtype] |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
136 |
rdef = rschema.rdef(self._cw.describe(self.eidfrom)[0], |
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
137 |
self._cw.describe(self.eidto)[0]) |
4190
742e3eb16f81
fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4048
diff
changeset
|
138 |
rdef.check_perm(self._cw, 'delete', fromeid=self.eidfrom, toeid=self.eidto) |
4048
12c4f7e2bed6
had been involontarly dropped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
139 |