[7156] | 1 | ## $Id: dynamicroles.py 15163 2018-09-23 05:05:04Z 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 |
---|
[7256] | 25 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[7156] | 26 | from zope.securitypolicy.principalpermission import ( |
---|
| 27 | AnnotationPrincipalPermissionManager,) |
---|
[7256] | 28 | from zope.securitypolicy.principalrole import AnnotationPrincipalRoleManager |
---|
[7811] | 29 | from waeup.kofa.students.interfaces import IStudent |
---|
[7156] | 30 | |
---|
| 31 | class StudentPrincipalRoleManager(AnnotationPrincipalRoleManager, |
---|
| 32 | grok.Adapter): |
---|
| 33 | grok.provides(IPrincipalRoleManager) |
---|
[9565] | 34 | grok.context(IStudent) |
---|
[7156] | 35 | |
---|
| 36 | #: The attribute name to lookup for additional roles |
---|
| 37 | extra_attrib = 'certificate' |
---|
| 38 | subcontainer = 'studycourse' |
---|
| 39 | |
---|
[7220] | 40 | # Role name mapping: |
---|
| 41 | # role name to look for in `extra_attrib` and parents |
---|
| 42 | # to |
---|
| 43 | # role to add in case this role was found |
---|
| 44 | rolename_mapping = { |
---|
| 45 | 'waeup.local.ClearanceOfficer':'waeup.StudentsClearanceOfficer', |
---|
[10639] | 46 | 'waeup.local.LocalStudentsManager': 'waeup.StudentsManager', |
---|
| 47 | 'waeup.local.LocalWorkflowManager': 'waeup.WorkflowManager', |
---|
[15163] | 48 | 'waeup.local.TranscriptOfficer': 'waeup.TranscriptOfficer', |
---|
| 49 | 'waeup.local.TranscriptSignee': 'waeup.TranscriptSignee', |
---|
[7220] | 50 | } |
---|
[7156] | 51 | |
---|
| 52 | def getRolesForPrincipal(self, principal_id): |
---|
| 53 | """Get roles for principal with id `principal_id`. |
---|
| 54 | |
---|
[8963] | 55 | See waeup.kofa.applicants.dynamicroles.ApplicantPrincipalRoleManager |
---|
| 56 | for further information. |
---|
[7156] | 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) |
---|
[7334] | 69 | current_level = getattr( |
---|
[7336] | 70 | self._context[self.subcontainer], 'current_level', 0) |
---|
[7156] | 71 | else: |
---|
| 72 | obj = getattr(self._context, self.extra_attrib, None) |
---|
[7336] | 73 | current_level = 0 |
---|
[9565] | 74 | # Lookup local roles for connected certificate and all parent |
---|
[7156] | 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: |
---|
[7336] | 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. |
---|
[11254] | 83 | if current_level and str(100*(current_level/100)) in role_id: |
---|
[7336] | 84 | # Grant additional role, which allows to validate or reject |
---|
| 85 | # course lists, only if external role corresponds |
---|
| 86 | # with current_level of student. |
---|
[7334] | 87 | result.append( |
---|
[7336] | 88 | ('waeup.StudentsCourseAdviser', setting)) |
---|
| 89 | else: |
---|
| 90 | # Otherwise grant at least view permissions. |
---|
| 91 | result.append( |
---|
| 92 | ('waeup.StudentsOfficer', setting)) |
---|
[8963] | 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)) |
---|
[7336] | 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)) |
---|
[10639] | 115 | # Local roles have been found, no need to climb up further. |
---|
| 116 | obj = None |
---|
[7156] | 117 | obj = getattr(obj, '__parent__', None) |
---|
[7811] | 118 | return result |
---|