<p>Thankyou for pointing that out Uli, I knew root['myapp'].getSiteManager()... must have been a bit hackish even if outside the webapp context.</p>
<p>I will change my offline code to use the setSite method.</p>
<div class="gmail_quote">On Jan 30, 2012 9:43 AM, "Uli Fouquet" <<a href="mailto:uli@gnufix.de">uli@gnufix.de</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Matt,<br>
<br>
Am Montag, den 30.01.2012, 00:25 +0000 schrieb Matt Rutherford:<br>
> I implemented (with a few edits for latest Grok version) my own PAU as<br>
> a LocalUtility according to the end of Chapter 7 in the Grok web dev<br>
> book. I'd like to base my app model around the user Account object<br>
> detailed here, which is stored in the UserFolder container object that<br>
> itself is an attribute of my authenticator plugin LocalUtility. But<br>
> looking for these objects in the ZODB I cannot find it under my<br>
> toplevel App object. I can see the<br>
> 'grokcore.site.directive.local_utility' attribute and from this can<br>
> get a grokcore.site.directive.LocalUtilityInfo object which gives some<br>
> properties of the authenticator plugin that I want to access but not<br>
> the underlying model.<br>
<br>
These grokcore.site... attributes are normally only helpers for grok to<br>
setup local utils when you add a grok.Application (aka<br>
grokcore.site.Site) instance to your ZODB. You shouldn't have to fiddle<br>
around with them under normal circumstances.<br>
<br>
During a request (i.e. from inside a view/viewlet/etc.) you can then do<br>
something like this (I will start with looking up a PAU as this is often<br>
asked)::<br>
<br>
from zope.component import getUtility<br>
from zope.authentication.interfaces import IAuthentication<br>
mypau = getUtility(IAuthentication)<br>
<br>
This works, because during a request the Zope publisher sets the 'site'<br>
according to the requested object.<br>
<br>
Sample: when requesting '<a href="http://localhost:8080/myapp/item/@@index" target="_blank">http://localhost:8080/myapp/item/@@index</a>', for<br>
this request 'myapp' will be set as site if it is a grok.Application or<br>
grokcore.site.Site instance.<br>
<br>
If you then ask for a utility, the machinery will first lookup 'local'<br>
ones (i.e. ones registered with your site or application) and only<br>
proceed to the global registry if it cannot find the requested<br>
utility/adapter in the site.<br>
<br>
> How should I access my authenticator plugin and it's UserFolder<br>
> attribute from my bin/python-console prompt?<br>
<br>
If you're not 'in a site' (for instance in the console or in a test),<br>
you can also set the site manually and then proceed in the usual way::<br>
<br>
from zope.component import getUtility<br>
from zope.component.hooks import setSite<br>
from zope.authentication.interfaces import IAuthentication<br>
setSite(myapp)<br>
mypau = getUtility(IAuthentication)<br>
<br>
where 'myapp' of course has to be a grok.Application or Site instance<br>
_already stored in the ZODB_. In the console something like::<br>
<br>
myapp = root['myapp']<br>
<br>
will give you that (if you created some application called 'myapp'<br>
before).<br>
<br>
In tests you can normally do the same except that here you normally have<br>
to create the Application instance first and store it in the ZODB::<br>
<br>
myapp = MyApp()<br>
getRootFolder()['myapp'] = MyApp()<br>
myapp = getRootFolder()['myapp']<br>
setSite(myapp)<br>
mypau = getUtility(IAuthentication)<br>
<br>
All that was about PluggableAuthenticationUtilities (PAUs). But you can<br>
retrieve all locally registered authenticators and the like accordingly.<br>
Just change the interface and additionally ask for a name::<br>
<br>
from zope.component import getUtility<br>
from zope.component.hooks import setSite<br>
from zope.pluggableauth.interfaces import IAuthenticatorPlugin<br>
setSite(myapp)<br>
myauthplugin = getUtility(IAuthenticatorPlugin, name='users')<br>
<br>
This should give you the local authenticator, given it was registered<br>
locally (not globally) under the name ``users`` (see the <a href="http://grok.name" target="_blank">grok.name</a>()<br>
directive in your authenticator definition).<br>
<br>
The other samples above should work with authenticators and similar as<br>
well when passing the correct interface and name. The trick, overall, is<br>
to set the site correctly before doing the utility lookup.<br>
<br>
Hope that helps,<br>
<br>
--<br>
Uli<br>
<br>
<br>
</blockquote></div>