Skip to main content

Sync a user with the Java SDK

permit.api.users.sync() creates a user in your Permit environment, or updates the user if a user with the same key already exists. Call sync() when a user signs up or signs in to your application, so the policy decision point (PDP) can evaluate permission checks for that user. This reference is for Java developers who connect their authentication system to Permit. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

note

Don't use sync() to change a user's roles after the first sync. To add or remove a role, call permit.api.users.assignRole() or permit.api.users.unassignRole().

Signature

OverloadBehavior
CreateOrUpdateResult<UserRead> sync(User user)Takes the same User object (io.permit.sdk.enforcement.User) that permit.check() takes. The SDK copies the key, email, first name, last name, and attributes into a UserCreate object. Role assignments set with withRoleAssignments() on the builder are not sent.
CreateOrUpdateResult<UserRead> sync(UserCreate userData)Takes a UserCreate object (io.permit.sdk.openapi.models.UserCreate), the same type that permit.api.users.create() takes.

Both overloads throw IOException, PermitApiError, and PermitContextError. The SDK sends the user as an HTTP PUT request to the user's key.

Parameters

ParameterTypeRequiredDescription
userUserYes, for the User overloadThe user to sync. Build the object with new User.Builder(key) and the fields in User fields.
userDataUserCreateYes, for the UserCreate overloadThe user to sync. The fields are listed in Create a user with the Java SDK.

User fields

The User.Builder(key) constructor sets the required key. Set the optional fields with the with...() builder methods, then call build().

FieldBuilder methodTypeRequiredDescription
keyUser.Builder(key)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.

Example

The example syncs a user with a key, an email address, a first and last name, and attributes, then reads the result. The attributes map is a java.util.HashMap.

import io.permit.sdk.api.models.CreateOrUpdateResult;
import io.permit.sdk.openapi.models.UserRead;
import io.permit.sdk.enforcement.User;
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");

// this is how you sync the user
CreateOrUpdateResult<UserRead> result = permit.api.users.sync(
(new User.Builder("auth0|john"))
.withEmail("john@permit.io")
.withFirstName("John")
.withLastName("Smith")
.withAttributes(userAttributes)
.build()
);
UserRead user = result.getResult();
boolean wasCreated = result.wasCreated();

Return value

sync() returns a CreateOrUpdateResult<UserRead> object (io.permit.sdk.api.models.CreateOrUpdateResult):

MethodReturns
getResult()The synced user as a UserRead object.
wasCreated()true if the call created the user (HTTP status 201). false if the call updated an existing 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. If the UserCreate key is null, the SDK throws PermitApiError with response code 406 before calling the API. 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.