0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/lib/impersonator_constraint.rb
Ted Johansson 66c55f60b4
FEATURE: Impersonation countdown (#39871)
### What is this change?

This PR adds a countdown to the impersonation banner, corresponding to
the time left out of the allotted time in the site setting. When the
countdown reaches 0 impersonation ends.

As part of this change, I made the `impersonate#destroy` endpoint
idempotent and visible to all admins, whether they are impersonating or
not. This prevents any "unexpected errors" from happening if there's a
race to end impersonation.

**Screenshot:**

<img width="537" height="79" alt="Screenshot 2026-05-11 at 11 26 08 AM"
src="https://github.com/user-attachments/assets/e44f6ef0-30ec-4050-ad08-e555c0e9f80a"
/>
2026-05-12 12:12:25 +08:00

20 lines
638 B
Ruby
Vendored

# frozen_string_literal: true
# This constraint accounts for admins who are impersonating other users
# and are therefore currently not identified as admins in their session.
class ImpersonatorConstraint
def matches?(request)
current_user = CurrentUser.lookup_from_env(request.env)
# An admin who's not impersonating anyone. Cool.
return true if current_user&.admin?
# An admin who's impersonating someone. Also cool.
# We know this because only admins can impersonate others.
return true if current_user&.is_impersonating
false
rescue Discourse::InvalidAccess, Discourse::ReadOnly
false
end
end