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

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

Add 'show students' button to CoursePage?.

Lecturers can now list all students in course but they can neither access the students nor the respective course tickets. A CourseTicketPrincipalRoleManager? does not yet exist. And we have to discuss what lecturers are supposed to see. In my opinion, lecturers must no see student data.

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