[7193] | 1 | ## $Id: permissions.py 12838 2015-03-31 17:31:34Z 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 |
---|
[11949] | 22 | from waeup.ikoba.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 | """ |
---|
| 31 | grok.name('waeup.Public') |
---|
[6142] | 32 | |
---|
[5433] | 33 | class Anonymous(grok.Permission): |
---|
| 34 | """Only-anonymous-can-do-this-permission. |
---|
[12838] | 35 | |
---|
[5433] | 36 | """ |
---|
[6142] | 37 | grok.name('waeup.Anonymous') |
---|
[4789] | 38 | |
---|
[7184] | 39 | class Authenticated(grok.Permission): |
---|
| 40 | """Only-logged-in-users-can-do-this-permission. |
---|
| 41 | """ |
---|
| 42 | grok.name('waeup.Authenticated') |
---|
[4789] | 43 | |
---|
[12065] | 44 | class ViewProducts(grok.Permission): |
---|
[11947] | 45 | grok.name('waeup.viewProducts') |
---|
[7184] | 46 | |
---|
[12065] | 47 | class ManageProducts(grok.Permission): |
---|
[11947] | 48 | grok.name('waeup.manageProducts') |
---|
[4789] | 49 | |
---|
[8367] | 50 | class ManagePortal(grok.Permission): |
---|
| 51 | grok.name('waeup.managePortal') |
---|
| 52 | |
---|
[4789] | 53 | class ManageUsers(grok.Permission): |
---|
| 54 | grok.name('waeup.manageUsers') |
---|
[6142] | 55 | |
---|
[7163] | 56 | class EditUser(grok.Permission): |
---|
| 57 | grok.name('waeup.editUser') |
---|
| 58 | |
---|
[6127] | 59 | class ManageDataCenter(grok.Permission): |
---|
| 60 | grok.name('waeup.manageDataCenter') |
---|
[6142] | 61 | |
---|
[8367] | 62 | class ImportData(grok.Permission): |
---|
| 63 | grok.name('waeup.importData') |
---|
| 64 | |
---|
[10177] | 65 | class ExportData(grok.Permission): |
---|
| 66 | grok.name('waeup.exportData') |
---|
| 67 | |
---|
[6907] | 68 | class ManagePortalConfiguration(grok.Permission): |
---|
| 69 | grok.name('waeup.managePortalConfiguration') |
---|
[6155] | 70 | |
---|
[12764] | 71 | class ViewPayments(grok.Permission): |
---|
| 72 | grok.name('waeup.viewPayments') |
---|
[7181] | 73 | |
---|
[12764] | 74 | class ManagePayments(grok.Permission): |
---|
| 75 | grok.name('waeup.managePayments') |
---|
| 76 | |
---|
[6125] | 77 | # Local Roles |
---|
[10226] | 78 | |
---|
[7163] | 79 | class Owner(grok.Role): |
---|
| 80 | grok.name('waeup.local.Owner') |
---|
| 81 | grok.title(u'Owner') |
---|
| 82 | grok.permissions('waeup.editUser') |
---|
| 83 | |
---|
[12068] | 84 | class ProductManager(grok.Role): |
---|
| 85 | grok.name('waeup.local.ProductManager') |
---|
| 86 | grok.title(u'Product Manager') |
---|
| 87 | grok.permissions('waeup.manageProducts', |
---|
| 88 | 'waeup.viewProducts', |
---|
| 89 | 'waeup.exportData') |
---|
| 90 | |
---|
[7178] | 91 | # Site Roles |
---|
[11947] | 92 | class ProductsOfficer(grok.Role): |
---|
| 93 | grok.name('waeup.ProductsOfficer') |
---|
| 94 | grok.title(u'Products Officer (view only)') |
---|
| 95 | grok.permissions('waeup.viewProducts') |
---|
[3521] | 96 | |
---|
[12072] | 97 | class ProductsManager(grok.Role): |
---|
[11947] | 98 | grok.name('waeup.ProductsManager') |
---|
| 99 | grok.title(u'Products Manager') |
---|
| 100 | grok.permissions('waeup.viewProducts', |
---|
| 101 | 'waeup.manageProducts') |
---|
[8367] | 102 | |
---|
| 103 | class DataCenterManager(grok.Role): |
---|
| 104 | grok.name('waeup.DataCenterManager') |
---|
| 105 | grok.title(u'Datacenter Manager') |
---|
| 106 | grok.permissions('waeup.manageDataCenter') |
---|
| 107 | |
---|
| 108 | class ImportManager(grok.Role): |
---|
| 109 | grok.name('waeup.ImportManager') |
---|
| 110 | grok.title(u'Import Manager') |
---|
| 111 | grok.permissions('waeup.manageDataCenter', |
---|
| 112 | 'waeup.importData') |
---|
| 113 | |
---|
[10177] | 114 | class ExportManager(grok.Role): |
---|
| 115 | grok.name('waeup.ExportManager') |
---|
| 116 | grok.title(u'Export Manager') |
---|
| 117 | grok.permissions('waeup.manageDataCenter', |
---|
| 118 | 'waeup.exportData') |
---|
| 119 | |
---|
[8367] | 120 | class UsersManager(grok.Role): |
---|
| 121 | grok.name('waeup.UsersManager') |
---|
| 122 | grok.title(u'Users Manager') |
---|
[9259] | 123 | grok.permissions('waeup.manageUsers', |
---|
| 124 | 'waeup.editUser') |
---|
[8367] | 125 | |
---|
[9300] | 126 | class WorkflowManager(grok.Role): |
---|
| 127 | grok.name('waeup.WorkflowManager') |
---|
| 128 | grok.title(u'Workflow Manager') |
---|
[9299] | 129 | grok.permissions('waeup.triggerTransition') |
---|
| 130 | |
---|
[12764] | 131 | class PaymentsOfficer(grok.Role): |
---|
| 132 | grok.name('waeup.PaymentsOfficer') |
---|
| 133 | grok.title(u'Payments Officer (view only)') |
---|
| 134 | grok.permissions('waeup.viewPayments') |
---|
| 135 | |
---|
| 136 | class PaymentsManager(grok.Role): |
---|
| 137 | grok.name('waeup.PaymentsManager') |
---|
| 138 | grok.title(u'Payments Manager') |
---|
| 139 | grok.permissions('waeup.viewPayments', |
---|
| 140 | 'waeup.managePayments') |
---|
| 141 | |
---|
[4789] | 142 | class PortalManager(grok.Role): |
---|
| 143 | grok.name('waeup.PortalManager') |
---|
[6159] | 144 | grok.title(u'Portal Manager') |
---|
[9259] | 145 | grok.permissions('waeup.managePortal', |
---|
| 146 | 'waeup.manageUsers', |
---|
[11947] | 147 | 'waeup.viewProducts', 'waeup.manageProducts', |
---|
[12207] | 148 | 'waeup.viewDocuments', 'waeup.manageDocuments', |
---|
[9259] | 149 | 'waeup.manageDataCenter', |
---|
| 150 | 'waeup.importData', |
---|
[10177] | 151 | 'waeup.exportData', |
---|
[11947] | 152 | 'waeup.managePortalConfiguration', |
---|
[9335] | 153 | 'waeup.editUser', |
---|
[9637] | 154 | 'waeup.manageReports', |
---|
[9645] | 155 | 'waeup.manageJobs', |
---|
[11958] | 156 | 'waeup.viewCustomer', 'waeup.viewCustomers', |
---|
| 157 | 'waeup.manageCustomer', 'waeup.viewCustomersContainer', |
---|
[12347] | 158 | 'waeup.editCustomerDocuments', |
---|
[11967] | 159 | 'waeup.triggerTransition', |
---|
[12090] | 160 | 'waeup.viewCustomersTab', |
---|
[12764] | 161 | 'waeup.editContracts', |
---|
[12765] | 162 | 'waeup.viewPayments', 'waeup.managePayments', |
---|
[7240] | 163 | ) |
---|
[4789] | 164 | |
---|
[9259] | 165 | class CCOfficer(grok.Role): |
---|
[9303] | 166 | """This is basically a copy of the the PortalManager class. We exclude some |
---|
[9262] | 167 | 'dangerous' permissions by commenting them out. |
---|
[9259] | 168 | """ |
---|
[10346] | 169 | grok.baseclass() |
---|
[9259] | 170 | grok.name('waeup.CCOfficer') |
---|
| 171 | grok.title(u'Computer Center Officer') |
---|
| 172 | grok.permissions(#'waeup.managePortal', |
---|
| 173 | #'waeup.manageUsers', |
---|
[11947] | 174 | 'waeup.viewProducts', 'waeup.manageProducts', |
---|
[12207] | 175 | 'waeup.viewDocuments', 'waeup.manageDocuments', |
---|
[9259] | 176 | #'waeup.manageACBatches', |
---|
| 177 | 'waeup.manageDataCenter', |
---|
| 178 | #'waeup.importData', |
---|
[10243] | 179 | 'waeup.exportData', |
---|
[11947] | 180 | 'waeup.managePortalConfiguration', |
---|
[9335] | 181 | #'waeup.editUser', |
---|
[9637] | 182 | 'waeup.manageReports', |
---|
[9645] | 183 | #'waeup.manageJobs', |
---|
[11958] | 184 | 'waeup.viewCustomer', 'waeup.viewCustomers', |
---|
| 185 | 'waeup.manageCustomer', 'waeup.viewCustomersContainer', |
---|
[12347] | 186 | 'waeup.editCustomerDocuments', |
---|
[11967] | 187 | 'waeup.triggerTransition', |
---|
[12090] | 188 | 'waeup.viewCustomersTab', |
---|
[12764] | 189 | 'waeup.editContracts', |
---|
[12765] | 190 | 'waeup.viewPayments', 'waeup.managePayments', |
---|
[9259] | 191 | ) |
---|
| 192 | |
---|
[7186] | 193 | def get_all_roles(): |
---|
[6157] | 194 | """Return a list of tuples ``<ROLE-NAME>, <ROLE>``. |
---|
| 195 | """ |
---|
| 196 | return getUtilitiesFor(IRole) |
---|
| 197 | |
---|
[7186] | 198 | def get_waeup_roles(also_local=False): |
---|
[11949] | 199 | """Get all Ikoba roles. |
---|
[6157] | 200 | |
---|
[11949] | 201 | Ikoba roles are ordinary roles whose id by convention starts with |
---|
[6157] | 202 | a ``waeup.`` prefix. |
---|
| 203 | |
---|
| 204 | If `also_local` is ``True`` (``False`` by default), also local |
---|
[11949] | 205 | roles are returned. Local Ikoba roles are such whose id starts |
---|
[6157] | 206 | with ``waeup.local.`` prefix (this is also a convention). |
---|
| 207 | |
---|
| 208 | Returns a generator of the found roles. |
---|
| 209 | """ |
---|
[7186] | 210 | for name, item in get_all_roles(): |
---|
[6157] | 211 | if not name.startswith('waeup.'): |
---|
[11949] | 212 | # Ignore non-Ikoba roles... |
---|
[4789] | 213 | continue |
---|
[6157] | 214 | if not also_local and name.startswith('waeup.local.'): |
---|
| 215 | # Ignore local roles... |
---|
[6045] | 216 | continue |
---|
[6157] | 217 | yield item |
---|
[4789] | 218 | |
---|
[7186] | 219 | def get_waeup_role_names(): |
---|
[11949] | 220 | """Get the ids of all Ikoba roles. |
---|
[6157] | 221 | |
---|
[11949] | 222 | See :func:`get_waeup_roles` for what a 'IkobaRole' is. |
---|
[6157] | 223 | |
---|
[11949] | 224 | This function returns a sorted list of Ikoba role names. |
---|
[6157] | 225 | """ |
---|
[7186] | 226 | return sorted([x.id for x in get_waeup_roles()]) |
---|
[6157] | 227 | |
---|
[6144] | 228 | class LocalRolesAssignable(grok.Adapter): |
---|
| 229 | """Default implementation for `ILocalRolesAssignable`. |
---|
| 230 | |
---|
| 231 | This adapter returns a list for dictionaries for objects for which |
---|
| 232 | we want to know the roles assignable to them locally. |
---|
| 233 | |
---|
| 234 | The returned dicts contain a ``name`` and a ``title`` entry which |
---|
| 235 | give a role (``name``) and a description, for which kind of users |
---|
| 236 | the permission is meant to be used (``title``). |
---|
| 237 | |
---|
| 238 | Having this adapter registered we make sure, that for each normal |
---|
| 239 | object we get a valid `ILocalRolesAssignable` adapter. |
---|
| 240 | |
---|
| 241 | Objects that want to offer certain local roles, can do so by |
---|
[6162] | 242 | setting a (preferably class-) attribute to a list of role ids. |
---|
[6144] | 243 | |
---|
| 244 | You can also define different adapters for different contexts to |
---|
| 245 | have different role lookup mechanisms become available. But in |
---|
| 246 | normal cases it should be sufficient to use this basic adapter. |
---|
| 247 | """ |
---|
| 248 | grok.context(Interface) |
---|
| 249 | grok.provides(ILocalRolesAssignable) |
---|
| 250 | |
---|
| 251 | _roles = [] |
---|
| 252 | |
---|
| 253 | def __init__(self, context): |
---|
| 254 | self.context = context |
---|
[6162] | 255 | role_ids = getattr(context, 'local_roles', self._roles) |
---|
[7186] | 256 | self._roles = [(name, role) for name, role in get_all_roles() |
---|
[6162] | 257 | if name in role_ids] |
---|
[6144] | 258 | return |
---|
| 259 | |
---|
| 260 | def __call__(self): |
---|
| 261 | """Get a list of dictionaries containing ``names`` (the roles to |
---|
| 262 | assign) and ``titles`` (some description of the type of user |
---|
| 263 | to assign each role to). |
---|
| 264 | """ |
---|
[7334] | 265 | list_of_dict = [dict( |
---|
[6162] | 266 | name=name, |
---|
| 267 | title=role.title, |
---|
[6163] | 268 | description=role.description) |
---|
[7334] | 269 | for name, role in self._roles] |
---|
| 270 | return sorted(list_of_dict, key=lambda x: x['name']) |
---|
[6144] | 271 | |
---|
[8774] | 272 | def get_all_users(): |
---|
| 273 | """Get a list of dictionaries. |
---|
| 274 | """ |
---|
| 275 | users = sorted(grok.getSite()['users'].items(), key=lambda x: x[1].title) |
---|
| 276 | for key, val in users: |
---|
| 277 | yield(dict(name=key, val=val)) |
---|
| 278 | |
---|
[6163] | 279 | def get_users_with_local_roles(context): |
---|
| 280 | """Get a list of dicts representing the local roles set for `context`. |
---|
| 281 | |
---|
| 282 | Each dict returns `user_name`, `user_title`, `local_role`, |
---|
| 283 | `local_role_title`, and `setting` for each entry in the local |
---|
| 284 | roles map of the `context` object. |
---|
| 285 | """ |
---|
[6202] | 286 | try: |
---|
| 287 | role_map = IPrincipalRoleMap(context) |
---|
| 288 | except TypeError: |
---|
| 289 | # no map no roles. |
---|
| 290 | raise StopIteration |
---|
[6163] | 291 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 292 | user = grok.getSite()['users'].get(user_name,None) |
---|
[7213] | 293 | user_title = getattr(user, 'title', user_name) |
---|
[10227] | 294 | local_role_title = getattr( |
---|
| 295 | dict(get_all_roles()).get(local_role, None), 'title', None) |
---|
[6163] | 296 | yield dict(user_name = user_name, |
---|
| 297 | user_title = user_title, |
---|
| 298 | local_role = local_role, |
---|
| 299 | local_role_title = local_role_title, |
---|
[9309] | 300 | setting = setting) |
---|
| 301 | |
---|
| 302 | def get_users_with_role(role, context): |
---|
| 303 | """Get a list of dicts representing the usres who have been granted |
---|
| 304 | a role for `context`. |
---|
| 305 | """ |
---|
| 306 | try: |
---|
| 307 | role_map = IPrincipalRoleMap(context) |
---|
| 308 | except TypeError: |
---|
| 309 | # no map no roles. |
---|
| 310 | raise StopIteration |
---|
| 311 | for user_name, setting in role_map.getPrincipalsForRole(role): |
---|
| 312 | user = grok.getSite()['users'].get(user_name,None) |
---|
| 313 | user_title = getattr(user, 'title', user_name) |
---|
| 314 | user_email = getattr(user, 'email', None) |
---|
| 315 | yield dict(user_name = user_name, |
---|
| 316 | user_title = user_title, |
---|
| 317 | user_email = user_email, |
---|
| 318 | setting = setting) |
---|