[7193] | 1 | ## $Id: permissions.py 10227 2013-05-25 05:59:03Z 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 |
---|
[7811] | 22 | from waeup.kofa.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 | |
---|
[8367] | 49 | class ManageAcademicsPermission(grok.Permission): |
---|
| 50 | grok.name('waeup.manageAcademics') |
---|
[4789] | 51 | |
---|
[8367] | 52 | class ManagePortal(grok.Permission): |
---|
| 53 | grok.name('waeup.managePortal') |
---|
| 54 | |
---|
[4789] | 55 | class ManageUsers(grok.Permission): |
---|
| 56 | grok.name('waeup.manageUsers') |
---|
[6142] | 57 | |
---|
[7205] | 58 | class ShowStudents(grok.Permission): |
---|
| 59 | grok.name('waeup.showStudents') |
---|
| 60 | |
---|
[7163] | 61 | class EditUser(grok.Permission): |
---|
| 62 | grok.name('waeup.editUser') |
---|
| 63 | |
---|
[6127] | 64 | class ManageDataCenter(grok.Permission): |
---|
| 65 | grok.name('waeup.manageDataCenter') |
---|
[6142] | 66 | |
---|
[8367] | 67 | class ImportData(grok.Permission): |
---|
| 68 | grok.name('waeup.importData') |
---|
| 69 | |
---|
[10177] | 70 | class ExportData(grok.Permission): |
---|
| 71 | grok.name('waeup.exportData') |
---|
| 72 | |
---|
[6907] | 73 | class ManagePortalConfiguration(grok.Permission): |
---|
| 74 | grok.name('waeup.managePortalConfiguration') |
---|
[6155] | 75 | |
---|
[7181] | 76 | class ManageACBatches(grok.Permission): |
---|
| 77 | grok.name('waeup.manageACBatches') |
---|
| 78 | |
---|
[6125] | 79 | # Local Roles |
---|
[10226] | 80 | class ApplicationsManager(grok.Role): |
---|
| 81 | grok.name('waeup.local.ApplicationsManager') |
---|
| 82 | grok.title(u'Applications Manager') |
---|
| 83 | grok.permissions('waeup.viewAcademics') |
---|
| 84 | |
---|
[7185] | 85 | class DepartmentManager(grok.Role): |
---|
| 86 | grok.name('waeup.local.DepartmentManager') |
---|
| 87 | grok.title(u'Department Manager') |
---|
[8367] | 88 | grok.permissions('waeup.manageAcademics','waeup.showStudents') |
---|
[6142] | 89 | |
---|
[6655] | 90 | class ClearanceOfficer(grok.Role): |
---|
[7168] | 91 | """The clearance officer role is meant for the |
---|
| 92 | assignment of dynamic roles only. |
---|
| 93 | """ |
---|
[6655] | 94 | grok.name('waeup.local.ClearanceOfficer') |
---|
| 95 | grok.title(u'Clearance Officer') |
---|
[7217] | 96 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
[6655] | 97 | |
---|
[8962] | 98 | class UGClearanceOfficer(grok.Role): |
---|
| 99 | """The clearance officer role is meant for the |
---|
| 100 | assignment of dynamic roles only. |
---|
| 101 | """ |
---|
| 102 | grok.name('waeup.local.UGClearanceOfficer') |
---|
| 103 | grok.title(u'UG Clearance Officer') |
---|
| 104 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 105 | |
---|
| 106 | class PGClearanceOfficer(grok.Role): |
---|
| 107 | """The clearance officer role is meant for the |
---|
| 108 | assignment of dynamic roles only. |
---|
| 109 | """ |
---|
| 110 | grok.name('waeup.local.PGClearanceOfficer') |
---|
| 111 | grok.title(u'PG Clearance Officer') |
---|
| 112 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 113 | |
---|
[7334] | 114 | class CourseAdviser100(grok.Role): |
---|
[7335] | 115 | """The 100 level course adviser role is meant for the |
---|
[7168] | 116 | assignment of dynamic roles only. |
---|
| 117 | """ |
---|
[7334] | 118 | grok.name('waeup.local.CourseAdviser100') |
---|
| 119 | grok.title(u'Course Adviser 100L') |
---|
| 120 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
[6655] | 121 | |
---|
[7334] | 122 | class CourseAdviser200(grok.Role): |
---|
[7335] | 123 | """The course 200 level adviser role is meant for the |
---|
[7334] | 124 | assignment of dynamic roles only. |
---|
| 125 | """ |
---|
| 126 | grok.name('waeup.local.CourseAdviser200') |
---|
| 127 | grok.title(u'Course Adviser 200L') |
---|
| 128 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 129 | |
---|
| 130 | class CourseAdviser300(grok.Role): |
---|
[7335] | 131 | """The 300 level course adviser role is meant for the |
---|
[7334] | 132 | assignment of dynamic roles only. |
---|
| 133 | """ |
---|
| 134 | grok.name('waeup.local.CourseAdviser300') |
---|
| 135 | grok.title(u'Course Adviser 300L') |
---|
| 136 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 137 | |
---|
| 138 | class CourseAdviser400(grok.Role): |
---|
[7335] | 139 | """The 400 level course adviser role is meant for the |
---|
[7334] | 140 | assignment of dynamic roles only. |
---|
| 141 | """ |
---|
| 142 | grok.name('waeup.local.CourseAdviser400') |
---|
| 143 | grok.title(u'Course Adviser 400L') |
---|
| 144 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 145 | |
---|
| 146 | class CourseAdviser500(grok.Role): |
---|
[7335] | 147 | """The 500 level course adviser role is meant for the |
---|
[7334] | 148 | assignment of dynamic roles only. |
---|
| 149 | """ |
---|
| 150 | grok.name('waeup.local.CourseAdviser500') |
---|
| 151 | grok.title(u'Course Adviser 500L') |
---|
| 152 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 153 | |
---|
| 154 | class CourseAdviser600(grok.Role): |
---|
[7335] | 155 | """The 600 level course adviser role is meant for the |
---|
[7334] | 156 | assignment of dynamic roles only. |
---|
| 157 | """ |
---|
| 158 | grok.name('waeup.local.CourseAdviser600') |
---|
| 159 | grok.title(u'Course Adviser 600L') |
---|
| 160 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 161 | |
---|
[10064] | 162 | class CourseAdviser700(grok.Role): |
---|
| 163 | """The 700 level course adviser role is meant for the |
---|
| 164 | assignment of dynamic roles only. |
---|
| 165 | """ |
---|
| 166 | grok.name('waeup.local.CourseAdviser700') |
---|
| 167 | grok.title(u'Course Adviser 700L') |
---|
| 168 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 169 | |
---|
| 170 | class CourseAdviser800(grok.Role): |
---|
| 171 | """The 800 level course adviser role is meant for the |
---|
| 172 | assignment of dynamic roles only. |
---|
| 173 | """ |
---|
| 174 | grok.name('waeup.local.CourseAdviser800') |
---|
| 175 | grok.title(u'Course Adviser 800L') |
---|
| 176 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 177 | |
---|
[9002] | 178 | class Lecturer(grok.Role): |
---|
| 179 | """The lecturer role is meant for the |
---|
| 180 | assignment of dynamic roles only. |
---|
| 181 | """ |
---|
| 182 | grok.name('waeup.local.Lecturer') |
---|
| 183 | grok.title(u'Lecturer') |
---|
| 184 | grok.permissions('waeup.showStudents', 'waeup.viewAcademics') |
---|
| 185 | |
---|
[7163] | 186 | class Owner(grok.Role): |
---|
| 187 | grok.name('waeup.local.Owner') |
---|
| 188 | grok.title(u'Owner') |
---|
| 189 | grok.permissions('waeup.editUser') |
---|
| 190 | |
---|
[7178] | 191 | # Site Roles |
---|
[7185] | 192 | class AcademicsOfficer(grok.Role): |
---|
| 193 | grok.name('waeup.AcademicsOfficer') |
---|
[7188] | 194 | grok.title(u'Academics Officer (view only)') |
---|
[7184] | 195 | grok.permissions('waeup.viewAcademics') |
---|
[3521] | 196 | |
---|
[8367] | 197 | class AcademicsManager(grok.Role): |
---|
| 198 | grok.name('waeup.AcademicsManager') |
---|
| 199 | grok.title(u'Academics Manager') |
---|
| 200 | grok.permissions('waeup.viewAcademics', |
---|
| 201 | 'waeup.manageAcademics') |
---|
| 202 | |
---|
[7181] | 203 | class ACManager(grok.Role): |
---|
| 204 | grok.name('waeup.ACManager') |
---|
| 205 | grok.title(u'Access Code Manager') |
---|
| 206 | grok.permissions('waeup.manageACBatches') |
---|
| 207 | |
---|
[8367] | 208 | class DataCenterManager(grok.Role): |
---|
| 209 | grok.name('waeup.DataCenterManager') |
---|
| 210 | grok.title(u'Datacenter Manager') |
---|
| 211 | grok.permissions('waeup.manageDataCenter') |
---|
| 212 | |
---|
| 213 | class ImportManager(grok.Role): |
---|
| 214 | grok.name('waeup.ImportManager') |
---|
| 215 | grok.title(u'Import Manager') |
---|
| 216 | grok.permissions('waeup.manageDataCenter', |
---|
| 217 | 'waeup.importData') |
---|
| 218 | |
---|
[10177] | 219 | class ExportManager(grok.Role): |
---|
| 220 | grok.name('waeup.ExportManager') |
---|
| 221 | grok.title(u'Export Manager') |
---|
| 222 | grok.permissions('waeup.manageDataCenter', |
---|
| 223 | 'waeup.exportData') |
---|
| 224 | |
---|
[8367] | 225 | class UsersManager(grok.Role): |
---|
| 226 | grok.name('waeup.UsersManager') |
---|
| 227 | grok.title(u'Users Manager') |
---|
[9259] | 228 | grok.permissions('waeup.manageUsers', |
---|
| 229 | 'waeup.editUser') |
---|
[8367] | 230 | |
---|
[9300] | 231 | class WorkflowManager(grok.Role): |
---|
| 232 | grok.name('waeup.WorkflowManager') |
---|
| 233 | grok.title(u'Workflow Manager') |
---|
[9299] | 234 | grok.permissions('waeup.triggerTransition') |
---|
| 235 | |
---|
[4789] | 236 | class PortalManager(grok.Role): |
---|
| 237 | grok.name('waeup.PortalManager') |
---|
[6159] | 238 | grok.title(u'Portal Manager') |
---|
[9259] | 239 | grok.permissions('waeup.managePortal', |
---|
| 240 | 'waeup.manageUsers', |
---|
[8374] | 241 | 'waeup.viewAcademics', 'waeup.manageAcademics', |
---|
[8367] | 242 | 'waeup.manageACBatches', |
---|
[9259] | 243 | 'waeup.manageDataCenter', |
---|
| 244 | 'waeup.importData', |
---|
[10177] | 245 | 'waeup.exportData', |
---|
[7184] | 246 | 'waeup.managePortalConfiguration', 'waeup.viewApplication', |
---|
| 247 | 'waeup.manageApplication', 'waeup.handleApplication', |
---|
[7250] | 248 | 'waeup.viewApplicantsTab', 'waeup.payApplicant', |
---|
[8565] | 249 | 'waeup.viewApplicationStatistics', |
---|
[7250] | 250 | 'waeup.viewStudent', 'waeup.manageStudent', |
---|
| 251 | 'waeup.clearStudent', 'waeup.payStudent', |
---|
| 252 | 'waeup.uploadStudentFile', 'waeup.showStudents', |
---|
[9273] | 253 | 'waeup.triggerTransition', |
---|
[7250] | 254 | 'waeup.viewStudentsContainer','waeup.viewStudentsTab', |
---|
[9186] | 255 | 'waeup.handleAccommodation', |
---|
[7205] | 256 | 'waeup.viewHostels', 'waeup.manageHostels', |
---|
[9335] | 257 | 'waeup.editUser', |
---|
[9637] | 258 | 'waeup.loginAsStudent', |
---|
| 259 | 'waeup.manageReports', |
---|
[9645] | 260 | 'waeup.manageJobs', |
---|
[7240] | 261 | ) |
---|
[4789] | 262 | |
---|
[9259] | 263 | class CCOfficer(grok.Role): |
---|
[9303] | 264 | """This is basically a copy of the the PortalManager class. We exclude some |
---|
[9262] | 265 | 'dangerous' permissions by commenting them out. |
---|
[9259] | 266 | """ |
---|
| 267 | grok.name('waeup.CCOfficer') |
---|
| 268 | grok.title(u'Computer Center Officer') |
---|
| 269 | grok.permissions(#'waeup.managePortal', |
---|
| 270 | #'waeup.manageUsers', |
---|
| 271 | 'waeup.viewAcademics', 'waeup.manageAcademics', |
---|
| 272 | #'waeup.manageACBatches', |
---|
| 273 | 'waeup.manageDataCenter', |
---|
| 274 | #'waeup.importData', |
---|
[10177] | 275 | #'waeup.exportData', |
---|
[9259] | 276 | 'waeup.managePortalConfiguration', 'waeup.viewApplication', |
---|
| 277 | 'waeup.manageApplication', 'waeup.handleApplication', |
---|
| 278 | 'waeup.viewApplicantsTab', 'waeup.payApplicant', |
---|
| 279 | 'waeup.viewApplicationStatistics', |
---|
| 280 | 'waeup.viewStudent', 'waeup.manageStudent', |
---|
| 281 | 'waeup.clearStudent', 'waeup.payStudent', |
---|
| 282 | 'waeup.uploadStudentFile', 'waeup.showStudents', |
---|
[9273] | 283 | #'waeup.triggerTransition', |
---|
[9259] | 284 | 'waeup.viewStudentsContainer','waeup.viewStudentsTab', |
---|
| 285 | 'waeup.handleAccommodation', |
---|
| 286 | 'waeup.viewHostels', 'waeup.manageHostels', |
---|
[9335] | 287 | #'waeup.editUser', |
---|
[9637] | 288 | #'waeup.loginAsStudent', |
---|
| 289 | 'waeup.manageReports', |
---|
[9645] | 290 | #'waeup.manageJobs', |
---|
[9259] | 291 | ) |
---|
| 292 | |
---|
[7186] | 293 | def get_all_roles(): |
---|
[6157] | 294 | """Return a list of tuples ``<ROLE-NAME>, <ROLE>``. |
---|
| 295 | """ |
---|
| 296 | return getUtilitiesFor(IRole) |
---|
| 297 | |
---|
[7186] | 298 | def get_waeup_roles(also_local=False): |
---|
[7819] | 299 | """Get all Kofa roles. |
---|
[6157] | 300 | |
---|
[7819] | 301 | Kofa roles are ordinary roles whose id by convention starts with |
---|
[6157] | 302 | a ``waeup.`` prefix. |
---|
| 303 | |
---|
| 304 | If `also_local` is ``True`` (``False`` by default), also local |
---|
[7819] | 305 | roles are returned. Local Kofa roles are such whose id starts |
---|
[6157] | 306 | with ``waeup.local.`` prefix (this is also a convention). |
---|
| 307 | |
---|
| 308 | Returns a generator of the found roles. |
---|
| 309 | """ |
---|
[7186] | 310 | for name, item in get_all_roles(): |
---|
[6157] | 311 | if not name.startswith('waeup.'): |
---|
[7819] | 312 | # Ignore non-Kofa roles... |
---|
[4789] | 313 | continue |
---|
[6157] | 314 | if not also_local and name.startswith('waeup.local.'): |
---|
| 315 | # Ignore local roles... |
---|
[6045] | 316 | continue |
---|
[6157] | 317 | yield item |
---|
[4789] | 318 | |
---|
[7186] | 319 | def get_waeup_role_names(): |
---|
[7819] | 320 | """Get the ids of all Kofa roles. |
---|
[6157] | 321 | |
---|
[7819] | 322 | See :func:`get_waeup_roles` for what a 'KofaRole' is. |
---|
[6157] | 323 | |
---|
[7819] | 324 | This function returns a sorted list of Kofa role names. |
---|
[6157] | 325 | """ |
---|
[7186] | 326 | return sorted([x.id for x in get_waeup_roles()]) |
---|
[6157] | 327 | |
---|
[6144] | 328 | class LocalRolesAssignable(grok.Adapter): |
---|
| 329 | """Default implementation for `ILocalRolesAssignable`. |
---|
| 330 | |
---|
| 331 | This adapter returns a list for dictionaries for objects for which |
---|
| 332 | we want to know the roles assignable to them locally. |
---|
| 333 | |
---|
| 334 | The returned dicts contain a ``name`` and a ``title`` entry which |
---|
| 335 | give a role (``name``) and a description, for which kind of users |
---|
| 336 | the permission is meant to be used (``title``). |
---|
| 337 | |
---|
| 338 | Having this adapter registered we make sure, that for each normal |
---|
| 339 | object we get a valid `ILocalRolesAssignable` adapter. |
---|
| 340 | |
---|
| 341 | Objects that want to offer certain local roles, can do so by |
---|
[6162] | 342 | setting a (preferably class-) attribute to a list of role ids. |
---|
[6144] | 343 | |
---|
| 344 | You can also define different adapters for different contexts to |
---|
| 345 | have different role lookup mechanisms become available. But in |
---|
| 346 | normal cases it should be sufficient to use this basic adapter. |
---|
| 347 | """ |
---|
| 348 | grok.context(Interface) |
---|
| 349 | grok.provides(ILocalRolesAssignable) |
---|
| 350 | |
---|
| 351 | _roles = [] |
---|
| 352 | |
---|
| 353 | def __init__(self, context): |
---|
| 354 | self.context = context |
---|
[6162] | 355 | role_ids = getattr(context, 'local_roles', self._roles) |
---|
[7186] | 356 | self._roles = [(name, role) for name, role in get_all_roles() |
---|
[6162] | 357 | if name in role_ids] |
---|
[6144] | 358 | return |
---|
| 359 | |
---|
| 360 | def __call__(self): |
---|
| 361 | """Get a list of dictionaries containing ``names`` (the roles to |
---|
| 362 | assign) and ``titles`` (some description of the type of user |
---|
| 363 | to assign each role to). |
---|
| 364 | """ |
---|
[7334] | 365 | list_of_dict = [dict( |
---|
[6162] | 366 | name=name, |
---|
| 367 | title=role.title, |
---|
[6163] | 368 | description=role.description) |
---|
[7334] | 369 | for name, role in self._roles] |
---|
| 370 | return sorted(list_of_dict, key=lambda x: x['name']) |
---|
[6144] | 371 | |
---|
[8774] | 372 | def get_all_users(): |
---|
| 373 | """Get a list of dictionaries. |
---|
| 374 | """ |
---|
| 375 | users = sorted(grok.getSite()['users'].items(), key=lambda x: x[1].title) |
---|
| 376 | for key, val in users: |
---|
| 377 | yield(dict(name=key, val=val)) |
---|
| 378 | |
---|
[6163] | 379 | def get_users_with_local_roles(context): |
---|
| 380 | """Get a list of dicts representing the local roles set for `context`. |
---|
| 381 | |
---|
| 382 | Each dict returns `user_name`, `user_title`, `local_role`, |
---|
| 383 | `local_role_title`, and `setting` for each entry in the local |
---|
| 384 | roles map of the `context` object. |
---|
| 385 | """ |
---|
[6202] | 386 | try: |
---|
| 387 | role_map = IPrincipalRoleMap(context) |
---|
| 388 | except TypeError: |
---|
| 389 | # no map no roles. |
---|
| 390 | raise StopIteration |
---|
[6163] | 391 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 392 | user = grok.getSite()['users'].get(user_name,None) |
---|
[7213] | 393 | user_title = getattr(user, 'title', user_name) |
---|
[10227] | 394 | local_role_title = getattr( |
---|
| 395 | dict(get_all_roles()).get(local_role, None), 'title', None) |
---|
[6163] | 396 | yield dict(user_name = user_name, |
---|
| 397 | user_title = user_title, |
---|
| 398 | local_role = local_role, |
---|
| 399 | local_role_title = local_role_title, |
---|
[9309] | 400 | setting = setting) |
---|
| 401 | |
---|
| 402 | def get_users_with_role(role, context): |
---|
| 403 | """Get a list of dicts representing the usres who have been granted |
---|
| 404 | a role for `context`. |
---|
| 405 | """ |
---|
| 406 | try: |
---|
| 407 | role_map = IPrincipalRoleMap(context) |
---|
| 408 | except TypeError: |
---|
| 409 | # no map no roles. |
---|
| 410 | raise StopIteration |
---|
| 411 | for user_name, setting in role_map.getPrincipalsForRole(role): |
---|
| 412 | user = grok.getSite()['users'].get(user_name,None) |
---|
| 413 | user_title = getattr(user, 'title', user_name) |
---|
| 414 | user_email = getattr(user, 'email', None) |
---|
| 415 | yield dict(user_name = user_name, |
---|
| 416 | user_title = user_title, |
---|
| 417 | user_email = user_email, |
---|
| 418 | setting = setting) |
---|