[Grok-dev] How to get current principal in an Utility?

Martijn Faassen faassen at startifact.com
Mon Nov 8 11:01:06 EST 2010


On 11/08/2010 04:31 PM, Christian Klinger wrote:
> Am 08.11.10 16:22, schrieb Shrek Zhou:
>>
>> I need to access current logged in principal in an Utility, how to do that?
>> :-(
>
> you can try this:
>
> import zope.security
> principal=zope.security.management.getInteraction().participations[0].principal
>
> But i don't know if it's the clear way todo it?

Unfortunately there is no cleaner way that I know of if you don't have 
the request - you're effectively relying on global request state, and 
that's never very clean. But it probably is fine in many cases.

You could of course send the request into your utility's methods and dig 
up the principal from there.

Regards,

Martijn



More information about the Grok-dev mailing list