discourse/lib/current_user.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

55 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module CurrentUser
def self.has_auth_cookie?(env)
Discourse.current_user_provider.new(env).has_auth_cookie?
end
def self.lookup_from_env(env)
Discourse.current_user_provider.new(env).current_user
end
# can be used to pretend current user does no exist, for CSRF attacks
def clear_current_user
@current_user_provider = Discourse.current_user_provider.new({})
end
def log_on_user(user, opts = {})
current_user_provider.log_on_user(user, session, cookies, opts)
user.logged_in
end
def log_off_user
current_user_provider.log_off_user(session, cookies)
end
def start_impersonating_user(user)
current_user_provider.start_impersonating_user(user)
end
def stop_impersonating_user
current_user_provider.stop_impersonating_user
end
def is_api?
current_user_provider.is_api?
end
def is_user_api?
current_user_provider.is_user_api?
end
def current_user
current_user_provider.current_user
end
def refresh_session(user)
current_user_provider.refresh_session(user, session, cookies)
end
private
def current_user_provider
@current_user_provider ||= Discourse.current_user_provider.new(request.env)
end
end