Skip to main content

Create a user with the Java SDK

permit.api.users.create() creates a user, with optional attributes and role assignments, in your Permit environment. This reference is for Java developers who add users to Permit from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

To create a user or update the user if the key already exists, use Sync a user with the Java SDK instead.

Signature

UserRead create(UserCreate userData) throws IOException, PermitApiError, PermitContextError

Parameters

ParameterTypeRequiredDescription
userDataUserCreateYesThe user to create. The fields are listed in UserCreate fields.

UserCreate fields

The UserCreate(key) constructor sets the required key field. Set the optional fields with the with...() builder methods.

FieldBuilder methodTypeRequiredDescription
keywithKey()StringYesA unique ID by which Permit identifies the user in permission checks. Pass the same key to permit.check(). Use any value that is unique in your system, such as an email address or a UUID.
emailwithEmail()StringNoThe email address of the user. Unique within the environment.
firstNamewithFirstName()StringNoThe first name of the user.
lastNamewithLastName()StringNoThe last name of the user.
attributeswithAttributes()HashMap<String, Object>NoUser attributes for attribute-based access control (ABAC) policies.
roleAssignmentswithRoleAssignments()List<UserRoleCreate>NoRoles to assign to the user when the user is created. See UserRoleCreate fields.

UserRoleCreate fields

Each UserRoleCreate object describes one role assignment. The UserRoleCreate(role) and UserRoleCreate(role, tenant) constructors set the role and the tenant.

FieldBuilder methodTypeRequiredDescription
rolewithRole()StringYesThe key or ID of the role to assign.
tenantwithTenant()StringNoThe key or ID of the tenant in which the role is assigned.
resourceInstancewithResourceInstance()StringNoThe resource instance on which the role is assigned, as an ID or in resource_type:resource_instance format.

Example

The example creates a user with a key, an email address, a first and last name, and attributes. The attributes map is a java.util.HashMap.

import io.permit.sdk.openapi.models.UserCreate;
import io.permit.sdk.openapi.models.UserRead;
import java.util.HashMap;

// optional attributes for attribute-based access control
HashMap<String, Object> userAttributes = new HashMap<>();
userAttributes.put("age", Integer.valueOf(50));
userAttributes.put("fav_color", "red");

UserRead user = permit.api.users.create(
(new UserCreate("auth0|john"))
.withEmail("john@permit.io")
.withFirstName("John")
.withLastName("Smith")
.withAttributes(userAttributes)
);

Create a user with role assignments

The example creates a user and assigns roles in the same call. The first two assignments grant the editor and moderator roles in the default tenant. The third assignment grants the editor role on one resource instance. The example uses UserRoleCreate from io.permit.sdk.openapi.models and java.util.List and java.util.Arrays.

import io.permit.sdk.openapi.models.UserCreate;
import io.permit.sdk.openapi.models.UserRead;
import io.permit.sdk.openapi.models.UserRoleCreate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

// optional attributes for attribute-based access control
HashMap<String, Object> userAttributes = new HashMap<>();
userAttributes.put("age", Integer.valueOf(50));
userAttributes.put("fav_color", "red");

final List<UserRoleCreate> roleAssignments = Arrays.asList(
new UserRoleCreate("editor", "default"),
new UserRoleCreate("moderator", "default"),
new UserRoleCreate("editor", "default").withResourceInstance("myResourceType:myResourceInstance")
);

UserRead user = permit.api.users.create(
(new UserCreate("auth0|john"))
.withEmail("john@permit.io")
.withFirstName("John")
.withLastName("Smith")
.withAttributes(userAttributes)
.withRoleAssignments(roleAssignments)
);

Return value

create() returns a UserRead object (io.permit.sdk.openapi.models.UserRead) with the created user, including the ID that Permit assigns to the user.

Exceptions

All exception classes except IOException are in the io.permit.sdk.api package.

ExceptionThrown when
IOExceptionThe HTTP request to the Permit API fails, for example because of a network error.
PermitApiErrorThe Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body.
PermitContextErrorThe SDK context does not include an environment, for example because the API key is scoped to an organization or project.