source: main/waeup.kofa/trunk/src/waeup/kofa/students/dynamicroles.py @ 17731

Last change on this file since 17731 was 15968, checked in by Henrik Bettermann, 5 years ago

Add waeup.local.ReportsOfficer role.

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1## $Id: dynamicroles.py 15968 2020-01-30 22:47:56Z 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
20Students need special security policy treatment, as officers with
21local roles for departments and faculties might have additional
22permissions (local roles on depts/faculties) here.
23"""
24import grok
25from zope.securitypolicy.interfaces import IPrincipalRoleManager
26from zope.securitypolicy.principalpermission import (
27    AnnotationPrincipalPermissionManager,)
28from zope.securitypolicy.principalrole import AnnotationPrincipalRoleManager
29from waeup.kofa.students.interfaces import IStudent
30
31class StudentPrincipalRoleManager(AnnotationPrincipalRoleManager,
32                                    grok.Adapter):
33    grok.provides(IPrincipalRoleManager)
34    grok.context(IStudent)
35
36    #: The attribute name to lookup for additional roles
37    extra_attrib = 'certificate'
38    subcontainer = 'studycourse'
39
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',
46        'waeup.local.LocalStudentsManager': 'waeup.StudentsManager',
47        'waeup.local.LocalWorkflowManager': 'waeup.WorkflowManager',
48        'waeup.local.TranscriptOfficer': 'waeup.TranscriptOfficer',
49        'waeup.local.TranscriptSignee': 'waeup.TranscriptSignee',
50        'waeup.local.ReportsOfficer': 'waeup.ReportsOfficer',
51        }
52
53    def getRolesForPrincipal(self, principal_id):
54        """Get roles for principal with id `principal_id`.
55
56        See waeup.kofa.applicants.dynamicroles.ApplicantPrincipalRoleManager
57        for further information.
58        """
59        apr_manager = AnnotationPrincipalRoleManager(self._context)
60        result = apr_manager.getRolesForPrincipal(principal_id)
61        if result != []:
62            # If there are local roles defined here, no additional
63            # lookup is done.
64            return result
65        # The principal has no local roles yet. Let's lookup the
66        # connected course, dept, etc.
67        if self.subcontainer:
68            obj = getattr(
69                self._context[self.subcontainer], self.extra_attrib, None)
70            current_level = getattr(
71                self._context[self.subcontainer], 'current_level', 0)
72        else:
73            obj = getattr(self._context, self.extra_attrib, None)
74            current_level = 0
75        # Lookup local roles for connected certificate and all parent
76        # objects. This way we fake 'role inheritance'.
77        while obj is not None:
78            extra_roles = IPrincipalRoleManager(obj).getRolesForPrincipal(
79                principal_id)
80            for role_id, setting in extra_roles:
81                if 'CourseAdviser' in role_id:
82                    # Found a Course Adviser role in external attribute or parent
83                    # thereof. We need a special treatment for Course Advisers.
84                    if current_level and str(100*(current_level/100)) in role_id:
85                        # Grant additional role, which allows to validate or reject
86                        # course lists, only if external role corresponds
87                        # with current_level of student.
88                        result.append(
89                            ('waeup.StudentsCourseAdviser', setting))
90                    else:
91                        # Otherwise grant at least view permissions.
92                        result.append(
93                            ('waeup.StudentsOfficer', setting))
94                elif 'UGClearanceOfficer' in role_id:
95                    if not self._context.is_postgrad:
96                        result.append(
97                            ('waeup.StudentsClearanceOfficer', setting))
98                    else:
99                        # Otherwise grant at least view permissions.
100                        result.append(
101                            ('waeup.StudentsOfficer', setting))
102                elif 'PGClearanceOfficer' in role_id:
103                    if self._context.is_postgrad:
104                        result.append(
105                            ('waeup.StudentsClearanceOfficer', setting))
106                    else:
107                        # Otherwise grant at least view permissions.
108                        result.append(
109                            ('waeup.StudentsOfficer', setting))
110                elif role_id in self.rolename_mapping.keys():
111                    # Grant additional role
112                    # permissions (allow, deny or unset)
113                    # according to the rolename mapping above.
114                    result.append(
115                        (self.rolename_mapping[role_id], setting))
116                    # Local roles have been found, no need to climb up further.
117                    obj = None
118            obj = getattr(obj, '__parent__', None)
119        return result
Note: See TracBrowser for help on using the repository browser.