List the roles assigned to a user with the Java SDK
permit.api.users.getAssignedRoles() fetches the role assignments of one user, in all tenants or in one tenant. This reference is for Java developers who read 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
| Overload | Behavior |
|---|---|
RoleAssignmentRead[] getAssignedRoles(String userKey) | Fetches page 1 with 100 results per page, in all tenants. |
RoleAssignmentRead[] getAssignedRoles(String userKey, int page) | Fetches the given page with 100 results per page, in all tenants. |
RoleAssignmentRead[] getAssignedRoles(String userKey, int page, int perPage) | Fetches the given page with the given page size, in all tenants. |
RoleAssignmentRead[] getAssignedRoles(String userKey, String tenantKey, int page, int perPage) | Fetches the given page with the given page size, in one tenant. Pass null as tenantKey to include all tenants. |
All overloads throw IOException, PermitApiError, and PermitContextError.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
userKey | String | Yes | The key of the user. |
tenantKey | String | No | The key of a tenant. Returns only the assignments in that tenant. |
page | int | No | The page number to fetch, starting at 1. Defaults to 1. |
perPage | int | No | The number of results per page. Defaults to 100. |
Example
Replace [USER KEY] with the key of the user. To limit the results to one tenant, call permit.api.users.getAssignedRoles("[USER KEY]", "[TENANT KEY]", 1, 100). The example uses RoleAssignmentRead from io.permit.sdk.openapi.models.
import io.permit.sdk.openapi.models.RoleAssignmentRead;
RoleAssignmentRead[] assignedRoles = permit.api.users.getAssignedRoles("[USER KEY]");
Return value
getAssignedRoles() returns an array of RoleAssignmentRead objects (io.permit.sdk.openapi.models.RoleAssignmentRead). Each object includes the user, role, and tenant keys of one assignment. The array is empty when the user has no role assignments on the requested page.
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. |