Users

Create user

To create a new user, use api.user.create(). If your portal is configured to use emails as usernames, you just need to pass in the email of the new user.

from plone import api
user = api.user.create(email='alice@plone.org')

Otherwise, you also need to pass in the username of the new user.

user = api.user.create(email='jane@plone.org', username='jane')

To set user properties when creating a new user, pass in a properties dict.

properties = dict(
    fullname='Bob',
    location='Munich',
)
user = api.user.create(
    username='bob',
    email='bob@plone.org',
    properties=properties
)

Besides user properties you can also specify a password for the new user. Otherwise a random 8-char alphanumeric password will be generated.

user = api.user.create(
    username='noob',
    email='noob@plone.org',
    password='secret'
)

Get user

You can get a user with api.user.get().

from plone import api
user = api.user.get(username='bob')

Get currently logged-in user

Getting the currently logged-in user is easy with api.user.get_current().

from plone import api
current = api.user.get_current()

Check if current user is anonymous

Sometimes you need to trigger or display some piece of information only for logged-in users. It’s easy to use api.user.is_anonymous() to do a basic check for it.

from plone import api
if not api.user.is_anonymous():
    trigger = False
trigger = True

Get all users

Get all users in your portal with api.user.get_all().

from plone import api
users = api.user.get_all()

Delete user

To delete a user, use api.user.delete() and pass in either the username or the user object you want to delete.

from plone import api
api.user.create(username='unwanted', email='unwanted@example.org')
api.user.delete(username='unwanted')
unwanted = api.user.create(username='unwanted', email='unwanted@example.org')
api.user.delete(user=unwanted)

Check for role

Again on the security aspects, checking if a user has a certain role can be done with api.user.has_role(). If you omit the user parameter, the currently logged-in user will be used.

from plone import api
has_role = api.user.has_role(username='bob', role='Manager')

When user is omitted the current user is used for role lookup.

from plone import api
has_role = api.user.has_role(role='Manager')

Check for permission

Likewise, you can also check if a user has a certain permission with api.user.has_permission(). Omitting the user parameter means the currently logged-in user will be used.

from plone import api

has_perm = api.user.has_permission(
    username='bob',
    permission='Manage portal content',
    object=api.portal.get()
)

When user is omitted the current user is used for the permission check.

from plone import api
has_perm = api.user.has_permission(
    permission='Manage portal content',
    object=api.portal.get()
)

Get groups that user is a member of

Use get_groups, passing in either the username or the user object you want to get groups for.

from plone import api
api.group.add_user(groupname='Reviewers', username='bob')
groups = api.user.get_groups(username='bob')

Project Versions

Table Of Contents

Previous topic

Content

Next topic

Groups

This Page