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