vendor/CMF/1.5.2/DCWorkflow

view utils.py @ 0:e89e53b8c32e

Vendor import of CMF 1.5.2
author fguillaume
date Wed, 20 Jul 2005 16:21:26 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Some common utilities.
15 $Id: utils.py,v 1.10 2004/08/12 15:07:44 jens Exp $
16 """
18 import os
19 from App.Common import package_home
21 _dtmldir = os.path.join( package_home( globals() ), 'dtml' )
23 from AccessControl.Role import gather_permissions
24 from AccessControl.Permission import Permission
25 from Acquisition import aq_base
28 def ac_inherited_permissions(ob, all=0):
29 # Get all permissions not defined in ourself that are inherited
30 # This will be a sequence of tuples with a name as the first item and
31 # an empty tuple as the second.
32 d = {}
33 perms = getattr(ob, '__ac_permissions__', ())
34 for p in perms: d[p[0]] = None
35 r = gather_permissions(ob.__class__, [], d)
36 if all:
37 if hasattr(ob, '_subobject_permissions'):
38 for p in ob._subobject_permissions():
39 pname=p[0]
40 if not d.has_key(pname):
41 d[pname]=1
42 r.append(p)
43 r = list(perms) + r
44 return r
46 def modifyRolesForPermission(ob, pname, roles):
47 '''
48 Modifies multiple role to permission mappings. roles is a list to
49 acquire, a tuple to not acquire.
50 '''
51 # This mimics what AccessControl/Role.py does.
52 data = ()
53 for perm in ac_inherited_permissions(ob, 1):
54 name, value = perm[:2]
55 if name == pname:
56 data = value
57 break
58 p = Permission(pname, data, ob)
59 if p.getRoles() != roles:
60 p.setRoles(roles)
61 return 1
62 return 0
64 def modifyRolesForGroup(ob, group, grant_roles, managed_roles):
65 """Modifies local roles for one group.
66 """
67 local_roles = getattr(ob, '__ac_local_roles__', None)
68 if local_roles is None:
69 local_roles = {}
70 roles = local_roles.get(group)
71 if not roles:
72 if not grant_roles:
73 # No roles exist and no grants requested. Leave unchanged.
74 return 0
75 else:
76 # Add new roles for this group.
77 local_roles[group] = list(grant_roles)
78 ob.__ac_local_roles__ = local_roles
79 return 1
80 # Edit the roles.
81 roles = list(roles)
82 changed = 0
83 for role in managed_roles:
84 if role in grant_roles and role not in roles:
85 # Add one role for this group.
86 roles.append(role)
87 changed = 1
88 elif role not in grant_roles and role in roles:
89 # Remove one role for this group.
90 roles.remove(role)
91 changed = 1
92 if changed:
93 if not roles and local_roles.has_key(group):
94 del local_roles[group]
95 else:
96 local_roles[group] = roles
97 ob.__ac_local_roles__ = local_roles
98 return changed