[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security/Grants/Global - PrincipalPermissionManager.py:1.1 PrincipalRoleManager.py:1.1 RolePermissionManager.py:1.1 __init__.py:1.1 meta.zcml:1.1 metaConfigure.py:1.1

Jim Fulton jim@zope.com
Thu, 20 Jun 2002 11:55:01 -0400


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

Added Files:
	PrincipalPermissionManager.py PrincipalRoleManager.py 
	RolePermissionManager.py __init__.py meta.zcml 
	metaConfigure.py 
Log Message:
implemented:

http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/MergeSecurityIntoZopeNamespace

While I was at it, I couldn't resist implementing a variation of:

http://dev.zope.org/Wikis/DevSite/Projects/ComponentArchitecture/SecurityPackageReorg

which was a lot more work. 



=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/PrincipalPermissionManager.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""Mappings between principals and permissions."""

from Zope.App.Security.IPrincipalPermissionManager \
     import IPrincipalPermissionManager
from Zope.App.Security.Grants.LocalSecurityMap import LocalSecurityMap
from Zope.App.Security.Settings import Allow, Deny, Unset


class PrincipalPermissionManager(LocalSecurityMap):
    """Mappings between principals and permissions."""

    __implements__ = IPrincipalPermissionManager

    def grantPermissionToPrincipal( self, permission_id, principal_id ):
        ''' See the interface IPrincipalPermissionManager '''
        self.addCell( permission_id, principal_id, Allow )

    def denyPermissionToPrincipal( self, permission_id, principal_id ):
        ''' See the interface IPrincipalPermissionManager '''
        self.addCell( permission_id, principal_id, Deny )

    def unsetPermissionForPrincipal( self, permission_id, principal_id ):
        ''' See the interface IPrincipalPermissionManager '''
        self.delCell( permission_id, principal_id )

    def getPrincipalsForPermission( self, permission_id ):
        ''' See the interface IPrincipalPermissionManager '''
        return self.getRow( permission_id )

    def getPermissionsForPrincipal( self, principal_id ):
        ''' See the interface IPrincipalPermissionManager '''
        return self.getCol( principal_id )

    def getSetting( self, permission_id, principal_id ):
        ''' See the interface IPrincipalPermissionManager '''
        return self.getCell( permission_id, principal_id, default=Unset )

    def getPrincipalsAndPermissions( self ):
        ''' See the interface IPrincipalPermissionManager '''
        return self.getAllCells()


# Permissions are our rows, and principals are our columns
principalPermissionManager = PrincipalPermissionManager()


# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
from Zope.Testing.CleanUp import addCleanUp
addCleanUp(principalPermissionManager._clear)
del addCleanUp


=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/PrincipalRoleManager.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""Mappings between principals and roles."""

from Zope.App.Security.Grants.LocalSecurityMap import LocalSecurityMap
from Zope.App.Security.Settings import Assign, Remove, Unset
from Zope.App.Security.IPrincipalRoleManager import IPrincipalRoleManager
from Zope.App.Security.IPrincipalRoleMap import IPrincipalRoleMap



class PrincipalRoleManager(LocalSecurityMap):
    """Mappings between principals and roles."""

    __implements__ = ( IPrincipalRoleManager, IPrincipalRoleMap )

    def assignRoleToPrincipal( self, role_id, principal_id ):
        ''' See the interface IPrincipalRoleManager '''
        self.addCell( role_id, principal_id, Assign )

    def removeRoleFromPrincipal( self, role_id, principal_id ):
        ''' See the interface IPrincipalRoleManager '''
        self.addCell( role_id, principal_id, Remove )

    def unsetRoleForPrincipal( self, role_id, principal_id ):
        ''' See the interface IPrincipalRoleManager '''
        self.delCell( role_id, principal_id )

    def getPrincipalsForRole( self, role_id ):
        ''' See the interface IPrincipalRoleMap '''
        return self.getRow( role_id )

    def getRolesForPrincipal( self, principal_id ):
        ''' See the interface IPrincipalRoleMap '''
        return self.getCol( principal_id )

    def getSetting( self, role_id, principal_id ):
        ''' See the interface IPrincipalRoleMap '''
        return self.getCell( role_id, principal_id, default=Unset )

    def getPrincipalsAndRoles( self ):
        ''' See the interface IPrincipalRoleMap '''
        return self.getAllCells()

# Roles are our rows, and principals are our columns
principalRoleManager = PrincipalRoleManager()

# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
from Zope.Testing.CleanUp import addCleanUp
addCleanUp(principalRoleManager._clear)
del addCleanUp


=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/RolePermissionManager.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
"""Mappings between roles and permissions."""

from Zope.App.Security.Grants.LocalSecurityMap import LocalSecurityMap
from Zope.App.Security.Settings import Allow, Deny
from Zope.App.Security.IRolePermissionManager import IRolePermissionManager


class RolePermissionManager(LocalSecurityMap):
    """Mappings between roles and permissions."""

    __implements__ = IRolePermissionManager

    # Implementation methods for interface
    # Zope.App.Security.IRolePermissionManager

    def grantPermissionToRole( self, permission_id, role_id ):
        '''See interface IRolePermissionMap'''
        self.addCell( permission_id, role_id, Allow )

    def denyPermissionToRole( self, permission_id, role_id ):
        '''See interface IRolePermissionMap'''
        self.addCell( permission_id, role_id, Deny )

    def unsetPermissionFromRole( self, permission_id, role_id ):
        '''See interface IRolePermissionMap'''
        self.delCell( permission_id, role_id )

    def getRolesForPermission( self, permission_id ):
        '''See interface IRolePermissionMap'''
        return self.getRow( permission_id )

    def getPermissionsForRole( self, role_id ):
        '''See interface IRolePermissionMap'''
        return self.getCol( role_id )

    def getSetting( self, permission_id, role_id ):
        '''See interface IRolePermissionMap'''
        return self.getCell( permission_id, role_id )

    def getRolesAndPermissions( self ):
        '''See interface IRolePermissionMap'''
        return self.getAllCells()

# Permissions are our rows, and roles are our columns
rolePermissionManager = RolePermissionManager()

# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
from Zope.Testing.CleanUp import addCleanUp
addCleanUp(rolePermissionManager._clear)
del addCleanUp


=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/__init__.py ===
##############################################################################
#
# Copyright (c) 2002 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.
# 
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: __init__.py,v 1.1 2002/06/20 15:54:59 jim Exp $
"""


=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/meta.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>

  <directives namespace="http://namespaces.zope.org/zope">

    <directive name="grant" attributes="principal permission role"
       handler=".metaConfigure.grant" />

  </directives>

</zopeConfigure>


=== Added File Zope3/lib/python/Zope/App/Security/Grants/Global/metaConfigure.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
# 
##############################################################################
""" Register security related configuration directives.

$Id: metaConfigure.py,v 1.1 2002/06/20 15:54:59 jim Exp $
"""
from RolePermissionManager import rolePermissionManager as role_perm_mgr
from PrincipalPermissionManager import principalPermissionManager \
        as principal_perm_mgr
from PrincipalRoleManager import principalRoleManager as principal_role_mgr
from Zope.Configuration.Action import Action
from Zope.Configuration.Exceptions import ConfigurationError


def grant(_context, principal=None, role=None, permission=None):
    if (  (principal is not None)
        + (role is not None)
        + (permission is not None)
          ) != 2:
        raise ConfigurationError(
            "Exactly two of the principal, role, and permission attributes "
            "must be specified")

    if principal:
        if role:
            return [
                Action(
                discriminator = ('grantRoleToPrincipal', role, principal),
                callable = principal_role_mgr.assignRoleToPrincipal,
                args = (role, principal),
                )
                ]
        if permission:
            return [
                Action(
                discriminator = ('grantPermissionToPrincipal', 
                                 permission,
                                 principal),
                callable = principal_perm_mgr.grantPermissionToPrincipal,
                args = (permission, principal),
                )
                ]
    else:
        return [
            Action(
            discriminator = ('grantPermissionToRole', permission, role),
            callable = role_perm_mgr.grantPermissionToRole,
            args = (permission, role),
            )
            ]