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

Last change on this file since 15315 was 15163, checked in by Henrik Bettermann, 6 years ago

Merge with /main/waeup.kofa/branches/henrik-transcript-workflow:15127-15162

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
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
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        }
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 certificate 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 current_level and 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                    # Local roles have been found, no need to climb up further.
116                    obj = None
117            obj = getattr(obj, '__parent__', None)
118        return result
Note: See TracBrowser for help on using the repository browser.