[7193] | 1 | ## $Id: permissions.py 7205 2011-11-26 06:49:16Z 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 | ## |
---|
[3521] | 18 | import grok |
---|
[6157] | 19 | from zope.component import getUtilitiesFor |
---|
[6144] | 20 | from zope.interface import Interface |
---|
[6163] | 21 | from zope.securitypolicy.interfaces import IRole, IPrincipalRoleMap |
---|
[6144] | 22 | from waeup.sirp.interfaces import ILocalRolesAssignable |
---|
[7176] | 23 | from waeup.sirp.utils.helpers import get_user_account |
---|
[3521] | 24 | |
---|
[4789] | 25 | class Public(grok.Permission): |
---|
| 26 | """Everyone-can-do-this-permission. |
---|
| 27 | |
---|
| 28 | This permission is meant to be applied to objects/views/pages |
---|
| 29 | etc., that should be usable/readable by everyone. |
---|
| 30 | |
---|
| 31 | We need this to be able to tune default permissions more |
---|
| 32 | restrictive and open up some dedicated objects like the front |
---|
| 33 | page. |
---|
| 34 | """ |
---|
| 35 | grok.name('waeup.Public') |
---|
[6142] | 36 | |
---|
[5433] | 37 | class Anonymous(grok.Permission): |
---|
| 38 | """Only-anonymous-can-do-this-permission. |
---|
| 39 | """ |
---|
[6142] | 40 | grok.name('waeup.Anonymous') |
---|
[4789] | 41 | |
---|
[7184] | 42 | class Authenticated(grok.Permission): |
---|
| 43 | """Only-logged-in-users-can-do-this-permission. |
---|
| 44 | """ |
---|
| 45 | grok.name('waeup.Authenticated') |
---|
[4789] | 46 | |
---|
[7184] | 47 | class ViewAcademicsPermission(grok.Permission): |
---|
| 48 | grok.name('waeup.viewAcademics') |
---|
| 49 | |
---|
[4789] | 50 | class ManageUniversity(grok.Permission): |
---|
| 51 | grok.name('waeup.manageUniversity') |
---|
| 52 | |
---|
| 53 | class ManageUsers(grok.Permission): |
---|
| 54 | grok.name('waeup.manageUsers') |
---|
[6142] | 55 | |
---|
[7205] | 56 | class ShowStudents(grok.Permission): |
---|
| 57 | grok.name('waeup.showStudents') |
---|
| 58 | |
---|
[7163] | 59 | class EditUser(grok.Permission): |
---|
| 60 | grok.name('waeup.editUser') |
---|
| 61 | |
---|
[6127] | 62 | class ManageDataCenter(grok.Permission): |
---|
| 63 | grok.name('waeup.manageDataCenter') |
---|
[6142] | 64 | |
---|
[6907] | 65 | class ManagePortalConfiguration(grok.Permission): |
---|
| 66 | grok.name('waeup.managePortalConfiguration') |
---|
[6155] | 67 | |
---|
[7181] | 68 | class ManageACBatches(grok.Permission): |
---|
| 69 | grok.name('waeup.manageACBatches') |
---|
| 70 | |
---|
[6125] | 71 | # Local Roles |
---|
[7185] | 72 | class DepartmentManager(grok.Role): |
---|
| 73 | grok.name('waeup.local.DepartmentManager') |
---|
| 74 | grok.title(u'Department Manager') |
---|
[7205] | 75 | grok.permissions('waeup.manageUniversity','waeup.showStudents') |
---|
[6142] | 76 | |
---|
[6655] | 77 | class ClearanceOfficer(grok.Role): |
---|
[7168] | 78 | """The clearance officer role is meant for the |
---|
| 79 | assignment of dynamic roles only. |
---|
| 80 | """ |
---|
[6655] | 81 | grok.name('waeup.local.ClearanceOfficer') |
---|
| 82 | grok.title(u'Clearance Officer') |
---|
[7205] | 83 | grok.permissions('waeup.showStudents') |
---|
[6655] | 84 | |
---|
| 85 | class CourseAdviser(grok.Role): |
---|
[7168] | 86 | """The course adviser role is meant for the |
---|
| 87 | assignment of dynamic roles only. |
---|
| 88 | """ |
---|
[6655] | 89 | grok.name('waeup.local.CourseAdviser') |
---|
| 90 | grok.title(u'Course Adviser') |
---|
[7205] | 91 | grok.permissions('waeup.showStudents') |
---|
[6655] | 92 | |
---|
[7163] | 93 | class Owner(grok.Role): |
---|
| 94 | grok.name('waeup.local.Owner') |
---|
| 95 | grok.title(u'Owner') |
---|
| 96 | grok.permissions('waeup.editUser') |
---|
| 97 | |
---|
[7178] | 98 | # Site Roles |
---|
[7185] | 99 | class AcademicsOfficer(grok.Role): |
---|
| 100 | grok.name('waeup.AcademicsOfficer') |
---|
[7188] | 101 | grok.title(u'Academics Officer (view only)') |
---|
[7184] | 102 | grok.permissions('waeup.viewAcademics') |
---|
[3521] | 103 | |
---|
[7181] | 104 | class ACManager(grok.Role): |
---|
| 105 | grok.name('waeup.ACManager') |
---|
| 106 | grok.title(u'Access Code Manager') |
---|
| 107 | grok.permissions('waeup.manageACBatches') |
---|
| 108 | |
---|
[4789] | 109 | class PortalManager(grok.Role): |
---|
| 110 | grok.name('waeup.PortalManager') |
---|
[6159] | 111 | grok.title(u'Portal Manager') |
---|
[4789] | 112 | grok.permissions('waeup.manageUniversity', 'waeup.manageUsers', |
---|
[7184] | 113 | 'waeup.viewAcademics', 'waeup.manageACBatches', |
---|
[6198] | 114 | 'waeup.manageDataCenter','waeup.managePortalSettings', |
---|
[7184] | 115 | 'waeup.managePortalConfiguration', 'waeup.viewApplication', |
---|
| 116 | 'waeup.manageApplication', 'waeup.handleApplication', |
---|
[7148] | 117 | 'waeup.viewStudent', 'waeup.manageStudent', 'clearStudent', |
---|
[7184] | 118 | 'waeup.uploadStudentFile', 'waeup.viewStudents', |
---|
[7205] | 119 | 'waeup.viewHostels', 'waeup.manageHostels', |
---|
| 120 | 'waeup.showStudents') |
---|
[4789] | 121 | |
---|
[7186] | 122 | def get_all_roles(): |
---|
[6157] | 123 | """Return a list of tuples ``<ROLE-NAME>, <ROLE>``. |
---|
| 124 | """ |
---|
| 125 | return getUtilitiesFor(IRole) |
---|
| 126 | |
---|
[7186] | 127 | def get_waeup_roles(also_local=False): |
---|
[6157] | 128 | """Get all WAeUP roles. |
---|
| 129 | |
---|
| 130 | WAeUP roles are ordinary roles whose id by convention starts with |
---|
| 131 | a ``waeup.`` prefix. |
---|
| 132 | |
---|
| 133 | If `also_local` is ``True`` (``False`` by default), also local |
---|
| 134 | roles are returned. Local WAeUP roles are such whose id starts |
---|
| 135 | with ``waeup.local.`` prefix (this is also a convention). |
---|
| 136 | |
---|
| 137 | Returns a generator of the found roles. |
---|
| 138 | """ |
---|
[7186] | 139 | for name, item in get_all_roles(): |
---|
[6157] | 140 | if not name.startswith('waeup.'): |
---|
[4789] | 141 | # Ignore non-WAeUP roles... |
---|
| 142 | continue |
---|
[6157] | 143 | if not also_local and name.startswith('waeup.local.'): |
---|
| 144 | # Ignore local roles... |
---|
[6045] | 145 | continue |
---|
[6157] | 146 | yield item |
---|
[4789] | 147 | |
---|
[7186] | 148 | def get_waeup_role_names(): |
---|
[6157] | 149 | """Get the ids of all WAeUP roles. |
---|
| 150 | |
---|
[7186] | 151 | See :func:`get_waeup_roles` for what a 'WAeUPRole' is. |
---|
[6157] | 152 | |
---|
| 153 | This function returns a sorted list of WAeUP role names. |
---|
| 154 | """ |
---|
[7186] | 155 | return sorted([x.id for x in get_waeup_roles()]) |
---|
[6157] | 156 | |
---|
[6144] | 157 | class LocalRolesAssignable(grok.Adapter): |
---|
| 158 | """Default implementation for `ILocalRolesAssignable`. |
---|
| 159 | |
---|
| 160 | This adapter returns a list for dictionaries for objects for which |
---|
| 161 | we want to know the roles assignable to them locally. |
---|
| 162 | |
---|
| 163 | The returned dicts contain a ``name`` and a ``title`` entry which |
---|
| 164 | give a role (``name``) and a description, for which kind of users |
---|
| 165 | the permission is meant to be used (``title``). |
---|
| 166 | |
---|
| 167 | Having this adapter registered we make sure, that for each normal |
---|
| 168 | object we get a valid `ILocalRolesAssignable` adapter. |
---|
| 169 | |
---|
| 170 | Objects that want to offer certain local roles, can do so by |
---|
[6162] | 171 | setting a (preferably class-) attribute to a list of role ids. |
---|
[6144] | 172 | |
---|
| 173 | You can also define different adapters for different contexts to |
---|
| 174 | have different role lookup mechanisms become available. But in |
---|
| 175 | normal cases it should be sufficient to use this basic adapter. |
---|
| 176 | """ |
---|
| 177 | grok.context(Interface) |
---|
| 178 | grok.provides(ILocalRolesAssignable) |
---|
| 179 | |
---|
| 180 | _roles = [] |
---|
| 181 | |
---|
| 182 | def __init__(self, context): |
---|
| 183 | self.context = context |
---|
[6162] | 184 | role_ids = getattr(context, 'local_roles', self._roles) |
---|
[7186] | 185 | self._roles = [(name, role) for name, role in get_all_roles() |
---|
[6162] | 186 | if name in role_ids] |
---|
[6144] | 187 | return |
---|
| 188 | |
---|
| 189 | def __call__(self): |
---|
| 190 | """Get a list of dictionaries containing ``names`` (the roles to |
---|
| 191 | assign) and ``titles`` (some description of the type of user |
---|
| 192 | to assign each role to). |
---|
| 193 | """ |
---|
[6162] | 194 | return [ |
---|
| 195 | dict( |
---|
| 196 | name=name, |
---|
| 197 | title=role.title, |
---|
[6163] | 198 | description=role.description) |
---|
[6162] | 199 | for name, role in self._roles] |
---|
[6144] | 200 | |
---|
[6163] | 201 | def get_users_with_local_roles(context): |
---|
| 202 | """Get a list of dicts representing the local roles set for `context`. |
---|
| 203 | |
---|
| 204 | Each dict returns `user_name`, `user_title`, `local_role`, |
---|
| 205 | `local_role_title`, and `setting` for each entry in the local |
---|
| 206 | roles map of the `context` object. |
---|
| 207 | """ |
---|
[6202] | 208 | try: |
---|
| 209 | role_map = IPrincipalRoleMap(context) |
---|
| 210 | except TypeError: |
---|
| 211 | # no map no roles. |
---|
| 212 | raise StopIteration |
---|
[6163] | 213 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 214 | user = grok.getSite()['users'].get(user_name,None) |
---|
| 215 | user_title = getattr(user, 'description', user_name) |
---|
[7186] | 216 | local_role_title = dict(get_all_roles())[local_role].title |
---|
[6163] | 217 | yield dict(user_name = user_name, |
---|
| 218 | user_title = user_title, |
---|
| 219 | local_role = local_role, |
---|
| 220 | local_role_title = local_role_title, |
---|
| 221 | setting = setting) |
---|