source: main/waeup.ikoba/trunk/src/waeup/ikoba/permissions.py @ 12770

Last change on this file since 12770 was 12765, checked in by Henrik Bettermann, 10 years ago

Add PaymentsManageActionButton?.

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