Getting the current user/principalΒΆ

You often might want to get the user currently accessing your page. The best way is to fetch the user (or principal) is via the page’s request. It’s done like this:

self.request.principal

If you don’t have a request at hand (you really should have one) you can use this somewhat hackish workaround:

import zope.security
principal=zope.security.management.getInteraction().participations[0].principal

If you are using Dolmen and menhir.contenttype.user you can use code like this to get the menhir.contenttype.user.User object:

def getCurrentUser(request):
    # todo: could use principal.id instead of this magic looking stuff
    return request.principal.__parent__[request.principal.__name__]