[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - RolePermissionMap.py:1.1.2.2

Barry Warsaw barry@wooz.org
Thu, 13 Dec 2001 13:02:13 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security
In directory cvs.zope.org:/tmp/cvs-serv10010/lib/python/Zope/App/Security

Modified Files:
      Tag: Zope-3x-branch
	RolePermissionMap.py 
Log Message:
Refactored to use new SecurityMap class.

Got rid of module level functions in favor of exposing the map as a
module global object

Clean up imports


=== Zope3/lib/python/Zope/App/Security/RolePermissionMap.py 1.1.2.1 => 1.1.2.2 ===
 """Mappings between roles and permissions."""
 
-from Zope.App.Security.IPermission import IPermission
-from Zope.App.Security.IRole import IRole
-from Zope.App.Security import PermissionRegistry
-from Zope.App.Security import RoleRegistry
-from Interface.verify import verify
+from Zope.App.Security.SecurityMap import SecurityMap
 
+class RolePermissionMap(SecurityMap):
+    """Mappings between roles and permissions."""
 
-# Key is Permission object, value is list of Role objects
-_bypermission={}
-# Key is Role object, value is list of Permission objects
-_byrole={}
-
-
-def grantPermissionToRole(permission, role):
     """Bind the permission to the role.
 
     permission must be an IPermission
     role must be an IRole
     """
-    assert verify(IPermission, permission)
-    assert verify(IRole, role)
-    _bypermission.setdefault(permission, []).append(role)
-    _byrole.setdefault(role, []).append(permission)
+    grantPermissionToRole = SecurityMap.addCell
 
-def getRolesForPermission(permission):
     """Return the list of roles for the given permission.
 
     permission must be an IPermission.  If no roles have been granted this
     permission, then the empty list is returned.
     """
-    assert verify(IPermission, permission)
-    return _bypermission.get(permission, [])
+    getRolesForPermission = SecurityMap.getColumnsForRow
 
-def getPermissionsForRole(role):
     """Return the list of permissions for the given role.
 
-    role must be an IRole.  If no permissions have been granted to this role,
-    then the empty list is returned.
+    role must be an IRole.  If no permissions have been granted to this
+    role, then the empty list is returned.
     """
-    assert verify(IRole, role)
-    return _byrole.get(role, [])
+    getPermissionsForRole = SecurityMap.getRowsForColumn
+
+
+# Permissions are our rows, and roles are our columns
+map = RolePermissionMap()
 
-def _clear(): # Reset, e.g., for unit testing antisepsis
-    _bypermission.clear()
-    _byrole.clear()