Skip to content

Commit

Permalink
Impersonation prevent the redirect when admin session expired (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
blacksam07 authored Nov 12, 2024
1 parent eb6f99e commit 1d89062
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
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

0 comments on commit 1d89062

Please sign in to comment.