Back to index

plone3  3.1.7
RecursiveGroupsPlugin.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 """ Classes: RecursiveGroupsPlugin
00016 
00017 $Id: RecursiveGroupsPlugin.py 70851 2006-10-20 19:25:56Z jens $
00018 """
00019 from Acquisition import aq_parent
00020 from AccessControl import ClassSecurityInfo
00021 from Globals import InitializeClass
00022 from BTrees.OOBTree import OOBTree
00023 
00024 from zope.interface import Interface
00025 
00026 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00027 
00028 from Products.PluggableAuthService.interfaces.plugins \
00029     import IGroupsPlugin
00030 from Products.PluggableAuthService.PropertiedUser import PropertiedUser
00031 from Products.PluggableAuthService.permissions import ManageGroups
00032 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00033 from Products.PluggableAuthService.utils import classImplements
00034 
00035 class IRecursiveGroupsPlugin(Interface):
00036     """ Marker interface.
00037     """
00038 
00039 manage_addRecursiveGroupsPluginForm = PageTemplateFile(
00040     'www/rgpAdd', globals(), __name__='manage_addRecursiveGroupsPluginForm' )
00041 
00042 def addRecursiveGroupsPlugin( dispatcher, id, title=None, REQUEST=None ):
00043     """ Add a RecursiveGroupsPlugin to a Pluggable Auth Service. """
00044 
00045     rgp = RecursiveGroupsPlugin(id, title)
00046     dispatcher._setObject(rgp.getId(), rgp)
00047 
00048     if REQUEST is not None:
00049         REQUEST['RESPONSE'].redirect(
00050                                 '%s/manage_workspace'
00051                                 '?manage_tabs_message='
00052                                 'RecursiveGroupsPlugin+added.'
00053                             % dispatcher.absolute_url())
00054 
00055 class SimpleGroup:
00056 
00057     def __init__( self, id ):
00058         self._id = id
00059 
00060     def getId( self ):
00061         return self._id
00062 
00063     def getGroups( self ):
00064         return ()
00065 
00066     def _addGroups( self, groups ):
00067         pass
00068 
00069 class RecursiveGroupsPlugin( BasePlugin ):
00070 
00071     """ PAS plugin for recursively flattening a collection of groups
00072     """
00073     meta_type = 'Recursive Groups Plugin'
00074 
00075     security = ClassSecurityInfo()
00076 
00077     def __init__(self, id, title=None):
00078 
00079         self._id = self.id = id
00080         self.title = title
00081 
00082     #
00083     #   IGroupsPlugin implementation
00084     #
00085     security.declarePrivate( 'getGroupsForPrincipal' )
00086     def getGroupsForPrincipal( self, user, request=None ):
00087 
00088         set = list( user.getGroups() )
00089         seen = []
00090         parent = aq_parent( self )
00091 
00092         while set:
00093             test = set.pop(0)
00094             if test in seen:
00095                 continue
00096             seen.append( test )
00097             new_groups = parent._getGroupsForPrincipal(
00098                 PropertiedUser( test ).__of__(parent), 
00099                 ignore_plugins=( self.getId(), ) )
00100             if new_groups:
00101                 set.extend( new_groups )
00102 
00103         return tuple( seen )
00104 
00105 classImplements( RecursiveGroupsPlugin
00106                , IRecursiveGroupsPlugin
00107                , IGroupsPlugin
00108                )
00109 
00110 InitializeClass(RecursiveGroupsPlugin)