Skip to content

Commit

Permalink
Handle paths that include query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
project-eutopia committed Apr 19, 2018
1 parent 3f8eb3f commit 0f39ea0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/controllers/devise/masquerades_controller.rb
Expand Up @@ -31,11 +31,11 @@ def show
masquerade_sign_in(self.resource)

if Devise.masquerade_routes_back && Rails::VERSION::MAJOR == 5
redirect_back(fallback_location: "#{after_masquerade_param_for(self.resource)}?#{after_masquerade_param_for(resource)}")
redirect_back(fallback_location: after_masquerade_full_path_for(resource))
elsif Devise.masquerade_routes_back && request.env['HTTP_REFERER'].present?
redirect_to :back
else
redirect_to("#{after_masquerade_path_for(self.resource)}?#{after_masquerade_param_for(resource)}")
redirect_to(after_masquerade_full_path_for(resource))
end
end

Expand Down Expand Up @@ -105,6 +105,14 @@ def after_masquerade_path_for(resource)
"/"
end

def after_masquerade_full_path_for(resource)
if after_masquerade_path_for(resource) =~ /\?/
"#{after_masquerade_path_for(resource)}&#{after_masquerade_param_for(resource)}"
else
"#{after_masquerade_path_for(resource)}?#{after_masquerade_param_for(resource)}"
end
end

def after_masquerade_param_for(resource)
"#{Devise.masquerade_param}=#{resource.masquerade_key}"
end
Expand Down
6 changes: 5 additions & 1 deletion spec/controllers/devise/masquerades_controller_spec.rb
Expand Up @@ -44,9 +44,13 @@
end # context

context '< Rails 5, fallback if http_referer not present' do
before do
allow_any_instance_of(described_class).to receive(:after_masquerade_path_for).and_return("/dashboard?color=red")
end

before { get :show, id: mask.to_param }

it { should redirect_to("/?masquerade=secure_key") }
it { should redirect_to("/dashboard?color=red&masquerade=secure_key") }
end # context
end # context

Expand Down

0 comments on commit 0f39ea0

Please sign in to comment.