21 IManagerRestrictionsFactory, IBaseSharedTool, IManagerRestrictionInfo |
21 IManagerRestrictionsFactory, IBaseSharedTool, IManagerRestrictionInfo |
22 |
22 |
23 # import packages |
23 # import packages |
24 from persistent import Persistent |
24 from persistent import Persistent |
25 from pyams_security.interfaces import IPrincipalInfo, IRevokedRoleEvent, IGrantedRoleEvent |
25 from pyams_security.interfaces import IPrincipalInfo, IRevokedRoleEvent, IGrantedRoleEvent |
26 from pyams_utils.adapter import adapter_config, ContextAdapter |
26 from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter |
27 from pyams_utils.request import check_request |
27 from pyams_utils.request import check_request |
28 from pyams_utils.traversing import get_parent |
28 from pyams_utils.traversing import get_parent |
29 from pyramid.events import subscriber |
29 from pyramid.events import subscriber |
30 from zope.annotation.interfaces import IAnnotations |
|
31 from zope.container.folder import Folder |
30 from zope.container.folder import Folder |
32 from zope.interface import implementer |
31 from zope.interface import implementer |
33 from zope.location import locate |
|
34 from zope.schema.fieldproperty import FieldProperty |
32 from zope.schema.fieldproperty import FieldProperty |
35 |
33 |
36 |
34 |
37 @implementer(IManagerRestrictionInfo) |
35 @implementer(IManagerRestrictionInfo) |
38 class SharedToolManagerRestrictionInfo(Persistent): |
36 class SharedToolManagerRestrictionInfo(Persistent): |
62 @adapter_config(context=IBaseSharedTool, provides=IManagerRestrictions) |
60 @adapter_config(context=IBaseSharedTool, provides=IManagerRestrictions) |
63 class SharedToolManagerRestrictions(ContextAdapter): |
61 class SharedToolManagerRestrictions(ContextAdapter): |
64 """Shared tool manager restrictions""" |
62 """Shared tool manager restrictions""" |
65 |
63 |
66 def get_restrictions(self, principal): |
64 def get_restrictions(self, principal): |
67 annotations = IAnnotations(self.context) |
65 restrictions_folder = get_annotation_adapter(self.context, MANAGER_RESTRICTIONS_KEY, Folder) |
68 restrictions_folder = annotations.get(MANAGER_RESTRICTIONS_KEY) |
|
69 if restrictions_folder is None: |
|
70 restrictions_folder = annotations[MANAGER_RESTRICTIONS_KEY] = Folder() |
|
71 locate(restrictions_folder, self.context) |
|
72 if IPrincipalInfo.providedBy(principal): |
66 if IPrincipalInfo.providedBy(principal): |
73 principal = principal.id |
67 principal = principal.id |
74 return restrictions_folder.get(principal) |
68 return restrictions_folder.get(principal) |
75 |
69 |
76 def set_restrictions(self, principal, restrictions): |
70 def set_restrictions(self, principal, restrictions): |
77 annotations = IAnnotations(self.context) |
71 restrictions_folder = get_annotation_adapter(self.context, MANAGER_RESTRICTIONS_KEY, Folder) |
78 restrictions_folder = annotations.get(MANAGER_RESTRICTIONS_KEY) |
|
79 if restrictions_folder is None: |
|
80 restrictions_folder = annotations[MANAGER_RESTRICTIONS_KEY] = Folder() |
|
81 locate(restrictions_folder, self.context) |
|
82 if IPrincipalInfo.providedBy(principal): |
72 if IPrincipalInfo.providedBy(principal): |
83 principal = principal.id |
73 principal = principal.id |
84 restrictions_folder[principal] = restrictions |
74 restrictions_folder[principal] = restrictions |
85 |
75 |
86 def drop_restrictions(self, principal): |
76 def drop_restrictions(self, principal): |
87 annotations = IAnnotations(self.context) |
77 restrictions_folder = get_annotation_adapter(self.context, MANAGER_RESTRICTIONS_KEY) |
88 restrictions_folder = annotations.get(MANAGER_RESTRICTIONS_KEY) |
|
89 if restrictions_folder is None: |
78 if restrictions_folder is None: |
90 return |
79 return |
91 if IPrincipalInfo.providedBy(principal): |
80 if IPrincipalInfo.providedBy(principal): |
92 principal = principal.id |
81 principal = principal.id |
93 if principal in restrictions_folder: |
82 if principal in restrictions_folder: |