Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impersonation prevent the redirect when admin session expired #865

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions app/admin/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
ActiveAdmin.register User do
permit_params :email, :first_name, :last_name, :username, :password, :password_confirmation

if ENV['IMPERSONATION_URL'].present?
member_action :impersonate, method: :post do
signed_data = Impersonation::Verifier.new.sign!(
user_id: resource.id, admin_user_id: current_admin_user.id
)
redirect_to "#{ENV.fetch('IMPERSONATION_URL')}?auth=#{signed_data}", allow_other_host: true
end
end

form do |f|
f.inputs 'Details' do
f.input :email
Expand Down Expand Up @@ -55,18 +64,9 @@
end

if ENV['IMPERSONATION_URL'].present?
action_item :user_impersonation, only: :show do
signed_data = Impersonation::Verifier.new.sign!(
user_id: resource.id, admin_user_id: current_admin_user.id
)

link_to_if Flipper[:impersonation_tool].enabled?,
"
<span class=\"#{'disabled_impersonate_button' unless Flipper[:impersonation_tool].enabled?}\">
Impersonate User
</span>
".html_safe, # rubocop:disable Rails/OutputSafety
"#{ENV.fetch('IMPERSONATION_URL')}?auth=#{signed_data}"
action_item :user_impersonation, only: :show, if: proc { Flipper.enabled?(:impersonation_tool) } do
link_to 'Impersonate User', impersonate_admin_user_path(resource), method: :post,
target: '_blank', rel: 'noopener'
end
end
end
6 changes: 5 additions & 1 deletion app/policies/admin/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

module Admin
class UserPolicy < Admin::ApplicationPolicy; end
class UserPolicy < Admin::ApplicationPolicy
def impersonate?
create? && Flipper.enabled?(:impersonation_tool)
end
end
end
17 changes: 17 additions & 0 deletions spec/policies/admin/user_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@
expect(subject).to permit(admin, user)
end
end

permissions :impersonate? do
let(:admin) { create(:admin_user) }
let(:user) { create(:user) }

it 'allow access when impersonate_tool is enable' do
allow(Flipper).to receive(:enabled?).with(:impersonation_tool).and_return(true)

expect(subject).to permit(admin, user)
end

it 'denies access when impersonate_tool is disable' do
allow(Flipper).to receive(:enabled?).with(:impersonation_tool).and_return(false)

expect(subject).not_to permit(admin, user)
end
end
end