[Zope3-Users] Re: Programatically add plugins to PAU
Andreas Elvers
andreas at work.de
Tue Apr 11 18:26:00 EDT 2006
Hi Florian,
I use events to trigger post initialization of my content object. setUpMyapp is called
by such a subscriber. This example is *not* complete. Maybe its helpful.
To register utilities I use a helper function 'addLocalUtility'.
from zope.app import zapi
from zope.app.container.interfaces import INameChooser
from zope.app.component.interfaces.registration import ActiveStatus
from zope.app.component.interfaces import ISite
from zope.app.utility import UtilityRegistration
def addLocalUtility(site, name, iface, utility, package='default',folderishUtility=None):
"""Add a utility to a site
The utility is added to the package and activated.
If folderishUtility is given, this Folder is treated as SiteManagementFolder
and the utility is added and registered in this SiteManagementFolder.
"""
# preconditions
if not ISite.providedBy(site):
raise TypeError('ISite required.')
# get site manager and site management folder
sitemanager = site.getSiteManager()
if folderishUtility != None:
default = folderishUtility
else:
default = sitemanager[package]
# add utility to site management folder
chooser = INameChooser(default)
folder_name = chooser.chooseName(utility.__name__, utility)
default[folder_name] = utility
# do utility registration
path = zapi.getPath(utility)
registration = UtilityRegistration(name, iface, utility)
key = default.registrationManager.addRegistration(registration)
zapi.traverse(default.registrationManager, key).status = ActiveStatus
return zapi.traverse(sitemanager, path)
This is the setup code called by event subscriber. Should be placed near the Myapp class.
def setUpMyapp(context):
# create a LocalSiteManager
sm = site.LocalSiteManager(context)
# and assign it to Myapp folder
context.setSiteManager(sm)
context.dataBaseConnectionString = context.dataBaseConnectionString
addLocalUtility(context,'psycopg',
IZopeDatabaseAdapter,
PsycopgAdapter(context.dataBaseConnectionString))
addLocalUtility(context,'sessionManager',
ISessionDataContainer,
PersistentSessionDataContainer())
addLocalUtility(context,'Search',
ISearch,
Search())
# adding PluggableAuthentication utility
pluggable = addLocalUtility(context,'',
IAuthentication,
PluggableAuthentication(context.name + '.'))
# adding PlugAuth GroupFolder utility
groupFolder = addLocalUtility(context,'groups',
IAuthenticatorPlugin,
groupfolder.GroupFolder('group.'),
folderishUtility=pluggable)
# adding PlugAuth PrincipalFolder utility
principalFolder = addLocalUtility(context,'principal',
IAuthenticatorPlugin,
PrincipalFolder('principal.'),
folderishUtility=pluggable)
# adding SessionCredentials utility
sessionCredentials = addLocalUtility(context,'sessionCredentials',
ICredentialsPlugin,
SessionCredentialsPlugin(),
folderishUtility=pluggable)
# Plugin setup
pluggable.credentialsPlugins = ('sessionCredentials',)
pluggable.authenticatorPlugins = ('groups', 'principal')
# setup groups
localGroup = groupfolder.GroupInformation("Local user group")
centralGroup = groupfolder.GroupInformation("Central user group")
groupFolder['local'] = localGroup
groupFolder['central'] = centralGroup
# grant roles to groups
prinRoleManager = IPrincipalRoleManager(context)
prinRoleManager.assignRoleToPrincipal(pluggable.prefix+'role.CentralEditor',
pluggable.prefix+'group.central')
# FIXME: set up default users for devlopment purposes only
principalFolder[u'admin'] = InternalPrincipal('admin','xxxxx','admin',
passwordManagerName='SHA1')
principalFolder[u'local'] = InternalPrincipal('local','xxxxx','local',
passwordManagerName='SHA1')
principalFolder[u'central'] = InternalPrincipal('central','xxxxx','central',
passwordManagerName='SHA1')
# setting breakpoint
import pdb; pdb.set_trace()
# FIXME: This does not work. see my previous post 2006/04/11 on zope3.user
#localGroup.principals = [pluggable.prefix+'principal.local',]
#centralGroup.principals = [pluggable.prefix+'principal.central',]
prinRoleManager.assignRoleToPrincipal('zope.ManageContent','principal.admin')
More information about the Zope3-users
mailing list