Back to index

plone3  3.1.7
BasePlugin.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 """ Class: BasePlugin
00016 
00017 $Id: BasePlugin.py 81792 2007-11-12 21:04:47Z wichert $
00018 """
00019 from OFS.SimpleItem import SimpleItem
00020 from OFS.PropertyManager import PropertyManager
00021 from Acquisition import aq_parent, aq_inner
00022 from AccessControl import ClassSecurityInfo
00023 from App.class_init import default__class_init__ as InitializeClass
00024 from Interface.Implements import flattenInterfaces
00025 
00026 from zope.interface import implementedBy
00027 from zope.interface import providedBy
00028 
00029 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00030 
00031 from Products.PluggableAuthService.utils import classImplements
00032 from Products.PluggableAuthService.permissions import ManageUsers
00033 
00034 class BasePlugin(SimpleItem, PropertyManager):
00035 
00036     """ Base class for all PluggableAuthService Plugins
00037     """
00038 
00039     security = ClassSecurityInfo()
00040 
00041     manage_options = ( ( { 'label': 'Activate',
00042                            'action': 'manage_activateInterfacesForm', }
00043                          ,
00044                        )
00045                      + SimpleItem.manage_options
00046                      + PropertyManager.manage_options
00047                      )
00048 
00049     prefix = ''
00050 
00051     _properties = (
00052         dict(id='prefix', type='string', mode='w',
00053              label='Optional Prefix'),)
00054 
00055     security.declareProtected( ManageUsers, 'manage_activateInterfacesForm' )
00056     manage_activateInterfacesForm = PageTemplateFile(
00057         'www/bpActivateInterfaces', globals(),
00058         __name__='manage_activateInterfacesForm')
00059 
00060     security.declareProtected( ManageUsers, 'listInterfaces' )
00061     def listInterfaces( self ):
00062         """ For ZMI update of interfaces. """
00063 
00064         results = []
00065 
00066         for iface in flattenInterfaces( providedBy( self ) ):
00067             results.append( iface.__name__ )
00068 
00069         return results
00070 
00071     security.declareProtected( ManageUsers, 'testImplements' )
00072     def testImplements( self, interface ):
00073         """ Can't access Interface.providedBy() directly in ZPT. """
00074         return interface.providedBy( self )
00075 
00076     security.declareProtected( ManageUsers, 'manage_activateInterfaces' )
00077     def manage_activateInterfaces( self, interfaces, RESPONSE=None ):
00078         """ For ZMI update of active interfaces. """
00079 
00080         pas_instance = self._getPAS()
00081         plugins = pas_instance._getOb( 'plugins' )
00082 
00083         active_interfaces = []
00084 
00085         for iface_name in interfaces:
00086             active_interfaces.append( plugins._getInterfaceFromName(
00087                                                 iface_name ) )
00088 
00089         pt = plugins._plugin_types
00090         id = self.getId()
00091 
00092         for type in pt:
00093             ids = plugins.listPluginIds( type )
00094             if id not in ids and type in active_interfaces:
00095                 plugins.activatePlugin( type, id ) # turn us on
00096             elif id in ids and type not in active_interfaces:
00097                 plugins.deactivatePlugin( type, id ) # turn us off
00098 
00099         if RESPONSE is not None:
00100             RESPONSE.redirect('%s/manage_workspace'
00101                               '?manage_tabs_message='
00102                               'Interface+activations+updated.'
00103                             % self.absolute_url())
00104 
00105     security.declarePrivate( '_getPAS' )
00106     def _getPAS( self ):
00107         """ Canonical way to get at the PAS instance from a plugin """
00108         return aq_parent( aq_inner( self ) )
00109 
00110 classImplements(BasePlugin, *implementedBy(SimpleItem))
00111 
00112 InitializeClass(BasePlugin)