Skip to content

PhoenixKit 2.23.0: Users with several roles act as one role at a time

PhoenixKit 2.23.0 adds an optional role switcher. A user who holds several roles can work as one of them at a time, with only that role’s access, and switch from the account menu. Roles also get an order you set by hand.

Acting as one role

The switcher is off by default. Turn it on in the new Roles tab of the users settings, with Let users with several roles act as one role at a time. A user with two or more switchable roles then works as exactly one of them. An Admin acting as another role has no admin access until they switch back. Owner and Admin are always switchable, and the User role always applies. Under Always-on roles you tick custom roles that should apply whatever role is active; those are not offered in the switcher.

The choice belongs to the session, not the account. Someone can be Admin on one computer and another role on a second one, and each new sign-in starts in the first role the user holds. When a user switches, every open page of theirs updates, and a page the new role cannot reach sends them away with “This page is not available in the role you switched to.” Each switch is written to the activity log.

Switcher location puts the control in the Account menu, under the language section, or in the Header (account menu on phones). The sessions list in the admin and the user’s own device list show which role each session is Acting as.

Role order

Roles can be reordered on the Roles page by dragging or with Move up and Move down. The order decides the role a new session starts in and the order the switcher lists roles. A fresh install keeps the old order: Owner, Admin, User, then custom roles by creation date.

Security fixes

  • Removing a role from a user signs out the sessions acting as that role. Their other sessions keep going.
  • An Admin acting as another role can no longer impersonate users.
  • After a password change, the old session on the browser that made the change is now signed out a few seconds later, so a stolen copy of that cookie stops working.
  • A deactivated user no longer gets a fresh session when their page refreshes its permissions.
  • The Wrong email? Change it form asks for the password when email confirmation is not required.
  • Permanently deleting from a folder’s trash no longer destroys a file that was only linked into that folder from elsewhere; it is unlinked instead.

Also fixed: a role change no longer empties the header’s account switcher until the next page load, and the time-zone alert no longer overwrites a user’s other custom fields.

Upgrading

Run mix phoenix_kit.update to apply migration V190, which adds the role order and the active role per session. Nothing changes for users until you turn the switcher on. Code that calls Roles.list_roles/0 now gets roles in the set order instead of system roles first, then by name.

Full release notes: PhoenixKit 2.23.0

2.23.0 · 13 September 2026

