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
| Parameter | Type | Required | Description |
|---|---|---|---|
userKey | String | Yes | The key or ID of the user who receives the role. |
roleKey | String | Yes | The key or ID of the role to assign. |
tenantKey | String | Yes | The 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.
| Exception | Thrown when |
|---|---|
IOException | The HTTP request to the Permit API fails, for example because of a network error. |
PermitApiError | The Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body. |
PermitContextError | The SDK context does not include an environment, for example because the API key is scoped to an organization or project. |