Skip to main content

Assign a role to a user with the Java SDK

permit.api.users.assignRole() assigns a role to a user in one tenant. The role is effective only within that tenant. This reference is for Java developers who manage role-based access control (RBAC) assignments from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

Signature

RoleAssignmentRead assignRole(String userKey, String roleKey, String tenantKey) throws IOException, PermitApiError, PermitContextError

Parameters

ParameterTypeRequiredDescription
userKeyStringYesThe key or ID of the user who receives the role.
roleKeyStringYesThe key or ID of the role to assign.
tenantKeyStringYesThe key or ID of the tenant that scopes the assignment.

Example

The example assigns the admin role to a user in a tenant. The parameters are positional: user, role, then tenant. The example uses RoleAssignmentRead from io.permit.sdk.openapi.models.

import io.permit.sdk.openapi.models.RoleAssignmentRead;

RoleAssignmentRead ra = permit.api.users.assignRole("john@permit.io", "admin", "acme-corp");

Return value

assignRole() returns a RoleAssignmentRead object (io.permit.sdk.openapi.models.RoleAssignmentRead). The object includes the user, role, and tenant keys, their IDs, and the assignment createdAt time.

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.