Skip to content

Plugin Authors

Finnegan's Owner edited this page Dec 2, 2026 · 1 revision

Plugin Authors

Setting a user's role from another plugin

A plugin that signs users in, from a directory or an identity provider for example, can give each user an RBAC role through com.diridium.rbac.UserRoles (since 1.4.0). Call it by reflection, so your plugin has no build or install dependency on RBAC. Here userId is the id of the engine user your plugin signed in, and the reflective calls throw ReflectiveOperationException, so make them inside a try:

Class<?> userRoles;
try {
    userRoles = Class.forName("com.diridium.rbac.UserRoles");
} catch (ClassNotFoundException e) {
    return; // RBAC isn't installed, or is older than 1.4.0
}
if (!(Boolean) userRoles.getMethod("isReady").invoke(null)) {
    return; // RBAC hasn't started
}
String result = (String) userRoles.getMethod("assign", int.class, String.class, String.class)
        .invoke(null, userId, "Billing", "My Sign-in Plugin");
Method Returns
isReady() whether RBAC has started
roleNames() every role's name, for a settings screen
adminRoleName() the admin role's name
currentRoleName(userId) the user's role, or null when the user has none
assign(userId, roleName, callerPlugin) one of the codes below
assign code Meaning
ASSIGNED the user now holds the role
ALREADY_ASSIGNED the user already held it; nothing changed
NO_SUCH_ROLE no role has exactly that name; nothing changed
LAST_ADMIN the user is the last user holding the admin role and keeps it; nothing changed
NOT_READY RBAC hasn't started; nothing changed

assign throws IllegalArgumentException when no engine user has that id, and changes nothing. On an unexpected failure, such as the database being down, assign throws and the role is unchanged. The read methods throw IllegalStateException before RBAC has started. Through Method.invoke, each of these exceptions arrives as the cause of an InvocationTargetException.

Roles are given by name, because role ids differ between engines. Names are compared exactly on every database, so "billing" doesn't find "Billing".

assign applies the same last-admin check as Assign Role in the Administrator, refreshes the user's permissions at once, and records an audit event with the user, the role and the plugin name you pass. RBAC records that name as given and doesn't check it.

Role changes run one at a time

Since 1.4.0, role changes on one engine run one at a time, whether they come from assign or from Assign Role and Remove Role in either Administrator. If two changes at the same moment would each move one of the last two admins off the admin role, the second is refused. A user delete doesn't take part: the engine deletes the user after RBAC's last-admin check allows it.

This covers one engine. Engines that share one database don't wait for each other, and the other engines keep the permissions they cached before the change (rbac issue #62).

Deprecated: calling RbacRepository directly

Plugins that call RbacRepository (getInstance, findRoleIdByName, getUserRoleId, getAdminRoleId, countUsersByRoleId, assignUserRole) and RbacAuthorizationController.invalidateCache directly should move to UserRoles. Those calls bypass RBAC's last-admin check, cache refresh and audit event, and don't wait for other role changes. They still work, but they're no longer a supported interface: a later release may change them, and its release notes will say so.


Home

Clone this wiki locally