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

Tres Seaver tseaver@zope.com
Fri, 8 Feb 2002 15:32:22 -0500


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

Added Files:
      Tag: Zope-3x-branch
	PrincipalRoleView.py 
Log Message:


 - Add first pass at Principal-Role managment component (aka "local
   roles").


=== Added File Zope3/lib/python/Zope/App/Security/PrincipalRoleView.py ===
##############################################################################
#
# Copyright (c) 2001 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
# 
##############################################################################
""" Management view component for principal-role management (Zope2's
    "local roles").

Revision information: $Id: PrincipalRoleView.py,v 1.1.2.1 2002/02/08 20:32:22 tseaver Exp $
"""

import time
from Zope.PageTemplate.PageTemplateFile import PageTemplateFile
from Zope.Publisher.Browser.AttributePublisher import AttributePublisher
from Zope.ComponentArchitecture.ContextDependent import ContextDependent
from Zope.ComponentArchitecture import getService, getAdapter

from Zope.App.Security.IPrincipalRoleManager import IPrincipalRoleManager
from Zope.App.Security.IPrincipalRoleMap import IPrincipalRoleMap

from Zope.App.Security.IPermission import IPermission
from Zope.App.Security.IRole import IRole

class PrincipalRoleView(AttributePublisher, ContextDependent):

    index = PageTemplateFile('pt/principal_role_association.pt')

    def getAllPrincipals(self):

        principals = getattr(self, '_principals', None)

        if principals is None:
            principals = self._principals = getService(
                self.getContext(), 'AuthenticationService'
                ).getPrincipals()

        return principals
    
    def getAllRoles(self):

        roles = getattr(self, '_roles', None)

        if roles is None:
            roles = self._roles = getService(
                self.getContext(), 'RoleService'
                ).getRoles()

        return roles

    def createGrid( self, principals=None, roles=None ):

        if not principals:
            principals = self.listAllPrincipals()

        if not roles:
            roles = self.listAllRoles()

        return PrincipalRoleGrid( principals, roles, self.getContext() )
        
    def action(self, principals, roles, mapping, testing=None):

        for row in mapping:
            pid = row.permission_id
            roles = row.role_ids

        if not testing:
            return self.index( 
                message="Settings changed at %s" % time.ctime(time.time())
                )


class PrincipalRoleGrid:

    def __init__( self, principals, roles, context ):    

        self._principals = principals
        self._roles = roles
        self._grid = {}

        map = getAdapter( context, IPrincipalRoleMap )

        for role in roles:
            for principal in principals:
                setting = map.getSetting( role, principal )
                self._grid[ ( role, principal ) ] = setting

    def principals( self ):
        return self._principals

    def roles( self ):
        return self._roles

    def getValue( self, role, principal ):
        return self._grid[ ( role, principal ) ]

    def listAvailableValues( self ):
        return ( 'Unset', 'Assigned', 'Removed' )