1 | ## $Id: dynamicroles.py 8963 2012-07-10 05:39:02Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Security policy components for students. |
---|
19 | |
---|
20 | Students need special security policy treatment, as officers with |
---|
21 | local roles for departments and faculties might have additional |
---|
22 | permissions (local roles on depts/faculties) here. |
---|
23 | """ |
---|
24 | import grok |
---|
25 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
26 | from zope.securitypolicy.principalpermission import ( |
---|
27 | AnnotationPrincipalPermissionManager,) |
---|
28 | from zope.securitypolicy.principalrole import AnnotationPrincipalRoleManager |
---|
29 | from waeup.kofa.students.interfaces import IStudent |
---|
30 | |
---|
31 | # All components in here have the same context: Student instances |
---|
32 | grok.context(IStudent) |
---|
33 | |
---|
34 | class StudentPrincipalRoleManager(AnnotationPrincipalRoleManager, |
---|
35 | grok.Adapter): |
---|
36 | grok.provides(IPrincipalRoleManager) |
---|
37 | |
---|
38 | #: The attribute name to lookup for additional roles |
---|
39 | extra_attrib = 'certificate' |
---|
40 | subcontainer = 'studycourse' |
---|
41 | |
---|
42 | # Role name mapping: |
---|
43 | # role name to look for in `extra_attrib` and parents |
---|
44 | # to |
---|
45 | # role to add in case this role was found |
---|
46 | rolename_mapping = { |
---|
47 | 'waeup.local.ClearanceOfficer':'waeup.StudentsClearanceOfficer', |
---|
48 | 'waeup.local.UGClearanceOfficer':'waeup.StudentsClearanceOfficer', |
---|
49 | 'waeup.local.PGClearanceOfficer':'waeup.StudentsClearanceOfficer', |
---|
50 | } |
---|
51 | |
---|
52 | def getRolesForPrincipal(self, principal_id): |
---|
53 | """Get roles for principal with id `principal_id`. |
---|
54 | |
---|
55 | See waeup.kofa.applicants.dynamicroles.ApplicantPrincipalRoleManager |
---|
56 | for further information. |
---|
57 | """ |
---|
58 | apr_manager = AnnotationPrincipalRoleManager(self._context) |
---|
59 | result = apr_manager.getRolesForPrincipal(principal_id) |
---|
60 | if result != []: |
---|
61 | # If there are local roles defined here, no additional |
---|
62 | # lookup is done. |
---|
63 | return result |
---|
64 | # The principal has no local roles yet. Let's lookup the |
---|
65 | # connected course, dept, etc. |
---|
66 | if self.subcontainer: |
---|
67 | obj = getattr( |
---|
68 | self._context[self.subcontainer], self.extra_attrib, None) |
---|
69 | current_level = getattr( |
---|
70 | self._context[self.subcontainer], 'current_level', 0) |
---|
71 | else: |
---|
72 | obj = getattr(self._context, self.extra_attrib, None) |
---|
73 | current_level = 0 |
---|
74 | # Lookup local roles for connected course and all parent |
---|
75 | # objects. This way we fake 'role inheritance'. |
---|
76 | while obj is not None: |
---|
77 | extra_roles = IPrincipalRoleManager(obj).getRolesForPrincipal( |
---|
78 | principal_id) |
---|
79 | for role_id, setting in extra_roles: |
---|
80 | if 'CourseAdviser' in role_id: |
---|
81 | # Found a Course Adviser role in external attribute or parent |
---|
82 | # thereof. We need a special treatment for Course Advisers. |
---|
83 | if str(100*(current_level/100)) in role_id: |
---|
84 | # Grant additional role, which allows to validate or reject |
---|
85 | # course lists, only if external role corresponds |
---|
86 | # with current_level of student. |
---|
87 | result.append( |
---|
88 | ('waeup.StudentsCourseAdviser', setting)) |
---|
89 | else: |
---|
90 | # Otherwise grant at least view permissions. |
---|
91 | result.append( |
---|
92 | ('waeup.StudentsOfficer', setting)) |
---|
93 | elif 'UGClearanceOfficer' in role_id: |
---|
94 | if not self._context.is_postgrad: |
---|
95 | result.append( |
---|
96 | ('waeup.StudentsClearanceOfficer', setting)) |
---|
97 | else: |
---|
98 | # Otherwise grant at least view permissions. |
---|
99 | result.append( |
---|
100 | ('waeup.StudentsOfficer', setting)) |
---|
101 | elif 'PGClearanceOfficer' in role_id: |
---|
102 | if self._context.is_postgrad: |
---|
103 | result.append( |
---|
104 | ('waeup.StudentsClearanceOfficer', setting)) |
---|
105 | else: |
---|
106 | # Otherwise grant at least view permissions. |
---|
107 | result.append( |
---|
108 | ('waeup.StudentsOfficer', setting)) |
---|
109 | elif role_id in self.rolename_mapping.keys(): |
---|
110 | # Grant additional role |
---|
111 | # permissions (allow, deny or unset) |
---|
112 | # according to the rolename mapping above. |
---|
113 | result.append( |
---|
114 | (self.rolename_mapping[role_id], setting)) |
---|
115 | return result |
---|
116 | obj = getattr(obj, '__parent__', None) |
---|
117 | return result |
---|