Skip to content

Add UserRoles, a supported way for plugins to set a user's role - #73

Merged
pacmano1 merged 1 commit into
mainfrom
user-roles-api
Nov 30, 2026
Merged

pacmano1 merged 1 commit into
mainfrom
user-roles-api

Conversation

@pacmano1

Copy link
Copy Markdown
Contributor

OIDC Authentication sets each user's role from identity provider groups, and the LDAP plugin, not released yet, does the same from directory groups. 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, and two plugins can race each other.

This adds com.diridium.rbac.UserRoles, a supported way to do it. Plugins still call it by reflection, with no build or install dependency on rbac.

  1. Static methods: isReady(), roleNames(), adminRoleName(), currentRoleName(userId) and assign(userId, roleName, callerPlugin).
  2. Roles are given by name, because role ids differ between engines, for example after a role export and import. Names are compared exactly on every database. MySQL and SQL Server look names up ignoring case, so rbac checks the role it finds against the name.
  3. assign returns ASSIGNED, ALREADY_ASSIGNED, NO_SUCH_ROLE, LAST_ADMIN or NOT_READY, and throws on an unexpected failure such as the database being down.
  4. It keeps the last user holding the admin role on that role, refreshes the user's cached permissions, and records an audit event with the user, the role and the plugin name the caller passes.

Role changes on one engine now run one at a time. UserRoles.assign and Assign Role and Remove Role in both Administrators hold one lock from the last-admin check through the write. Before, two demotions of the last two admins at the same moment could both go through and leave no admin; now the second is refused. A lock was rejected on 2026-06-12, when only people changed roles. Plugins change roles at every sign-in, and a restart doesn't repair a race there: rbac restores the admin role at startup only to a user 1 left with no role. The lock covers one engine. Engines sharing one database don't wait for each other (issue #62).

The repository calls OIDC Authentication 1.0.3 makes are unchanged and still pinned by ReflectionCallerContractTest. They are deprecated: the README says so, and the release notes will name the release that ends support for them (issue #72). The LDAP plugin is to use UserRoles from its first release.

Docs: a README section for plugin authors, CLAUDE.md and a design notes entry. The wiki pages go out with the release.

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. 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 set of public methods, their signatures and the codes.
  • An e2e check 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: every code, the case check, the last admin kept, the user's next request allowed at once, and one audit event naming the plugin. The whole e2e suite passed on OIE 4.7.0 with each database: Derby 353 checks, MySQL 366, Postgres 367, Oracle 359, SQL Server 366 with the jTDS driver and 366 with Microsoft's, no failures.

Closes #56

Co-authored with Claude Code.

OIDC Authentication sets a user's role from identity provider groups,
and the LDAP plugin, not released yet, does the same from directory
groups. 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:

- isReady, roleNames, adminRoleName, currentRoleName and assign, all
  static.
- Roles by name, compared exactly on every database.
- assign returns ASSIGNED, ALREADY_ASSIGNED, NO_SUCH_ROLE, LAST_ADMIN or
  NOT_READY, and throws on unexpected failures.
- It keeps the last admin on the admin role, refreshes that user's
  cached permissions, and records an audit event naming the plugin.

Role changes on one engine now run one at a time: UserRoles.assign and
the Administrators' Assign Role and Remove Role hold one lock from the
last-admin check through the write. A lock was rejected on 2026-06-12,
when only people changed roles. Plugins change roles at every sign-in,
and a restart doesn't repair a race there: rbac restores the admin role
at startup only to a user 1 left with no role.

The direct repository calls OIDC Authentication 1.0.3 makes keep working
and are deprecated. The release notes will name the release that ends
support for them.

Tests: unit tests for each code assign returns, a race test that fails
without the lock, a contract test pinning the class name, its public
methods and the codes, and an e2e check that calls UserRoles from a
channel deploy script by reflection on all six databases.

Co-Authored-By: Claude <noreply@anthropic.com>
@pacmano1
pacmano1 merged commit b18f224 into main Nov 30, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give other plugins a supported way to assign a user's role

1 participant