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

Godefroid Chapelle gotcha at bubblenet.be
Tue Nov 9 09:39:43 EST 2010


Le 08/11/10 17:01, Martijn Faassen a écrit :
> 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.

Which, imo, feels cleaner : one of the roles of the request is to give 
access to the user. If the utility needs access to the logged-in 
principal, I think it needs access to the request.

>
> Regards,
>
> Martijn

-- 
Godefroid Chapelle (aka __gotcha) http://bubblenet.be


More information about the Grok-dev mailing list