source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/students/dynamicroles.py @ 15156

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

Reorganise interfaces.

Transcript processing views and viewlets are now in the context of studycourses. Officers can now validate, sign and release transcripts directly on the transcript page.

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: dynamicroles.py 15155 2018-09-20 07:04:17Z 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        }
50
51    def getRolesForPrincipal(self, principal_id):
52        """Get roles for principal with id `principal_id`.
53
54        See waeup.kofa.applicants.dynamicroles.ApplicantPrincipalRoleManager
55        for further information.
56        """
57        apr_manager = AnnotationPrincipalRoleManager(self._context)
58        result = apr_manager.getRolesForPrincipal(principal_id)
59        if result != []:
60            # If there are local roles defined here, no additional
61            # lookup is done.
62            return result
63        # The principal has no local roles yet. Let's lookup the
64        # connected course, dept, etc.
65        if self.subcontainer:
66            obj = getattr(
67                self._context[self.subcontainer], self.extra_attrib, None)
68            current_level = getattr(
69                self._context[self.subcontainer], 'current_level', 0)
70        else:
71            obj = getattr(self._context, self.extra_attrib, None)
72            current_level = 0
73        # Lookup local roles for connected certificate and all parent
74        # objects. This way we fake 'role inheritance'.
75        while obj is not None:
76            extra_roles = IPrincipalRoleManager(obj).getRolesForPrincipal(
77                principal_id)
78            for role_id, setting in extra_roles:
79                if 'CourseAdviser' in role_id:
80                    # Found a Course Adviser role in external attribute or parent
81                    # thereof. We need a special treatment for Course Advisers.
82                    if current_level and str(100*(current_level/100)) in role_id:
83                        # Grant additional role, which allows to validate or reject
84                        # course lists, only if external role corresponds
85                        # with current_level of student.
86                        result.append(
87                            ('waeup.StudentsCourseAdviser', setting))
88                    else:
89                        # Otherwise grant at least view permissions.
90                        result.append(
91                            ('waeup.StudentsOfficer', setting))
92                elif 'UGClearanceOfficer' in role_id:
93                    if not self._context.is_postgrad:
94                        result.append(
95                            ('waeup.StudentsClearanceOfficer', setting))
96                    else:
97                        # Otherwise grant at least view permissions.
98                        result.append(
99                            ('waeup.StudentsOfficer', setting))
100                elif 'PGClearanceOfficer' in role_id:
101                    if self._context.is_postgrad:
102                        result.append(
103                            ('waeup.StudentsClearanceOfficer', setting))
104                    else:
105                        # Otherwise grant at least view permissions.
106                        result.append(
107                            ('waeup.StudentsOfficer', setting))
108                elif role_id in self.rolename_mapping.keys():
109                    # Grant additional role
110                    # permissions (allow, deny or unset)
111                    # according to the rolename mapping above.
112                    result.append(
113                        (self.rolename_mapping[role_id], setting))
114                    # Local roles have been found, no need to climb up further.
115                    obj = None
116            obj = getattr(obj, '__parent__', None)
117        return result
Note: See TracBrowser for help on using the repository browser.