WARNING: If you are reading this on GitHub, DON’T! Read it on ReadTheDocs:
references and proper formatting.

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_users().

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

Get group’s users

If you set the groupname parameter, then api.user.get_users() will return only users that are members of this group.

from plone import api
users = api.user.get_users(groupname='staff')

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)

Get user’s roles

The api.user.get_roles() method is used to getting user’s roles. By default it returns site-wide roles.

from plone import api
roles = api.user.get_roles(username='jane')

If you pass in a content object, it will return local roles of the user in that particular context.

from plone import api
portal = api.portal.get()
blog = api.content.create(container=portal, type='Document', id='blog', title='My blog')
roles = api.user.get_roles(username='jane', obj=portal['blog'])

Get user permissions

The api.user.get_permissions() method is used to getting user’s permissions. By default it returns site root permissions.

from plone import api
mike = api.user.create(email='mike@plone.org', username='mike')
permissions = api.user.get_permissions(username='mike')

If you pass in a content object, it will return local permissions of the user in that particular context.

from plone import api
portal = api.portal.get()
folder = api.content.create(container=portal, type='Folder', id='folder_two', title='Folder Two')
permissions = api.user.get_permissions(username='mike', obj=portal['folder_two'])

Grant roles to user

The api.user.grant_roles() allows us to grant a list of roles to the user.

from plone import api
api.user.grant_roles(username='jane',
    roles=['Reviewer', 'SiteAdministrator']
)

If you pass a content object or folder, the roles are granted only on that conext and not site wide. But all site wide roles will be also returned by api.user.get_roles() to this user on the given context.

from plone import api
folder = api.content.create(container=portal, type='Folder', id='folder_one', title='Folder One')
api.user.grant_roles(username='jane',
    roles=['Editor', 'Contributor'],
    obj=portal['folder_one']
)

Revoke roles from user

The api.user.revoke_roles() allows us to revoke a list of roles from the user.

from plone import api
api.user.revoke_roles(username='jane',
    roles=['SiteAdministrator']
)

If you pass a context object the local roles will be removed.

from plone import api
folder = api.content.create(container=portal, type='Folder', id='folder_three', title='Folder Three')
api.user.grant_roles(username='jane',
    roles=['Editor', 'Contributor'],
    obj=portal['folder_three']
)
api.user.revoke_roles(username='jane',
    roles=['Editor'],
    obj=portal['folder_three']
)

Project Versions

Table Of Contents

Previous topic

Content

Next topic

Groups

This Page