diff --git a/app/controllers/devise/masquerades_controller.rb b/app/controllers/devise/masquerades_controller.rb index d3cedb3..f896d9b 100644 --- a/app/controllers/devise/masquerades_controller.rb +++ b/app/controllers/devise/masquerades_controller.rb @@ -15,7 +15,7 @@ def show if send("#{masqueraded_resource_name}_masquerade?") resource = masquerading_current_user - go_back(resource, path: after_masquerade_full_path_for(resource)) + go_back(resource, path: after_masquerade_full_path_for(resource), message: "") else masqueradable_resource = find_masqueradable_resource @@ -33,14 +33,14 @@ def show masquerade_sign_in(resource) - go_back(resource, path: after_masquerade_full_path_for(resource)) + go_back(resource, path: after_masquerade_full_path_for(resource), message: after_masquerade_flash_message(resource)) end end def back unless send("#{masqueraded_resource_name}_masquerade?") resource = send("current_#{masqueraded_resource_name}") - go_back(resource, path: after_back_masquerade_path_for(resource)) + go_back(resource, path: after_back_masquerade_path_for(resource), message: "") else masqueradable_resource = send("current_#{masqueraded_resource_name}") @@ -54,7 +54,7 @@ def back sign_in(resource) request.env['devise.skip_trackable'] = nil - go_back(resource, path: after_back_masquerade_path_for(resource)) + go_back(resource, path: after_back_masquerade_path_for(resource), message: after_back_masquerade_flash_message(masqueradable_resource)) cleanup_masquerade_owner_session(masqueradable_resource) end @@ -88,10 +88,12 @@ def find_owner_resource(masqueradable_resource) end end - def go_back(user, path:) + def go_back(user, path:, message:) if Devise.masquerade_routes_back + flash[:notice] = message redirect_back(fallback_location: path) else + flash[:notice] = message redirect_to path end end @@ -132,6 +134,10 @@ def after_masquerade_path_for(resource) '/' end + def after_masquerade_flash_message(resource) + "" + end + def after_masquerade_full_path_for(resource) after_masquerade_path_for(resource) end @@ -140,6 +146,10 @@ def after_back_masquerade_path_for(resource) '/' end + def after_back_masquerade_flash_message(resource) + "" + end + def save_masquerade_owner_session(masqueradable_resource) guid = SecureRandom.uuid diff --git a/spec/controllers/devise/masquerades_controller_spec.rb b/spec/controllers/devise/masquerades_controller_spec.rb index 2e284e2..47eac9b 100644 --- a/spec/controllers/devise/masquerades_controller_spec.rb +++ b/spec/controllers/devise/masquerades_controller_spec.rb @@ -33,12 +33,16 @@ let(:mask) { create(:user) } before do + allow_any_instance_of(described_class).to( + receive(:after_masquerade_flash_message).with(mask).and_return("You are logged as an user")) + get :show, params: { id: mask.id, masquerade: mask.masquerade_key } end it { expect(cache_read(mask)).to be } it { expect(session["warden.user.user.key"].first.first).to eq(mask.id) } it { should redirect_to('/') } + it { expect(flash[:notice]).to match("You are logged as an user") } context 'and back' do before { get :back } @@ -81,12 +85,17 @@ context 'and back' do before do + allow_any_instance_of(described_class).to( + receive(:after_back_masquerade_flash_message).with(mask).and_return("You logged out from user's session") + ) + get :show, params: { id: mask.id, masquerade: mask.masquerade_key } get :back end it { should redirect_to(masquerade_page) } + it { expect(flash[:notice]).to match("You logged out from user's session") } end # context context 'and back fallback if http_referer not present' do