1 | import grok |
---|
2 | from zope.interface import Interface |
---|
3 | from waeup.sirp.interfaces import ILocalRolesAssignable |
---|
4 | |
---|
5 | class Public(grok.Permission): |
---|
6 | """Everyone-can-do-this-permission. |
---|
7 | |
---|
8 | This permission is meant to be applied to objects/views/pages |
---|
9 | etc., that should be usable/readable by everyone. |
---|
10 | |
---|
11 | We need this to be able to tune default permissions more |
---|
12 | restrictive and open up some dedicated objects like the front |
---|
13 | page. |
---|
14 | """ |
---|
15 | grok.name('waeup.Public') |
---|
16 | |
---|
17 | class Anonymous(grok.Permission): |
---|
18 | """Only-anonymous-can-do-this-permission. |
---|
19 | """ |
---|
20 | grok.name('waeup.Anonymous') |
---|
21 | |
---|
22 | class ViewPermission(grok.Permission): |
---|
23 | grok.name('waeup.View') |
---|
24 | |
---|
25 | class ManageUniversity(grok.Permission): |
---|
26 | grok.name('waeup.manageUniversity') |
---|
27 | |
---|
28 | class ManageUsers(grok.Permission): |
---|
29 | grok.name('waeup.manageUsers') |
---|
30 | |
---|
31 | class ManageDataCenter(grok.Permission): |
---|
32 | grok.name('waeup.manageDataCenter') |
---|
33 | |
---|
34 | class ManagePortalSettings(grok.Permission): |
---|
35 | grok.name('waeup.managePortalSettings') |
---|
36 | |
---|
37 | class ViewStudents(grok.Permission): |
---|
38 | grok.name('waeup.viewStudents') |
---|
39 | |
---|
40 | # Local Roles |
---|
41 | class DepartmentOfficer(grok.Role): |
---|
42 | grok.name('waeup.local.DepartmentOfficer') |
---|
43 | grok.permissions('waeup.manageUniversity','waeup.View', 'waeup.Public') |
---|
44 | |
---|
45 | # Global Roles |
---|
46 | class PortalUser(grok.Role): |
---|
47 | grok.name('waeup.PortalUser') |
---|
48 | grok.permissions('waeup.View', 'waeup.Public') |
---|
49 | |
---|
50 | class PortalManager(grok.Role): |
---|
51 | grok.name('waeup.PortalManager') |
---|
52 | grok.permissions('waeup.manageUniversity', 'waeup.manageUsers', |
---|
53 | 'waeup.View', 'waeup.Public','waeup.manageACBatches', |
---|
54 | 'waeup.manageDataCenter','waeup.managePortalSettings') |
---|
55 | |
---|
56 | def getRoles(): |
---|
57 | app = grok.getSite() |
---|
58 | app = None |
---|
59 | manager = None |
---|
60 | if app is not None: |
---|
61 | from zope.securitypolicy.interfaces import IRolePermissionManager |
---|
62 | manager = IRolePermissionManager(app, None) |
---|
63 | else: |
---|
64 | from zope.securitypolicy.rolepermission import ( |
---|
65 | rolePermissionManager as manager) |
---|
66 | role_permission_map = manager.getRolesAndPermissions() |
---|
67 | result = dict() |
---|
68 | for item in role_permission_map: |
---|
69 | if not item[1].startswith('waeup.'): |
---|
70 | # Ignore non-WAeUP roles... |
---|
71 | continue |
---|
72 | if item[1].startswith('waeup.local.'): |
---|
73 | continue |
---|
74 | result[item[1]] = True |
---|
75 | return sorted(result.keys()) |
---|
76 | |
---|
77 | class LocalRolesAssignable(grok.Adapter): |
---|
78 | """Default implementation for `ILocalRolesAssignable`. |
---|
79 | |
---|
80 | This adapter returns a list for dictionaries for objects for which |
---|
81 | we want to know the roles assignable to them locally. |
---|
82 | |
---|
83 | The returned dicts contain a ``name`` and a ``title`` entry which |
---|
84 | give a role (``name``) and a description, for which kind of users |
---|
85 | the permission is meant to be used (``title``). |
---|
86 | |
---|
87 | Having this adapter registered we make sure, that for each normal |
---|
88 | object we get a valid `ILocalRolesAssignable` adapter. |
---|
89 | |
---|
90 | Objects that want to offer certain local roles, can do so by |
---|
91 | setting a (preferably class-) attribute to a list of dictionaries. |
---|
92 | |
---|
93 | You can also define different adapters for different contexts to |
---|
94 | have different role lookup mechanisms become available. But in |
---|
95 | normal cases it should be sufficient to use this basic adapter. |
---|
96 | """ |
---|
97 | grok.context(Interface) |
---|
98 | grok.provides(ILocalRolesAssignable) |
---|
99 | |
---|
100 | _roles = [] |
---|
101 | |
---|
102 | def __init__(self, context): |
---|
103 | self.context = context |
---|
104 | self._roles = getattr(context, 'local_roles', self._roles) |
---|
105 | return |
---|
106 | |
---|
107 | def __call__(self): |
---|
108 | """Get a list of dictionaries containing ``names`` (the roles to |
---|
109 | assign) and ``titles`` (some description of the type of user |
---|
110 | to assign each role to). |
---|
111 | """ |
---|
112 | return self._roles |
---|
113 | |
---|
114 | def roles(self): |
---|
115 | """Return a list of roles assignable to the context object. |
---|
116 | """ |
---|
117 | return [x['name'] for x in self._roles] |
---|