discourse/app/controllers/admin/impersonate_controller.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

32 lines
796 B
Ruby
Vendored

# frozen_string_literal: true
class Admin::ImpersonateController < Admin::AdminController
skip_before_action :ensure_admin, only: :destroy
def create
params.require(:username_or_email)
user = User.find_by_username_or_email(params[:username_or_email])
raise Discourse::NotFound if user.blank?
guardian.ensure_can_impersonate!(user)
StaffActionLogger.new(current_user).log_impersonate(user)
raise Discourse::InvalidAccess if current_user.is_impersonating
start_impersonating_user(user)
render body: nil
end
def destroy
if current_user.is_impersonating
impersonated_user = current_user
stop_impersonating_user
StaffActionLogger.new(current_user).log_stop_impersonation(impersonated_user)
end
render body: nil
end
end