Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Some common utilities.
00014 
00015 $Id: utils.py 68156 2006-05-16 19:40:59Z yuppie $
00016 """
00017 
00018 import os
00019 
00020 from AccessControl import ModuleSecurityInfo
00021 from AccessControl.Permission import Permission
00022 from AccessControl.Role import gather_permissions
00023 from App.Common import package_home
00024 from zope.i18nmessageid import MessageFactory
00025 
00026 security = ModuleSecurityInfo('Products.DCWorkflow.utils')
00027 
00028 _dtmldir = os.path.join( package_home( globals() ), 'dtml' )
00029 _xmldir = os.path.join( package_home( globals() ), 'xml' )
00030 
00031 
00032 def ac_inherited_permissions(ob, all=0):
00033     # Get all permissions not defined in ourself that are inherited
00034     # This will be a sequence of tuples with a name as the first item and
00035     # an empty tuple as the second.
00036     d = {}
00037     perms = getattr(ob, '__ac_permissions__', ())
00038     for p in perms: d[p[0]] = None
00039     r = gather_permissions(ob.__class__, [], d)
00040     if all:
00041        if hasattr(ob, '_subobject_permissions'):
00042            for p in ob._subobject_permissions():
00043                pname=p[0]
00044                if not d.has_key(pname):
00045                    d[pname]=1
00046                    r.append(p)
00047        r = list(perms) + r
00048     return r
00049 
00050 def modifyRolesForPermission(ob, pname, roles):
00051     '''
00052     Modifies multiple role to permission mappings.  roles is a list to
00053     acquire, a tuple to not acquire.
00054     '''
00055     # This mimics what AccessControl/Role.py does.
00056     data = ()
00057     for perm in ac_inherited_permissions(ob, 1):
00058         name, value = perm[:2]
00059         if name == pname:
00060             data = value
00061             break
00062     p = Permission(pname, data, ob)
00063     if p.getRoles() != roles:
00064         p.setRoles(roles)
00065         return 1
00066     return 0
00067 
00068 def modifyRolesForGroup(ob, group, grant_roles, managed_roles):
00069     """Modifies local roles for one group.
00070     """
00071     local_roles = getattr(ob, '__ac_local_roles__', None)
00072     if local_roles is None:
00073         local_roles = {}
00074     roles = local_roles.get(group)
00075     if not roles:
00076         if not grant_roles:
00077             # No roles exist and no grants requested.  Leave unchanged.
00078             return 0
00079         else:
00080             # Add new roles for this group.
00081             local_roles[group] = list(grant_roles)
00082             ob.__ac_local_roles__ = local_roles
00083             return 1
00084     # Edit the roles.
00085     roles = list(roles)
00086     changed = 0
00087     for role in managed_roles:
00088         if role in grant_roles and role not in roles:
00089             # Add one role for this group.
00090             roles.append(role)
00091             changed = 1
00092         elif role not in grant_roles and role in roles:
00093             # Remove one role for this group.
00094             roles.remove(role)
00095             changed = 1
00096     if changed:
00097         if not roles and local_roles.has_key(group):
00098             del local_roles[group]
00099         else:
00100             local_roles[group] = roles
00101         ob.__ac_local_roles__ = local_roles
00102     return changed
00103 
00104 security.declarePublic('Message')
00105 Message = _ = MessageFactory('cmf_default')