Massimo Variolo
Massimo Variolo

Reputation: 4777

get current logged user on Plone

I would like to get the 'location' (an attribute, like username, user id, ...) of current logged user in my plone instance.

To get the current user I've tried:

from AccessControl import getSecurityManager
user = getSecurityManager().getUser()
username = user.getUserName()

But for both user and username I get the string "System Processes"

How can I solve this?

edit

I've tried

from plone import api
user = api.user.get_current()
user.getProperty('location')

but I get:

CannotGetPortalError: Unable to get the portal object.

Upvotes: 2

Views: 1494

Answers (3)

gwarah
gwarah

Reputation: 251

self.context must be called in a class method. In a simple python scritp use just context. This script works

from Products.CMFCore.utils import getToolByName 
mt = getToolByName(context, 'portal_membership') 
if  mt.isAnonymousUser(): 
    member = 'anonymous' 
else:
    member = mt.getAuthenticatedMember() 

print member
return printed

Upvotes: 1

Massimo Variolo
Massimo Variolo

Reputation: 4777

I've found a solution here.

from Products.CMFCore.utils import getToolByName

membership = getToolByName(self.context, 'portal_membership')
authenticated_user = membership.getAuthenticatedMember().getProperty('location') 
print authenticated_user

Upvotes: 2

adamfc
adamfc

Reputation: 178

I would advise using plone.api here. You can do the following:

from plone import api
user = api.user.get_current()
user.getProperty('location')

Upvotes: 3

Related Questions