Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Role-Based Access Control plugin for OIE/Mirth Connect 4.7.0 (1.1.4 is the last
## Project Structure
```
rbac/
├── server/ # RbacAuthorizationController, RbacServicePlugin, RbacServlet, RbacRepository, RbacMigrator, RoleTransfer*/RoleImport* (export and import), PluginSettingsGuard
├── server/ # RbacAuthorizationController, RbacServicePlugin, RbacServlet, RbacRepository, RbacMigrator, RoleTransfer*/RoleImport* (export and import), PluginSettingsGuard, UserRoles (for other plugins)
├── shared/ # RbacServletInterface (JAX-RS), Role model, RoleImportResult/RoleImportRow, NameCharacters (the name rules the server and the Swing role editor share)
├── client/ # RbacClientPlugin, RbacSettingsPanel (+ UserAssignmentsPanel), RoleEditorDialog, RoleTransferActions/RoleImport*/RoleExport* (export and import), SecureAuthorizationController
├── package/ # plugin.xml, sqlmap.xml, assembly.xml
Expand Down Expand Up @@ -43,6 +43,11 @@ Output: `package/target/rbac-1.3.0.zip`
- Names with control characters: `RoleService` refuses a new or renamed role name, and newly added permissions, holding one of those characters or a character XML can't carry (`UNSAFE_CHARACTERS`), and a new or changed description holding a control character other than a tab or a line break, or a character XML can't carry (`UNSAFE_DESCRIPTION`); the import planner refuses a file that would create a role with such a name. Existing names, permissions and descriptions are left alone. A stored name, permission or description holding a character XML can't carry (a bell, U+0007) makes the role list answer HTTP 500 to both Administrators and stops role export (measured on OIE 4.7.0), so `UnlistableRoles` logs such roles at startup with their id, and the README gives the REST call that deletes one. Deleting a role removes its users' assignments (`ON DELETE CASCADE`), and the README says so. Owner's decisions 2026-11-23 and 2026-11-24: refuse only these characters. The engine's channel-name allow-list isn't used; the engine checks it only in the Swing client.
- Cache: `ConcurrentHashMap<userId, UserAuthCache>` invalidated on all write REST endpoints. `DeletedUserCleanup` removes a deleted user's `rbac_user_role` row on the request after the engine's delete (the engine has no user-delete hook, but it asks rbac to authorize `removeUser` first, so the approved target is held as pending and checked against the engine on the next authorization); the startup sweep (`deleteOrphanUserRoles`) covers users deleted while rbac wasn't loaded.

### Setting a role from another plugin (`UserRoles`)
- `com.diridium.rbac.UserRoles` (public, static methods, issue #56) is the supported way for another plugin to set a user's role, called by reflection: `isReady`, `roleNames`, `adminRoleName`, `currentRoleName(int)`, `assign(int, String, String)` returning `ASSIGNED`, `ALREADY_ASSIGNED`, `NO_SUCH_ROLE`, `LAST_ADMIN` or `NOT_READY`; unexpected failures throw; the reads throw `IllegalStateException` before rbac starts. `UserRolesService` does the work and is unit-tested; `UserRolesContractTest` pins the class name, its exact set of public methods and the codes, so a new public method fails the build until it is meant. Owner's decisions 2026-11-26 to 2026-11-29: by role name, never id (ids differ after an export and import); names compared exactly on every database (`findRoleIdByName` ignores case on MySQL and SQL Server, so its answer is checked); no remove method until a plugin asks; the audit event (`RbacAuditLog.pluginAssignment`) names the plugin as the caller gives it.
- The outside calls OIDC 1.0.3 makes (`RbacRepository` methods and `invalidateCache(Integer)`, pinned by `ReflectionCallerContractTest`) are deprecated, not removed: they keep working until a release named in the release notes. rbac keeps using those methods itself. The LDAP plugin uses `UserRoles` from its first release.
- The e2e suite calls `UserRoles` from a channel deploy script, by reflection like a plugin, with no signed-in user (`checks.user_roles`). Measured 2026-11-29: a deploy script reaches rbac's classes through `Class.forName`.

### Client-Side Authorization
- `SecureAuthorizationController` implements `com.mirth.connect.client.ui.AuthorizationController`
- Must be in package `com.mirth.connect.plugins.auth.client` (factory expects this exact FQCN)
Expand Down Expand Up @@ -94,7 +99,7 @@ Output: `package/target/rbac-1.3.0.zip`

## Key Design Decisions
- Admin access is by role flag (`is_admin`), not by user ID — supports multiple admin users; identified the same way on server and client (`Role.isAdmin()`)
- The admin role cannot be deleted, and an "admin floor" prevents removing/reassigning the last *live* admin user (enforced server-side by `AdminRoleGuard`; the floor count excludes orphaned rows for engine-deleted users). The admin role's name *can* be changed; the role editor disables the admin role's name, permission, and channel controls (only the description is editable) and re-sends its stored perms on save.
- The admin role cannot be deleted, and an "admin floor" prevents removing/reassigning the last *live* admin user (enforced server-side by `AdminRoleGuard`; the floor count excludes orphaned rows for engine-deleted users). Role changes run one at a time: `RoleService.assignUserRole`/`removeUserRole` and `UserRoles.assign` hold `AdminRoleGuard.ROLE_CHANGES` from the floor check through the write, so two simultaneous demotions can't leave no admin (`RoleChangeRaceTest`). This reverses a 2026-06-12 decision against a lock: sign-in plugins now change roles programmatically, and a restart doesn't recover their case (`AdminRoleSeeder` restores only a roleless user 1). Owner's decision 2026-11-29. One engine only; OIDC's direct repository calls don't take the lock. The admin role's name *can* be changed; the role editor disables the admin role's name, permission, and channel controls (only the description is editable) and re-sends its stored perms on save.
- The admin role's retention floor is the **core** permission set, not core+extension. The seeder grants only core perms, and admin access is by the flag regardless of stored perms, so requiring extension perms just made the admin role uneditable.
- `manageExtensions` is effectively an admin-equivalent grant: a holder can disable the RBAC extension, and on the next restart the engine reverts to the always-allow `DefaultAuthorizationController` (every user becomes admin). The role editor tags it with a warning tooltip; treat it as a privileged grant. This mirrors stock OIE (manageExtensions = install/enable/disable plugins = arbitrary code).
- `Manage Roles` is for delegating user management, not a security boundary: a holder can give their own account any role, the admin role included (measured on OIE 4.7.0 with rbac 1.2.0). Both role editors tag it with a warning tooltip, like `manageExtensions`.
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ A role stores a plugin's permission by its display name, the name the role edito

The engine's plugin-settings calls (`/api/extensions/{name}/properties`) are allowed only under the exact name of an installed extension, and rbac's own settings only to the admin role.

## 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`. Call it by reflection, so your plugin has no build or install dependency on RBAC:

```java
Class<?> userRoles;
try {
userRoles = Class.forName("com.diridium.rbac.UserRoles");
} catch (ClassNotFoundException e) {
return; // RBAC isn't installed, or is older than the first version with UserRoles
}
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");
```

Its static methods:

- `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)`: gives the user the role and returns a code.

`assign` returns `ASSIGNED`, `ALREADY_ASSIGNED`, `NO_SUCH_ROLE`, `LAST_ADMIN` or `NOT_READY`. `LAST_ADMIN` means the user is the last user holding the admin role, so they keep it. 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.

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 on one engine run one at a time, whether they come from `assign` or from the Administrator. Two changes made at the same moment can't both move one of the last two admins off the admin role. Engines that share one database don't wait for each other, and the other engines keep the permissions they cached before the change ([#62](https://github.com/diridium-com/role-based-access-control/issues/62)).

Plugins that call `RbacRepository` 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 keep working for now, and the release notes will name the release that stops supporting them ([#72](https://github.com/diridium-com/role-based-access-control/issues/72)).

## If the RBAC tab can't list roles

A role whose name, permission or description contains a control character stops the OIE Administrator and the web Administrator from listing roles. RBAC names it in mirth.log at startup, with its number. Delete it with `curl -k -u admin -H 'X-Requested-With: OpenAPI' -X DELETE https://<engine>:8443/api/extensions/rbac/roles/<number>`, then recreate it in the role editor. Deleting a role also removes it from its users, so assign them the new role.
Expand Down
23 changes: 23 additions & 0 deletions docs/design-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,26 @@ The list and removal are admin-only, like role export and import.
- Browser tests: who sees the task, the list with the disabled plugins, Remove Selected and Remove All with their confirmations, and an empty list.
- The e2e suite: a made-up grant is listed and no registered permission is, a View Roles and Manage Roles holder who is not the admin is refused both calls, and a removal takes the made-up grant and leaves the engine permission.
- Both dialogs, driven on OIE 4.7.0 with a role still holding "Use Perm Test", the permission of a throwaway test plugin that doesn't ship, after the plugin's version 2 renamed it: both listed it, and the web dialog removed a made-up grant while that role kept its engine permissions.

## 2026-11-29: UserRoles, for plugins that set a user's role

Two sign-in plugins give users a role from their directory or identity provider groups: OIDC Authentication (gibson9583/oie-oidc-auth), and the LDAP plugin, which isn't released yet. Both call rbac's repository by reflection. Those calls bypass rbac's last-admin check, cache refresh and audit event, so each plugin repeats the check and the refresh itself. `com.diridium.rbac.UserRoles` is the supported way. Plugins still call it by reflection, so they need no build or install dependency on rbac.

Roles are given by name. Role ids differ between engines, for example after a role export and import, and both plugins are configured with names. The database's name lookup ignores case on MySQL and SQL Server, so `UserRoles` checks the role it finds against the name and answers the same on every database.

`assign` returns a code for each expected outcome. An unexpected failure, such as the database being down, throws. Both plugins catch every exception around their reflection calls (read in the code of OIDC 1.0.3 and of the LDAP plugin), so the caller can log the cause.

There is no method to take a role away. Neither plugin's code does that today. A method is easy to add later, but once a plugin relies on one, removing it means a deprecation. The contract test pins the exact set of public methods so that each new one is a deliberate choice.

Role changes on one engine now run one at a time. A lock around the last-admin check and the write was considered and rejected on 2026-06-12, when only people changed roles in the Administrator, with a note to revisit if roles were ever changed programmatically. Sign-in plugins change roles programmatically, at every sign-in. A restart doesn't repair a race there either. rbac restores the admin role at startup only to a user 1 left with no role, and a plugin moves users to another role. The lock covers `UserRoles.assign` and the Administrators' Assign Role and Remove Role. Engines sharing one database aren't covered.

The audit event names the plugin as the caller gives it. rbac doesn't check that name: any plugin in the engine can write rbac's tables directly.

OIDC's direct calls keep working. Going by its code, if they stopped working, OIDC 1.0.3 would log a warning in mirth.log at each sign-in and leave the user's role as it was, until a new OIDC release. They are deprecated, and a later release, named in the release notes, ends support for them.

### Tests

- Unit tests: each code `assign` returns, a name differing only in case where the database ignores case, the last admin kept, a database failure thrown with nothing recorded, the audit event and the one user's cache refresh.
- A race test starts two demotions of the last two admins at the same moment: once with both through `UserRoles`, once with one through `UserRoles` and one through the Administrator's Assign Role. It checks that one admin is left. With the lock removed, both demotions went through in both cases and no admin was left. With only the Administrator path's lock removed, the second case failed.
- A contract test pins the class name, its exact public methods, their signatures and the codes, looked up the way plugins look them up.
- The e2e suite calls `UserRoles` from a channel deploy script on OIE 4.7.0, by reflection and with no signed-in user, as a sign-in plugin does. Before writing it, a probe showed a deploy script can reach rbac's classes through `Class.forName`. Not measured: whether that is the class loader a plugin's own classes see. The LDAP plugin's switch to `UserRoles` will test that.
Loading
Loading