Skip to main content
Version: 2.0.0

permit.api.users.sync()

This function is used to sync (save) a user's information to the Permit.io cloud and PDP (Policy Decision Point) upon user creation.

note

This function should be used for the initial creation of the user. This means that when you need to have the user synced between your authentication solution and Permit, this is the function you need to use.

Once the user has been synced with Permit, you should not use this sdk call to update the user's role.

Instead, you should use the assignRole function.

Parameters

user - An object that contains the information about the user. The payload is defined below.

Payload

key - A unique id by which Permit will identify the user for permission checks.

You will later pass this ID to the permit.check() API. You can use anything for this ID - the user email, a UUID or anything else as long as it's unique on your end. The user key must be url-friendly (slugified).

email - optional - The email of the user. If synced, will be unique inside the environment.
firstName - optional - First name of the user.
lastName - optional - Last name of the user.
attributes - optional - Arbitraty user attributes that will be used to enforce attribute-based access control policies.

Implementation

import io.permit.sdk.api.models.CreateOrUpdateResult;
import io.permit.sdk.openapi.models.UserRead;
import io.permit.sdk.enforcement.User;

// 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|elon"))
.withEmail("elonmusk@tesla.com")
.withFirstName("Elon")
.withLastName("Musk")
.withAttributes(userAttributes)
.build()
);
UserRead user = result.getResult();
boolean wasCreated = result.wasCreated();