mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-10 07:11:00 +08:00
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.
41 lines
1 KiB
Ruby
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
|