PhoenixKit 1.7.218: Signing in as another user, and usernames that stay put
PhoenixKit 1.7.218 lets Owners and Admins sign in as another user, and fixes the admin user form renaming users each time it was saved.
Impersonation goes through a new POST /users/session/impersonate/:user_uuid action and needs multi-session to be on. The signed-in root account decides: it must be an Owner or Admin, an Owner can never be a target, and an Admin cannot take another Admin. Every attempt, including a refused one, is written to the activity feed, and the person whose account was used gets a security notification. There is no button for it in the admin yet.
Opening a user in the admin and saving without a username field generated a new one, and the user’s own name counted as taken, so maria became maria_1, then maria_2. Existing usernames now stay as they are. Two sign-ups racing for the same generated name now get a form error instead of a crash.
A queued email was also passed through the provider’s interception twice, so it was logged twice. That is fixed.
Full release notes: PhoenixKit 1.7.218
1.7.218 · 29 July 2026
Added
- Sign in as another user (#672) —
MultiSession.impersonate/2, aPOST /users/session/impersonate/:user_uuidaction, and the authority layer over the existing multi-account stack. The rules are role-based on purpose:Scope.can_access_admin_area?/1is true for any permission holder, so a permission check would have let a customer with one self-service grant borrow another customer’s account. Instead the root account decides (never the active one, or an impersonated session could chain) and must hold Owner or Admin; an Owner is never a target; an Admin cannot take another Admin, while an Owner can, because there is nothing above it to escalate to. Requiresmulti_session_enabled.
Changed
MultiSession.add_authenticated_user/3(#672 review) — takes the activity-feed action to write, defaulting tosession.account_added. Existing/2callers are unaffected.already_intercepted: truenow impliesskip_queue: true(#670 review) — the opt can only mean “a queue worker is re-sending what it dequeued”, so leaving the two independent let a worker that set one and forgot the other offer its own job straight back to the queue that handed it over.session.impersonatedandsession.account_addedrender as notifications (#672 review) — both are claimed by thesecuritypreference type (so they appear in the preferences UI and can be muted) and both have recipient-facing copy instead of the humanized raw action string (“Session impersonated”).
Fixed
- Impersonation refusals are recorded (#672 review) — the feed was written
only on success, and only after the session had already changed, despite the
documented promise of “every attempt, before the session changes”. An Admin
reaching for the Owner’s account, reaching sideways for another Admin, or a
non-staff user hitting the endpoint directly all left no trace at all. They now
write
session.impersonation_refusedwith the deciding rule inmetadata["reason"], and carry notarget_uuidso a refused attempt is a feed entry rather than a message sent to the account it named. - One activity row per impersonation, saying what happened (#672 review) — a
success wrote both
session.account_addedandsession.impersonated, and the first is indistinguishable from a user voluntarily adding an account of their own. - Impersonation authority is settled before the uuid is resolved (#672
review) — checking existence first answered “User not found.” for an unused
uuid and a permission message for a live one, turning the deliberately precise
operator copy into an account-existence oracle for every signed-in user. New
MultiSession.may_impersonate?/1shares its rule withauthorize_impersonation/2so the two cannot drift. - Existing users are no longer renamed when the admin form rebuilds its
changeset (#671) —
maybe_generate_username_from_email/1ran on every registration changeset and minted a username whenever the params carried none, which is exactly what the admin edit form sends; and the uniqueness walk asked the database whether the name was taken without excluding the user it was generating for, somaria’s own row mademarialook unavailable to maria. Opening a user and saving turnedmariaintomaria_1, thenmaria_2. - A generated username carries its uniqueness constraint (#671 review) —
generation runs at the end of
registration_changeset/3, aftervalidate_username/2has already decided there was no username change to guard, so the generated name reached the database unprotected. Two registrations racing on the same local part both settled on it and the loser got anEcto.ConstraintErrorraised out ofRepo.insert/1instead of an{:error, changeset}. - A queued message is intercepted once (#670) —
skip_queue: trueskipped only the queue offer, so a queued-then-drained message went throughintercept_before_send/2twice and a provider that logs per interception recorded every send twice. - Out-of-contract
maybe_enqueue/2returns are logged (#670) — a provider bug returning anything other than:continue/{:queued, ref}still sends (a bug must not eat the message) but no longer does so invisibly. - Removed seven
Logger.infocalls left in the admin user form from debugging the username rewrite (#671).
PhoenixKit 1.7.218. Install or update: {:phoenix_kit, "~> 1.7"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.