source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/permissions.py @ 15146

Last change on this file since 15146 was 15134, checked in by Henrik Bettermann, 6 years ago

Add ne permissions and roles.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.7 KB
Line 
1# $Id: permissions.py 15134 2018-09-09 10:36:38Z 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#
18import grok
19from zope.component import getUtilitiesFor
20from zope.interface import Interface
21from zope.securitypolicy.interfaces import IRole, IPrincipalRoleMap
22from waeup.kofa.interfaces import ILocalRolesAssignable
23
24
25class Public(grok.Permission):
26    """The Public or everyone-can-do-this-permission is being applied
27    to views/pages that are used by everyone.
28    """
29    grok.name('waeup.Public')
30
31
32class Anonymous(grok.Permission):
33    """The Anonymous permission is applied to
34    views/pages which are dedicated to anonymous users only.
35    Logged-in users can't access these views.
36    """
37    grok.name('waeup.Anonymous')
38
39
40class Authenticated(grok.Permission):
41    """The Authenticated permission is applied to pages
42    which can only be used by logged-in users and not by anonymous users.
43    """
44    grok.name('waeup.Authenticated')
45
46
47class ViewAcademics(grok.Permission):
48    """The ViewAcademics permission is applied to all
49    views of the Academic Section. Users with this permission can view but
50    not edit content in the Academic Section.
51    """
52    grok.name('waeup.viewAcademics')
53
54
55class ManageAcademics(grok.Permission):
56    """The ManageAcademics permission is applied to all edit/manage
57    pages in the Academic Section. Users who have this permission
58    can change/edit context objects.
59    """
60    grok.name('waeup.manageAcademics')
61
62
63class ManagePortal(grok.Permission):
64    """The ManagePortal permission is used for very few pages
65    (e.g. the DatacenterSettings page). Only PortalManagers have this
66    permission. It is furthermore used to control delete methods of container
67    pages in the Academic Section. The ManageAcademics permission,
68    described above, does enable users to edit content but not to
69    remove sub-containers, like faculties, departments or certificates.
70    Users must have the ManagePortal permission too to remove
71    entire containers.
72    """
73    grok.name('waeup.managePortal')
74
75
76class ManageUsers(grok.Permission):
77    """The ManageUsers permission is a real superuser permission
78    and therefore very 'dangerous'. It allows to add, remove or edit
79    user accounts. Editing a user account includes the option to assign
80    or remove roles. That means that a user with this permission can lock out
81    other users by either removing their account or by removing
82    permissions.
83    """
84    grok.name('waeup.manageUsers')
85
86
87class ShowStudents(grok.Permission):
88    """Users with this permission do not neccessarily see the 'Students' tab
89    but they can search for students at department, certificate or course
90    level. If they additionally have the ExportData permission they can
91    export the data as csv files.
92
93    Bursary or Department Officers don't have the ExportData
94    permission (see Roles section) and are only allowed to export bursary
95    or payments overview data respectively.
96    """
97    grok.name('waeup.showStudents')
98
99
100class ClearAllStudents(grok.Permission):
101    """The ClearAllStudents permission allows to clear all students
102    in a department at one sweep.
103    """
104    grok.name('waeup.clearAllStudents')
105
106
107class EditScores(grok.Permission):
108    """The EditScores permission allows to edit scores in course tickets.
109    """
110    grok.name('waeup.editScores')
111
112
113class TriggerTransition(grok.Permission):
114    """The TriggerTransition permission allows to trigger workflow transitions
115    of student and document objects.
116    """
117    grok.name('waeup.triggerTransition')
118
119
120class EditUser(grok.Permission):
121    """The EditUser permission is required for editing
122    single user accounts.
123    """
124    grok.name('waeup.editUser')
125
126
127class ManageDataCenter(grok.Permission):
128    """The ManageDataCenter permission allows to access all pages
129    in the Data Center and to upload files. It does not automatically
130    allow to process uploaded data files.
131    """
132    grok.name('waeup.manageDataCenter')
133
134
135class ImportData(grok.Permission):
136    """The ImportData permission allows to batch process (import) any kind of
137    portal data except for user data. The User Data processor
138    requires also the ManageUsers permission.
139    """
140    grok.name('waeup.importData')
141
142
143class ExportData(grok.Permission):
144    """The ExportData permission allows to export any kind of portal data.
145    """
146    grok.name('waeup.exportData')
147
148
149class ExportPaymentsOverview(grok.Permission):
150    grok.name('waeup.exportPaymentsOverview')
151
152
153class ExportBursaryData(grok.Permission):
154    grok.name('waeup.exportBursaryData')
155
156
157class ManagePortalConfiguration(grok.Permission):
158    """The ManagePortalConfiguration permission allows to
159    edit global and sessional portal configuration data.
160    """
161    grok.name('waeup.managePortalConfiguration')
162
163
164class ManageACBatches(grok.Permission):
165    """The ManageACBatches permission allows to view and
166    manage accesscodes.
167    """
168    grok.name('waeup.manageACBatches')
169
170
171class PutBiometricDataPermission(grok.Permission):
172    """This permission allows to upload/change biometric data.
173    """
174    grok.name('waeup.putBiometricData')
175
176
177class GetBiometricDataPermission(grok.Permission):
178    """This permission allows to read biometric data.
179    """
180    grok.name('waeup.getBiometricData')
181
182
183# Local Roles
184
185class ApplicationsManager(grok.Role):
186    """The local ApplicationsManager role can be assigned at applicants
187    container and at department level. At department level an Applications
188    Manager can manage all applicants which desire to study a programme
189    offered by the department (1st Choice Course of Study).
190
191    At container level (local) Applications Managers gain permissions which
192    allow to manage the container and all applicants inside the container.  At
193    container level the permission set of this local role corresonds with the
194    permission set of the same-named global role.
195    """
196    grok.name('waeup.local.ApplicationsManager')
197    grok.title(u'Applications Manager')
198    grok.permissions('waeup.viewAcademics',
199                     'waeup.manageApplication', 'waeup.viewApplication',
200                     'waeup.payApplicant')
201
202
203class DepartmentOfficer(grok.Role):
204    """The local DepartmentOfficer role can be assigned at faculty or
205    department level. The role allows to list all student data within the
206    faculty/department the local role is assigned.
207    """
208    grok.name('waeup.local.DepartmentOfficer')
209    grok.title(u'Department Officer')
210    grok.permissions('waeup.showStudents',
211                     'waeup.viewAcademics',
212                     'waeup.exportPaymentsOverview')
213
214
215class DepartmentManager(grok.Role):
216    """The local DepartmentManager role can be assigned at faculty or
217    department level. The role allows to edit all data within this container.
218    It does not automatically allow to remove sub-containers.
219
220    Department Managers (Dean of Faculty or Head of Department respectively)
221    can also list student data but not access student pages.
222    """
223    grok.name('waeup.local.DepartmentManager')
224    grok.title(u'Department Manager')
225    grok.permissions('waeup.manageAcademics',
226                     'waeup.showStudents',
227                     'waeup.exportData')
228
229
230class ClearanceOfficer(grok.Role):
231    """The local ClearanceOfficer role can be assigned at faculty or
232    department level. The role allows to list or export all student
233    data within the faculty/department the local role is assigned.
234
235    Clearance Officers can furthermore clear all students or reject clearance
236    of all students in their faculty/department. They get the
237    StudentsClearanceOfficer role for this subset of students.
238    """
239    grok.name('waeup.local.ClearanceOfficer')
240    grok.title(u'Clearance Officer')
241    grok.permissions('waeup.showStudents',
242                     'waeup.viewAcademics',
243                     'waeup.exportData',
244                     'waeup.clearAllStudents')
245
246
247class LocalStudentsManager(grok.Role):
248    """The local LocalStudentsManager role can be assigned at faculty or
249    department level. The role allows to view all data and to view or export
250    all student data within the faculty/department the local role is assigned.
251
252    Local Students Managers can furthermore manage data of students
253    in their faculty/department. They get the StudentsManager role for
254    this subset of students.
255    """
256    grok.name('waeup.local.LocalStudentsManager')
257    grok.title(u'Students Manager')
258    grok.permissions('waeup.showStudents',
259                     'waeup.viewAcademics',
260                     'waeup.exportData')
261
262
263class LocalWorkflowManager(grok.Role):
264    """The local LocalWorkflowManager role can be assigned at faculty level.
265    The role allows to view all data and to list or export
266    all student data within the faculty the local role is assigned.
267
268    Local Workflow Managers can trigger transition of students in their
269    faculty/department. They get the WorkflowManager role for
270    this subset of students.
271    """
272    grok.name('waeup.local.LocalWorkflowManager')
273    grok.title(u'Student Workflow Manager')
274    grok.permissions('waeup.showStudents',
275                     'waeup.viewAcademics',
276                     'waeup.exportData')
277
278
279class UGClearanceOfficer(grok.Role):
280    """UG Clearance Officers are regular Clearance Officers with restricted
281    dynamic permission assignment. They can only access undergraduate
282    students.
283    """
284    grok.name('waeup.local.UGClearanceOfficer')
285    grok.title(u'UG Clearance Officer')
286    grok.permissions('waeup.showStudents',
287                     'waeup.viewAcademics',
288                     'waeup.exportData',
289                     'waeup.clearAllStudents')
290
291
292class PGClearanceOfficer(grok.Role):
293    """PG Clearance Officers are regular Clearance Officers with restricted
294    dynamic permission assignment. They can only access postgraduate
295    students.
296    """
297    grok.name('waeup.local.PGClearanceOfficer')
298    grok.title(u'PG Clearance Officer')
299    grok.permissions('waeup.showStudents',
300                     'waeup.viewAcademics',
301                     'waeup.exportData',
302                     'waeup.clearAllStudents')
303
304
305class CourseAdviser100(grok.Role):
306    """The local CourseAdviser100 role can be assigned at faculty,
307    department or certificate level. The role allows to view all data and
308    to list or export all student data within the faculty, department
309    or certificate the local role is assigned.
310
311    Local Course Advisers can validate or reject course lists of students
312    in ther faculty/department/certificate at level 100.
313    They get the StudentsCourseAdviser role for this subset of students.
314    """
315    grok.name('waeup.local.CourseAdviser100')
316    grok.title(u'Course Adviser 100L')
317    grok.permissions('waeup.showStudents',
318                     'waeup.viewAcademics',
319                     'waeup.exportData')
320
321
322class CourseAdviser200(grok.Role):
323    """Same as CourseAdviser100 but for level 200.
324    """
325    grok.name('waeup.local.CourseAdviser200')
326    grok.title(u'Course Adviser 200L')
327    grok.permissions('waeup.showStudents',
328                     'waeup.viewAcademics',
329                     'waeup.exportData')
330
331
332class CourseAdviser300(grok.Role):
333    """Same as CourseAdviser100 but for level 300.
334    """
335    grok.name('waeup.local.CourseAdviser300')
336    grok.title(u'Course Adviser 300L')
337    grok.permissions('waeup.showStudents',
338                     'waeup.viewAcademics',
339                     'waeup.exportData')
340
341
342class CourseAdviser400(grok.Role):
343    """Same as CourseAdviser100 but for level 400.
344    """
345    grok.name('waeup.local.CourseAdviser400')
346    grok.title(u'Course Adviser 400L')
347    grok.permissions('waeup.showStudents',
348                     'waeup.viewAcademics',
349                     'waeup.exportData')
350
351
352class CourseAdviser500(grok.Role):
353    """Same as CourseAdviser100 but for level 500.
354    """
355    grok.name('waeup.local.CourseAdviser500')
356    grok.title(u'Course Adviser 500L')
357    grok.permissions('waeup.showStudents',
358                     'waeup.viewAcademics',
359                     'waeup.exportData')
360
361
362class CourseAdviser600(grok.Role):
363    """Same as CourseAdviser100 but for level 600.
364    """
365    grok.name('waeup.local.CourseAdviser600')
366    grok.title(u'Course Adviser 600L')
367    grok.permissions('waeup.showStudents',
368                     'waeup.viewAcademics',
369                     'waeup.exportData')
370
371
372class CourseAdviser700(grok.Role):
373    """Same as CourseAdviser100 but for level 700.
374    """
375    grok.name('waeup.local.CourseAdviser700')
376    grok.title(u'Course Adviser 700L')
377    grok.permissions('waeup.showStudents',
378                     'waeup.viewAcademics',
379                     'waeup.exportData')
380
381
382class CourseAdviser800(grok.Role):
383    """Same as CourseAdviser100 but for level 800.
384    """
385    grok.name('waeup.local.CourseAdviser800')
386    grok.title(u'Course Adviser 800L')
387    grok.permissions('waeup.showStudents',
388                     'waeup.viewAcademics',
389                     'waeup.exportData')
390
391
392class LocalTranscriptOfficer(grok.Role):
393    """The LocalTranscriptOfficer role can be assigned at faculty and
394    department level. The role allows to view, to validate and to
395    release student transcripts.
396    """
397    grok.name('waeup.local.TranscriptOfficer')
398    grok.title(u'Transcript Officer')
399    grok.permissions('waeup.viewAcademics',
400                     'waeup.viewTranscript',
401                     'waeup.processTranscript',
402                     'waeup.viewStudent',
403                     'waeup.viewStudentsContainer',
404                     )
405
406
407class LocalTranscriptSignee(grok.Role):
408    """The LocalTranscriptSignee role can be assigned at faculty and
409    department level. The role allows to view and to sign student transcripts.
410    """
411    grok.name('waeup.local.TranscriptSignee')
412    grok.title(u'Transcript Signee')
413    grok.permissions('waeup.viewAcademics',
414                     'waeup.viewTranscript',
415                     'waeup.signTranscript',
416                     'waeup.viewStudent',
417                     'waeup.viewStudentsContainer',
418                     )
419
420
421class Lecturer(grok.Role):
422    """The local Lecturer role can be assigned at course level.
423    The role allows to export some student
424    data within the course the local role is assigned. Lecturers can't access
425    student data directly but they can edit the scores in course tickets.
426    """
427    grok.name('waeup.local.Lecturer')
428    grok.title(u'Lecturer')
429    grok.permissions('waeup.editScores',
430                     'waeup.viewAcademics',
431                     'waeup.exportData')
432
433
434class Owner(grok.Role):
435    """Each user 'owns' her/his user object and gains permission to edit
436    some of the user attributes.
437    """
438    grok.name('waeup.local.Owner')
439    grok.title(u'Owner')
440    grok.permissions('waeup.editUser')
441
442
443# Site Roles
444class AcademicsOfficer(grok.Role):
445    """An Academics Officer can view but not edit data in the
446    academic section.
447
448    This is the default role which is automatically assigned to all
449    officers of the portal. A user with this role can access all display pages
450    at faculty, department, course, certificate and certificate course level.
451    """
452    grok.name('waeup.AcademicsOfficer')
453    grok.title(u'Academics Officer (view only)')
454    grok.permissions('waeup.viewAcademics')
455
456
457class AcademicsManager(grok.Role):
458    """An Academics Manager can view and edit all data in the
459    scademic section, i.e. access all manage pages
460    at faculty, department, course, certificate and certificate course level.
461    """
462    grok.name('waeup.AcademicsManager')
463    grok.title(u'Academics Manager')
464    title = u'Academics Manager'
465    grok.permissions('waeup.viewAcademics',
466                     'waeup.manageAcademics')
467
468
469class ACManager(grok.Role):
470    """This is the role for Access Code Managers.
471    An AC Manager can view and manage the Accesscodes Section, see
472    ManageACBatches permission above.
473    """
474    grok.name('waeup.ACManager')
475    grok.title(u'Access Code Manager')
476    grok.permissions('waeup.manageACBatches')
477
478
479class DataCenterManager(grok.Role):
480    """This single-permission role is dedicated to those users
481    who are charged with batch processing of portal data.
482    A Data Center Manager can access all pages in the Data Center,
483    see ManageDataCenter permission above.
484    """
485    grok.name('waeup.DataCenterManager')
486    grok.title(u'Datacenter Manager')
487    grok.permissions('waeup.manageDataCenter')
488
489
490class ImportManager(grok.Role):
491    """An Import Manager is a Data Center Manager who is also allowed
492    to batch process (import) data. All batch processors (importers) are
493    available except for the User Processor. This processor requires the
494    Users Manager role too. The ImportManager role includes the
495    DataCenterManager role but not vice versa.
496    """
497    grok.name('waeup.ImportManager')
498    grok.title(u'Import Manager')
499    grok.permissions('waeup.manageDataCenter',
500                     'waeup.importData')
501
502
503class ExportManager(grok.Role):
504    """An Export Manager is a Data Center Manager who is also allowed
505    to export all kind of portal data. The ExportManager role includes the
506    DataCenterManager role but not vice versa.
507    """
508    grok.name('waeup.ExportManager')
509    grok.title(u'Export Manager')
510    grok.permissions('waeup.manageDataCenter',
511                     'waeup.exportData',
512                     'waeup.showStudents')
513
514
515class BursaryOfficer(grok.Role):
516    """Bursary Officers can export bursary data. They can't access the
517    Data Center but see student data export buttons in the Academic Section.
518    """
519    grok.name('waeup.BursaryOfficer')
520    grok.title(u'Bursary Officer')
521    grok.permissions('waeup.showStudents',
522                     'waeup.viewAcademics',
523                     'waeup.exportBursaryData')
524
525
526class UsersManager(grok.Role):
527    """A Users Manager can add, remove or edit
528    user accounts, see ManageUsers permission for further information.
529    Be very careful with this role.
530    """
531    grok.name('waeup.UsersManager')
532    grok.title(u'Users Manager')
533    grok.permissions('waeup.manageUsers',
534                     'waeup.editUser')
535
536
537class WorkflowManager(grok.Role):
538    """The Workflow Manager can trigger workflow transitions
539    of student and document objects, see TriggerTransition permission
540    for further information.
541    """
542    grok.name('waeup.WorkflowManager')
543    grok.title(u'Workflow Manager')
544    grok.permissions('waeup.triggerTransition')
545
546
547class FingerprintReaderDeviceRole(grok.Role):
548    """Fingerprint Reader Devices.
549
550    Fingerprint readers are remote devices that can store and retrieve
551    fingerprint data.
552    """
553    grok.name('waeup.FingerprintDevice')
554    grok.title(u'Fingerprint Reader')
555    grok.permissions(
556        'waeup.getBiometricData',
557        'waeup.putBiometricData',
558    )
559
560
561class PortalManager(grok.Role):
562    """The PortalManager role is the maximum set of Kofa permissions
563    which are needed to manage the entire portal. This set must not
564    be customized. It is recommended to assign this role only
565    to a few certified Kofa administrators.
566    A less dangerous manager role is the CCOfficer role described below.
567    For the most tasks the CCOfficer role is sufficient.
568    """
569    grok.name('waeup.PortalManager')
570    grok.title(u'Portal Manager')
571    grok.permissions('waeup.managePortal',
572                     'waeup.manageUsers',
573                     'waeup.viewAcademics', 'waeup.manageAcademics',
574                     'waeup.manageACBatches',
575                     'waeup.manageDataCenter',
576                     'waeup.importData',
577                     'waeup.exportData',
578                     'waeup.viewTranscript',
579                     'waeup.processTranscript',
580                     'waeup.viewDocuments', 'waeup.manageDocuments',
581                     'waeup.managePortalConfiguration',
582                     'waeup.viewApplication',
583                     'waeup.manageApplication', 'waeup.handleApplication',
584                     'waeup.viewApplicantsTab', 'waeup.payApplicant',
585                     'waeup.viewApplicationStatistics',
586                     'waeup.viewStudent', 'waeup.manageStudent',
587                     'waeup.clearStudent', 'waeup.payStudent',
588                     'waeup.clearStudentFinancially',  # not used in base pkg
589                     'waeup.uploadStudentFile', 'waeup.showStudents',
590                     'waeup.clearAllStudents',
591                     'waeup.createStudents',
592                     'waeup.editScores',
593                     'waeup.triggerTransition',
594                     'waeup.validateStudent',
595                     'waeup.viewStudentsContainer',
596                     'waeup.handleAccommodation',
597                     'waeup.viewHostels', 'waeup.manageHostels',
598                     'waeup.editUser',
599                     'waeup.loginAsStudent',
600                     'waeup.handleReports',
601                     'waeup.manageReports',
602                     'waeup.manageJobs',
603                     )
604
605
606class CCOfficer(grok.Role):
607    """The role of the Computer Center Officer is basically a copy
608    of the the PortalManager role. Some 'dangerous' permissions are excluded
609    by commenting them out (see source code). If officers need to gain more
610    access rights than defined in this role, do not hastily switch to the
611    PortalManager role but add further manager roles instead. Additional
612    roles could be: UsersManager, ACManager, ImportManager, WorkflowManager
613    or StudentImpersonator.
614
615    CCOfficer is a base class which means that this role is subject to
616    customization. It is not used in the ``waeup.kofa`` base package.
617    """
618    grok.baseclass()
619    grok.name('waeup.CCOfficer')
620    grok.title(u'Computer Center Officer')
621    grok.permissions(
622        # 'waeup.managePortal',
623        # 'waeup.manageUsers',
624        'waeup.viewAcademics',
625        'waeup.manageAcademics',
626        # 'waeup.manageACBatches',
627        'waeup.manageDataCenter',
628        # 'waeup.importData',
629        'waeup.exportData',
630        'waeup.viewTranscript',
631        'waeup.processTranscript',
632        'waeup.viewDocuments', 'waeup.manageDocuments',
633        'waeup.managePortalConfiguration', 'waeup.viewApplication',
634        'waeup.manageApplication', 'waeup.handleApplication',
635        'waeup.viewApplicantsTab', 'waeup.payApplicant',
636        'waeup.viewApplicationStatistics',
637        'waeup.viewStudent', 'waeup.manageStudent',
638        'waeup.clearStudent', 'waeup.payStudent',
639        'waeup.uploadStudentFile', 'waeup.showStudents',
640        'waeup.clearAllStudents',
641        # 'waeup.createStudents',
642        'waeup.editScores',
643        # 'waeup.triggerTransition',
644        'waeup.validateStudent',
645        'waeup.viewStudentsContainer',
646        'waeup.handleAccommodation',
647        'waeup.viewHostels', 'waeup.manageHostels',
648        # 'waeup.editUser',
649        # 'waeup.loginAsStudent',
650        'waeup.handleReports',
651        'waeup.manageReports',
652        # 'waeup.manageJobs',
653        )
654
655
656def get_all_roles():
657    """Return a list of tuples ``<ROLE-NAME>, <ROLE>``.
658    """
659    return getUtilitiesFor(IRole)
660
661
662def get_waeup_roles(also_local=False):
663    """Get all Kofa roles.
664
665    Kofa roles are ordinary roles whose id by convention starts with
666    a ``waeup.`` prefix.
667
668    If `also_local` is ``True`` (``False`` by default), also local
669    roles are returned. Local Kofa roles are such whose id starts
670    with ``waeup.local.`` prefix (this is also a convention).
671
672    Returns a generator of the found roles.
673    """
674    for name, item in get_all_roles():
675        if not name.startswith('waeup.'):
676            # Ignore non-Kofa roles...
677            continue
678        if not also_local and name.startswith('waeup.local.'):
679            # Ignore local roles...
680            continue
681        yield item
682
683
684def get_waeup_role_names():
685    """Get the ids of all Kofa roles.
686
687    See :func:`get_waeup_roles` for what a 'KofaRole' is.
688
689    This function returns a sorted list of Kofa role names.
690    """
691    return sorted([x.id for x in get_waeup_roles()])
692
693
694class LocalRolesAssignable(grok.Adapter):
695    """Default implementation for `ILocalRolesAssignable`.
696
697    This adapter returns a list for dictionaries for objects for which
698    we want to know the roles assignable to them locally.
699
700    The returned dicts contain a ``name`` and a ``title`` entry which
701    give a role (``name``) and a description, for which kind of users
702    the permission is meant to be used (``title``).
703
704    Having this adapter registered we make sure, that for each normal
705    object we get a valid `ILocalRolesAssignable` adapter.
706
707    Objects that want to offer certain local roles, can do so by
708    setting a (preferably class-) attribute to a list of role ids.
709
710    You can also define different adapters for different contexts to
711    have different role lookup mechanisms become available. But in
712    normal cases it should be sufficient to use this basic adapter.
713    """
714    grok.context(Interface)
715    grok.provides(ILocalRolesAssignable)
716
717    _roles = []
718
719    def __init__(self, context):
720        self.context = context
721        role_ids = getattr(context, 'local_roles', self._roles)
722        self._roles = [(name, role) for name, role in get_all_roles()
723                       if name in role_ids]
724        return
725
726    def __call__(self):
727        """Get a list of dictionaries containing ``names`` (the roles to
728        assign) and ``titles`` (some description of the type of user
729        to assign each role to).
730        """
731        list_of_dict = [dict(
732                name=name,
733                title=role.title,
734                description=role.description)
735                for name, role in self._roles]
736        return sorted(list_of_dict, key=lambda x: x['name'])
737
738
739def get_all_users():
740    """Get a list of dictionaries.
741    """
742    users = sorted(grok.getSite()['users'].items(), key=lambda x: x[1].title)
743    for key, val in users:
744        yield(dict(name=key, val=val))
745
746
747def get_users_with_local_roles(context):
748    """Get a list of dicts representing the local roles set for `context`.
749
750    Each dict returns `user_name`, `user_title`, `local_role`,
751    `local_role_title`, and `setting` for each entry in the local
752    roles map of the `context` object.
753    """
754    try:
755        role_map = IPrincipalRoleMap(context)
756    except TypeError:
757        # no map no roles.
758        raise StopIteration
759    for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
760        user = grok.getSite()['users'].get(user_name, None)
761        user_title = getattr(user, 'title', user_name)
762        local_role_title = getattr(
763            dict(get_all_roles()).get(local_role, None), 'title', None)
764        yield dict(user_name=user_name,
765                   user_title=user_title,
766                   local_role=local_role,
767                   local_role_title=local_role_title,
768                   setting=setting)
769
770
771def get_users_with_role(role, context):
772    """Get a list of dicts representing the usres who have been granted
773    a role for `context`.
774    """
775    try:
776        role_map = IPrincipalRoleMap(context)
777    except TypeError:
778        # no map no roles.
779        raise StopIteration
780    for user_name, setting in role_map.getPrincipalsForRole(role):
781        user = grok.getSite()['users'].get(user_name, None)
782        user_title = getattr(user, 'title', user_name)
783        user_email = getattr(user, 'email', None)
784        yield dict(user_name=user_name,
785                   user_title=user_title,
786                   user_email=user_email,
787                   setting=setting)
Note: See TracBrowser for help on using the repository browser.