简体   繁体   中英

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM