[CMF-checkins] CVS: CMF/CMFSetup - rolemap.py:1.10
Yvo Schubbe
y.2004_ at wcm-solutions.de
Thu Aug 5 15:14:04 EDT 2004
Update of /cvs-repository/CMF/CMFSetup
In directory cvs.zope.org:/tmp/cvs-serv20351/CMFSetup
Modified Files:
rolemap.py
Log Message:
- refactored rolemap using minidom
- converted the roles attribute to role sub-elements
- updated default profile
=== CMF/CMFSetup/rolemap.py 1.9 => 1.10 ===
--- CMF/CMFSetup/rolemap.py:1.9 Tue Jun 8 16:01:26 2004
+++ CMF/CMFSetup/rolemap.py Thu Aug 5 15:13:33 2004
@@ -1,8 +1,21 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
""" CMFSetup: Role-permission export / import
$Id$
"""
-from xml.sax import parseString
+
+from xml.dom.minidom import parseString as domParseString
from AccessControl import ClassSecurityInfo
from AccessControl.Permission import Permission
@@ -11,7 +24,8 @@
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from permissions import ManagePortal
-from utils import HandlerBase
+from utils import _getNodeAttribute
+from utils import _getNodeAttributeBoolean
from utils import _xmldir
#
@@ -21,7 +35,7 @@
def importRolemap( context ):
- """ Export roles / permission map as an XML file
+ """ Import roles / permission map from an XML file.
o 'context' must implement IImportContext.
@@ -38,7 +52,7 @@
)
o Register via XML:
-
+
<setup-step id="importRolemap"
version="20040518-01"
handler="Products.CMFSetup.rolemap.importRolemap"
@@ -109,7 +123,7 @@
)
o Register via XML:
-
+
<export-script id="exportRolemap"
version="20040518-01"
handler="Products.CMFSetup.rolemap.exportRolemap"
@@ -130,9 +144,9 @@
""" Synthesize XML description of sitewide role-permission settings.
"""
- security = ClassSecurityInfo()
+ security = ClassSecurityInfo()
security.setDefaultAccess( 'allow' )
-
+
def __init__( self, site ):
self._site = site
@@ -155,12 +169,12 @@
o Returns a sqeuence of mappings describing locally-modified
permission / role settings. Keys include:
-
+
'permission' -- the name of the permission
-
+
'acquire' -- a flag indicating whether to acquire roles from the
site's container
-
+
'roles' -- the list of roles which have the permission.
o Do not include permissions which both acquire and which define
@@ -193,47 +207,49 @@
return self._rolemap()
security.declareProtected( ManagePortal, 'parseXML' )
- def parseXML( self, text ):
+ def parseXML( self, xml, encoding=None ):
""" Pseudo API.
"""
- reader = getattr( text, 'read', None )
+ dom = domParseString(xml)
- if reader is not None:
- text = reader()
+ return _extractRolemapNode(dom, encoding)
- parser = _RolemapParser()
- parseString( text, parser )
+InitializeClass( RolemapConfigurator )
- return parser._roles, parser._permissions
-InitializeClass( RolemapConfigurator )
+def _extractRolemapNode(parent, encoding=None):
+
+ rm_node = parent.getElementsByTagName('rolemap')[0]
+ r_node = rm_node.getElementsByTagName('roles')[0]
+ roles = _extractRoleNodes(r_node, encoding)
-class _RolemapParser( HandlerBase ):
+ p_node = rm_node.getElementsByTagName('permissions')[0]
+ permissions = _extractPermissionNodes(p_node, encoding)
- def __init__( self, encoding='latin-1' ):
+ return roles, permissions
- self._encoding = encoding
- self._roles = []
- self._permissions = []
+def _extractPermissionNodes(parent, encoding=None):
- def startElement( self, name, attrs ):
+ result = []
- if name == 'role':
- self._roles.append( self._extract( attrs, 'name' ) )
+ for p_node in parent.getElementsByTagName('permission'):
+ name = _getNodeAttribute(p_node, 'name', encoding)
+ roles = _extractRoleNodes(p_node, encoding)
+ acquire = _getNodeAttributeBoolean(p_node, 'acquire')
+ result.append( { 'name': name,
+ 'roles': roles,
+ 'acquire': acquire } )
- elif name == 'permission':
+ return tuple(result)
- acquire = self._extract( attrs, 'acquire' ).lower()
- acquire = acquire in ( '1', 'true', 'yes' )
+def _extractRoleNodes(parent, encoding=None):
- info = { 'name' : self._extract( attrs, 'name' )
- , 'roles' : self._extract( attrs, 'roles' ).split()
- , 'acquire' : acquire
- }
+ result = []
- self._permissions.append( info )
+ for r_node in parent.getElementsByTagName('role'):
+ value = _getNodeAttribute(r_node, 'name', encoding)
+ result.append(value)
- elif name not in ( 'rolemap', 'permissions', 'roles' ):
- raise ValueError, 'Unknown element: %s' % name
+ return tuple(result)
More information about the CMF-checkins
mailing list