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

Barry Warsaw barry@wooz.org
Wed, 12 Dec 2001 17:29:55 -0500


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

Added Files:
      Tag: Zope-3x-branch
	RolePermissionMap.py 
Log Message:
The global mapping of roles to permissions.


=== Added File Zope3/lib/python/Zope/App/Security/RolePermissionMap.py ===
# RolePermissionMap.py
#
# Copyright (c) 2001 Zope Coporation and Contributors.  All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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.

"""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


# 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)

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, [])

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.
    """
    assert verify(IRole, role)
    return _byrole.get(role, [])

def _clear(): # Reset, e.g., for unit testing antisepsis
    _bypermission.clear()
    _byrole.clear()