Added

  • Active role: users act as one role at a time, per session (opt-in, role_switcher_enabled, default off). A user holding two or more switchable roles acts as exactly one of them, and Scope.for_user/1 narrows cached_roles and cached_permissions to that role plus the always-on roles — an Admin acting as “Seller” has no admin access until they switch back. Owner and Admin are always switchable; User is always on; custom roles are switchable unless listed in role_switcher_always_on_roles. The choice is stored on the session token (phoenix_kit_users_tokens.active_role_uuid, V190): Admin on one machine and Seller on another at the same time, a role of its own for an impersonation session, a fresh start for every sign-in and every added multi-session account. It reaches for_user/1 through %User{active_role_uuid: _}, a virtual field only the session-token loader fills, so every scope rebuild — plugs, LiveView mounts, the role-change refresh (now reloading through the session token), sibling packages — narrows without changes. A stored role the user no longer holds is ignored. New: PhoenixKit.Users.ActiveRole, Scope.active_role/1, Scope.held_roles/1, Scope.narrowed?/1, Scope.switchable_roles/1, Scope.for_user(user, narrow: false), Roles.get_user_role_records/1, Roles.get_role_records_for_users/1, Permissions.get_permissions_for_roles/1; settings role_switcher_enabled, role_switcher_location, role_switcher_always_on_roles. Design and review record: dev_docs/plans/2026-09-13-active-role.md.
  • Role order — phoenix_kit_user_roles.position (V190; seeded Owner, Admin, User, then custom roles by creation). Reordered on /admin/users/roles by dragging or with the arrows (Roles.reorder_roles/1, Roles.move_role/2); Roles.list_roles/0, list_roles_paginated/1, get_custom_roles/0 and get_user_role_records/1 follow it. It decides the default role of every new session — the first switchable role the user holds — and the order the switcher lists roles in.
  • Switching the active role — PUT /users/session/role (role_uuid, optional return_to) through ActiveRole.switch/3: refused unless the switcher is on and the role is one of the user’s switchable roles, written to that session’s token only, logged as session.role_switched, and broadcast so every open LiveView of the user rebuilds its scope from its own token and leaves pages the new role cannot reach (“This page is not available in the role you switched to.”). return_to is followed only when the new scope can mount it — admin-area paths are resolved through the router to their LiveView and asked the mount gate’s own question, every role alike.
  • Removing a role signs out the sessions acting as it (Sessions.revoke_user_sessions_in_role/2, from Roles.remove_role/3 and sync_user_roles/3): the person starts over in whatever they still hold. Sessions in another role keep going and refresh in place.
  • The role switcher UI — PhoenixKitWeb.Components.Core.RoleSwitcher. A “Role” section under Language in both account dropdowns (AdminNav.admin_user_dropdown/1, UserDashboardNav.user_dropdown/1), and with role_switcher_location set to "header" a compact control in the admin and dashboard headers from sm up, the menu section taking over on phones. Renders only for a narrowed scope — an impersonation session included; the switchable roles ride on the scope (Scope.switchable_roles/1), so it costs no query per page. The multi-session account list labels each account with the role its session is acting as.
  • Sessions lists show the role each session acts as — Sessions.* rows carry active_role; /admin/users/sessions and the user’s own devices list render it beside the device.
  • A “Roles” tab on /admin/settings/users for the role_switcher_* settings: the on/off switch, the switcher location, and which custom roles are always on (Owner, Admin and User are fixed and not offered). Guide: dev_docs/guides/2026-09-13-active-role.md.

Changed

  • Roles.list_roles/0 and list_roles_paginated/1 order by role order (position, then name) instead of system-roles-first-then-name. The seeded order is the same for an untouched install.

Fixed

  • Permanently deleting from a folder’s trash view no longer destroys a file merely linked in from another scope (post-#804 review): the single-file delete now requires the file’s home in scope, like the bulk delete always did; a linked file in this folder’s trash is unlinked instead. Rotation, which is written on the file, again requires the file’s home in scope rather than only its appearance in the viewed folder.
  • The parked “Wrong email? Change it” form asks for the password when email confirmation is not enforced. With require_email_confirmation off an unconfirmed account is fully usable, so the password-less path (2.22.7) would have let a stolen session re-address a live account. Confirmation enforced keeps the password-less path.
  • A stolen copy of the session cookie survived a password change on the submitting browser’s token, which 2.22.18 exempted from the disconnect so the re-login could go out. Session.create now disconnects that token five seconds after the new one is issued (Sessions.disconnect_tokens_later/2).
  • The LiveView scope refresh handed a deactivated user a fresh scope. reload_session_user/2 now passes through ensure_active_user/1 like every other token/user resolution.
  • An Admin acting as another role could still impersonate. The impersonation authority now reads the root session’s roles in effect, so it follows the active role even for a hand-crafted POST. Targets are still judged by their real roles.
  • “You cannot edit your own role” ignored roles you hold but are not acting as. Permissions.can_edit_role_permissions?/2 now checks Scope.held_roles/1.
  • A role or permission change emptied the header’s account switcher until the next full page load: the LiveView scope refresh rebuilt the scope without the multi-session fields. They are now carried over.
  • The time-zone alert replaced the whole custom_fields map from the socket’s user struct, silently restoring stale values of every other key a concurrent tab had changed. It now merges its one key atomically (Auth.merge_user_custom_fields/3).

PhoenixKit 2.23.0. Install or update: {:phoenix_kit, "~> 2.23"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.