discourse/app/controllers/admin/impersonate_controller.rb
Ted Johansson b24a3d81ed
DEV: Allow impersonation without session swapping (#34213)
The current impersonation feature works by signing you in as the user you are impersonating. This has the side effect of invalidating your own session and forcing you to log out and in again.

In this experimental implementation you keep your existing session, but DefaultCurrentUserProvider returns the user being impersonated, allowing you to see the site from their perspective.
2025-08-21 14:18:15 +08:00

41 lines
1 KiB
Ruby

# 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)
# log impersonate
StaffActionLogger.new(current_user).log_impersonate(user)
if SiteSetting.experimental_impersonation
raise Discourse::InvalidAccess if current_user.is_impersonating
start_impersonating_user(user)
else
# Log on as the user
log_on_user(user, impersonate: true)
end
render body: nil
end
def destroy
raise Discourse::NotFound if !SiteSetting.experimental_impersonation
raise Discourse::InvalidAccess if !current_user.is_impersonating
puppet = current_user
stop_impersonating_user
StaffActionLogger.new(current_user).log_stop_impersonation(puppet)
render body: nil
end
end