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

Last change on this file since 9189 was 9002, checked in by Henrik Bettermann, 12 years ago

Add lecturer local role which has no dynamic role assignment yet.

Add test_import to CourseProcessorTests?.

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