Back to index

plone3  3.1.7
ScriptablePlugin.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: ScriptablePlugin
00016 
00017 $Id: ScriptablePlugin.py 70853 2006-10-20 20:03:04Z jens $
00018 """
00019 from sets import Set
00020 from urllib import quote_plus
00021 from OFS.Folder import Folder
00022 from AccessControl import ClassSecurityInfo
00023 from AccessControl.Permissions import manage_users as ManageUsers
00024 from App.class_init import default__class_init__ as InitializeClass
00025 
00026 from zope.interface import Interface
00027 from zope.interface import implementedBy
00028 from zope.interface import providedBy
00029 
00030 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00031 
00032 from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
00033 from Products.PluggableAuthService.utils import directlyProvides
00034 from Products.PluggableAuthService.utils import classImplements
00035 
00036 class IScriptablePlugin(Interface):
00037     """ Marker interface.
00038     """
00039 
00040 import Products
00041 
00042 manage_addScriptablePluginForm = PageTemplateFile(
00043     'www/spAdd', globals(), __name__='manage_addScriptablePluginForm' )
00044 
00045 def addScriptablePlugin( dispatcher, id, title=None, REQUEST=None ):
00046     """ Add a Scriptable Plugin to a Pluggable Auth Service. """
00047 
00048     sp = ScriptablePlugin(id, title)
00049     dispatcher._setObject(sp.getId(), sp)
00050 
00051     if REQUEST is not None:
00052         REQUEST['RESPONSE'].redirect(
00053                                 '%s/manage_workspace'
00054                                 '?manage_tabs_message='
00055                                 'ScriptablePlugin+added.'
00056                             % dispatcher.absolute_url())
00057 
00058 class ScriptablePlugin(Folder, BasePlugin):
00059 
00060     """ Allow users to implement plugin interfaces using script objects.
00061 
00062     o Allowable types include ExternalMethods, Python Scripts, ZSQL Methods,
00063       and DTML methods.
00064 
00065     o Provide UI for creating scripts for known plugin types.
00066     """
00067 
00068     security = ClassSecurityInfo()
00069 
00070     meta_type = 'Scriptable Plugin'
00071 
00072     manage_options = ( ( Folder.manage_options[0], )
00073                      + ( { 'label': 'Interfaces',
00074                            'action': 'manage_editInterfacesForm', }
00075                          ,
00076                        )
00077                      + BasePlugin.manage_options
00078                      )
00079 
00080     security.declareProtected( ManageUsers, 'manage_editInterfacesForm' )
00081     manage_editInterfacesForm = PageTemplateFile(
00082         'www/spEditInterfaces', globals(),
00083         __name__='manage_editInterfacesForm')
00084 
00085     def __creatable_by_emergency_user__( self ):
00086         return 1
00087 
00088     def __init__(self, id, title=None):
00089         self._id = self.id = id
00090         self.title = title
00091 
00092     security.declareProtected( ManageUsers, 'hasMethod')
00093     def hasMethod(self, method_name):
00094         """ Do we implement this method directly?
00095         """
00096         return method_name in self.objectIds()
00097 
00098     security.declarePublic('all_meta_types')
00099     def all_meta_types(self):
00100         """ What objects can be contained here? """
00101         allowed_types = ( 'Script (Python)'
00102                         , 'External Method'
00103                         , 'Z SQL Method'
00104                         , 'DTML Method'
00105                         , 'Page Template'
00106                         )
00107 
00108         return [x for x in Products.meta_types if x['name'] in allowed_types]
00109 
00110     security.declareProtected( ManageUsers, '_delOb' )
00111     def _delOb( self, id ):
00112         """
00113             Override ObjectManager's _delOb to account for removing any
00114             interface assertions the object might implement.
00115         """
00116         myId = self.getId()
00117         pas_instance = self._getPAS()
00118         plugins = pas_instance._getOb( 'plugins' )
00119         curr_interfaces = Set(providedBy(self))
00120 
00121         del_interfaces = Set([x for x in providedBy(self) if id in x.names()])
00122 
00123         for interface in del_interfaces:
00124             if myId in plugins.listPluginIds( interface ):
00125                 plugins.deactivatePlugin( interface, myId )
00126 
00127         delattr( self, id )
00128 
00129         directlyProvides( self, *(list(curr_interfaces - del_interfaces)) )
00130 
00131     security.declareProtected( ManageUsers, 'manage_updateInterfaces' )
00132     def manage_updateInterfaces( self, interfaces, RESPONSE=None ):
00133         """ For ZMI update of interfaces. """
00134 
00135         pas_instance = self._getPAS()
00136         plugins = pas_instance._getOb( 'plugins' )
00137 
00138         new_interfaces = []
00139 
00140         for interface in interfaces:
00141             new_interfaces.append( plugins._getInterfaceFromName( interface ) )
00142 
00143         klass_interfaces = [x for x in implementedBy(ScriptablePlugin)]
00144         directlyProvides( self, *(klass_interfaces + new_interfaces) )
00145 
00146         if RESPONSE is not None:
00147             RESPONSE.redirect('%s/manage_workspace'
00148                               '?manage_tabs_message='
00149                               'Interfaces+updated.'
00150                             % self.absolute_url())
00151 
00152 
00153 classImplements( ScriptablePlugin
00154                , IScriptablePlugin
00155                , *(implementedBy(Folder) + implementedBy(BasePlugin))
00156                )
00157 
00158 InitializeClass(ScriptablePlugin)