Back to index

plone3  3.1.7
SearchPrincipalsPlugin.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 """ SearchPrincipalsPlugin   Plugin to delegate enumerateUsers
00016                              and enumerateGroups requests to another 
00017                              PluggableAuthService
00018 """
00019 
00020 # General Python imports
00021 import copy, os
00022 from urllib import quote_plus
00023 
00024 # Zope imports
00025 from Acquisition import aq_base
00026 from OFS.Folder import Folder
00027 from Globals import InitializeClass
00028 from AccessControl import ClassSecurityInfo
00029 from AccessControl.SpecialUsers import emergency_user
00030 
00031 from zope.interface import Interface
00032 
00033 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00034 
00035 from Products.PluggableAuthService.interfaces.plugins import \
00036      IUserEnumerationPlugin
00037 from Products.PluggableAuthService.interfaces.plugins import \
00038      IGroupEnumerationPlugin
00039 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00040 from Products.PluggableAuthService.utils import classImplements
00041 
00042 class ISearchPrincipalsPlugin(Interface):
00043     """ Marker interface.
00044     """
00045 
00046 addSearchPrincipalsPluginForm = PageTemplateFile(
00047     'www/sppAdd', globals(), __name__='addSearchPrincipalsPluginForm' )
00048 
00049 
00050 def addSearchPrincipalsPlugin( dispatcher
00051                              , id
00052                              , title=''
00053                              , delegate_path=''
00054                              , REQUEST=None
00055                              ):
00056     """ Factory method to instantiate a SearchPrincipalsPlugin """
00057     spp = SearchPrincipalsPlugin( id, title=title
00058                                 , delegate_path=delegate_path )
00059     dispatcher._setObject(id, spp)
00060 
00061     if REQUEST is not None:
00062         REQUEST.RESPONSE.redirect('%s/manage_main' % dispatcher.absolute_url())
00063 
00064 
00065 class SearchPrincipalsPlugin(BasePlugin):
00066     """ SearchPrincipalsPlugin delegates its enumerateUsers
00067     and enumerateGroups methods to a delegate object
00068     """
00069     security = ClassSecurityInfo()
00070     meta_type = 'Search Principals Plugin'
00071 
00072     _properties = ( { 'id' : 'delegate'
00073                     , 'label' : ' Delegate Path'
00074                     , 'type' : 'string'
00075                     , 'mode' : 'w'
00076                     }
00077                   ,
00078                   )
00079 
00080     def __init__(self, id, title='', delegate_path=''):
00081         """ Initialize a new instance """
00082         self.id = id
00083         self.title = title
00084         self.delegate = delegate_path
00085 
00086     security.declarePrivate('_getDelegate')
00087     def _getDelegate(self):
00088         """ Safely retrieve a PluggableAuthService to work with """
00089         uf = getattr(aq_base(self), 'acl_users', None)
00090 
00091         if uf is None and self.delegate:
00092             uf = self.unrestrictedTraverse(self.delegate)
00093 
00094         return uf
00095 
00096     security.declarePrivate('enumerateUsers')
00097     def enumerateUsers( self
00098                       , id=None
00099                       , login=None
00100                       , exact_match=0
00101                       , sort_by=None
00102                       , max_results=None
00103                       , **kw
00104                       ):
00105         """ see IUserEnumerationPlugin """
00106         acl = self._getDelegate()
00107 
00108         if acl is None:
00109             return ()
00110 
00111         return acl.searchUsers( id=id
00112                               , login=login
00113                               , exact_match=exact_match
00114                               , sort_by=sort_by
00115                               , max_results=max_results
00116                               , **kw )
00117 
00118     security.declarePrivate('enumerateGroups')
00119     def enumerateGroups( self
00120                        , id=None
00121                        , exact_match=0
00122                        , sort_by=None
00123                        , max_results=None
00124                        , **kw
00125                        ):
00126         """ see IGroupEnumerationPlugin """
00127         acl = self._getDelegate()
00128 
00129         if acl is None:
00130             return ()
00131 
00132         return acl.searchGroups( id=id
00133                                , exact_match=exact_match
00134                                , sort_by=sort_by
00135                                , max_results=max_results
00136                                , **kw )
00137 
00138 classImplements( SearchPrincipalsPlugin
00139                , ISearchPrincipalsPlugin
00140                , IUserEnumerationPlugin
00141                , IGroupEnumerationPlugin
00142                )
00143 
00144 InitializeClass(SearchPrincipalsPlugin)