source: main/waeup.kofa/trunk/src/waeup/kofa/permissions.py @ 10436

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

CCOfficer is a baseclass and only necessary in custom packages.

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