Skip to main content

Update a role with the Java SDK

permit.api.roles.update() changes the fields you set on an existing role and leaves the other fields unchanged. This reference is for Java developers who manage role-based access control (RBAC) roles from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

Signature

RoleRead update(String roleKey, RoleUpdate roleData) throws IOException, PermitApiError, PermitContextError

Parameters

ParameterTypeRequiredDescription
roleKeyStringYesThe key or the ID (UUID) of the role. The Permit API accepts either value.
roleDataRoleUpdateYesThe fields to change. The fields are listed in RoleUpdate fields.

RoleUpdate fields

Create a RoleUpdate with the no-argument constructor and set only the fields you want to change. Every field is optional. The SDK sends the update as an HTTP PATCH request.

FieldBuilder methodTypeDescription
namewithName()StringThe display name of the role.
descriptionwithDescription()StringWhat the role represents or which permissions it grants.
permissionswithPermissions()List<String>The permissions the role grants, in resource:action format (for example document:read).
attributeswithAttributes()HashMap<String, Object>Key-value metadata about the role.
extendswithExtends()List<String>Keys of roles that this role extends. The role inherits all permissions of the listed roles.
grantedTowithGrantedTo()DerivedRoleBlockEditDerived role rules applied to this role.

To add or remove permissions without replacing the full list, the SDK also has permit.api.roles.assignPermissions(roleKey, permissions) and permit.api.roles.removePermissions(roleKey, permissions). Both methods take an ArrayList<String> and return a RoleRead.

Example

The example changes the description of the admin role.

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

permit.api.roles.update("admin", new RoleUpdate().withDescription("new description"));

Return value

update() returns a RoleRead object (io.permit.sdk.openapi.models.RoleRead) with the updated role.

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.