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

Redirects with custom flash messages #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions app/controllers/devise/masquerades_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}")

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/devise/masquerades_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down
Loading