1 | ## |
---|
2 | ## securitypolicy.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Mon Nov 14 09:37:10 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """Security policy components for applicants. |
---|
23 | |
---|
24 | Applicants need special security policy treatment, as officers with |
---|
25 | local roles for departments and faculties might have additional |
---|
26 | permissions (local roles on depts/faculties) here. |
---|
27 | """ |
---|
28 | import grok |
---|
29 | from zope.securitypolicy.interfaces import ( |
---|
30 | IPrincipalRoleManager, IPrincipalPermissionManager,) |
---|
31 | from zope.securitypolicy.principalrole import AnnotationPrincipalRoleManager |
---|
32 | from zope.securitypolicy.principalpermission import ( |
---|
33 | AnnotationPrincipalPermissionManager,) |
---|
34 | from zope.securitypolicy.securitymap import AnnotationSecurityMap |
---|
35 | from zope.securitypolicy.settings import Allow, Deny, Unset |
---|
36 | from waeup.sirp.applicants.interfaces import IApplicant |
---|
37 | |
---|
38 | # All components in here have the same context: Applicant instances |
---|
39 | grok.context(IApplicant) |
---|
40 | |
---|
41 | class ApplicantSecurityMap(AnnotationSecurityMap): |
---|
42 | pass |
---|
43 | |
---|
44 | class ApplicantPrincipalRoleManager(AnnotationPrincipalRoleManager, |
---|
45 | grok.Adapter): |
---|
46 | grok.provides(IPrincipalRoleManager) |
---|
47 | |
---|
48 | def getRolesForPrincipal(self, principal_id): |
---|
49 | """Get roles for principal with id `principal_id`. |
---|
50 | |
---|
51 | Different to the default implementation, this method also |
---|
52 | takes into account local roles set on any department connected |
---|
53 | to the context applicant. |
---|
54 | |
---|
55 | If the given principal has 'waeup.local.ClearanceOfficer' |
---|
56 | permissions set on the connected department, it additionally |
---|
57 | gets 'waeup.ApplicationsOfficer' role for the context |
---|
58 | applicant. |
---|
59 | |
---|
60 | Some advantages of this approach: |
---|
61 | |
---|
62 | - we don't have to store extra local roles for clearance |
---|
63 | officers in ZODB for each applicant |
---|
64 | |
---|
65 | - when local roles on a department change, we don't have to |
---|
66 | update thousands of applicants; the local role is assigned |
---|
67 | dynamically. |
---|
68 | |
---|
69 | Disadvantage: |
---|
70 | |
---|
71 | - More expensive role lookups when a clearance officer wants |
---|
72 | to see an applicant form. |
---|
73 | """ |
---|
74 | result = super(ApplicantPrincipalRoleManager, self |
---|
75 | ).getRolesForPrincipal(principal_id) |
---|
76 | if result != []: |
---|
77 | # If there are local roles defined here, no additional |
---|
78 | # lookup is done. |
---|
79 | return result |
---|
80 | # The principal has no local roles yet. Let's lookup the |
---|
81 | # connected dept. |
---|
82 | course = getattr(self._context, 'course1', None) |
---|
83 | dept = getattr( |
---|
84 | getattr(course, '__parent__', None), |
---|
85 | '__parent__', None) |
---|
86 | if dept is None: |
---|
87 | # No deptartment, no extra roles. |
---|
88 | return result |
---|
89 | dept_roles = IPrincipalRoleManager(dept).getRolesForPrincipal( |
---|
90 | principal_id) |
---|
91 | # 'Grant' 'waeup.ApplicationsOfficer' permissions (allow, deny |
---|
92 | # or unset) for the passed in principal id if it has clearance |
---|
93 | # officer role on the connected department. |
---|
94 | for role_id, setting in dept_roles: |
---|
95 | if role_id == 'waeup.local.ClearanceOfficer': |
---|
96 | result.append( |
---|
97 | ('waeup.ApplicationsOfficer', setting)) |
---|
98 | return result |
---|