Skip to content

Commit

Permalink
Parse referrer from omniauth.origin if present
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Jan 23, 2025
1 parent 6ec1f5b commit 78df36a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ detectors:
- AccountController#cancel_ill_success
- AccountController#current_patron
- ApplicationController#default_url_options
- ApplicationController#referrer_from_url
- BookmarksController#csv_bom
- BookmarksController#two_values
- Orangelight::Catalog#online_holding_note?
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ApplicationController < ActionController::Base

def after_sign_in_path_for(resource)
stored_location = stored_location_for(resource)

if referrer.present? && (referrer.exclude?("sign_in") && !origin&.include?("redirect-to-alma"))
referrer
elsif origin.present?
Expand All @@ -24,7 +23,7 @@ def after_sign_in_path_for(resource)
origin.chomp('/email')
elsif !request.env['omniauth.origin'].nil? &&
/request|borrow-direct|email|bookmarks|search_history|redirect-to-alma/.match(request.env['omniauth.origin'])
request.env['omniauth.origin']
referrer_from_url(request.env['omniauth.origin'])
elsif stored_location.present?
stored_location
else
Expand Down Expand Up @@ -66,6 +65,15 @@ def verify_admin!
head :forbidden unless current_user.admin?
end

def referrer_from_url(url)
query = URI.parse(url).query
if query
CGI.parse(query).try(:[], "referer")&.first
else
url
end
end

before_action do
Rack::MiniProfiler.authorize_request if current_user&.admin?
end
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
get '/users/sign_in/'
expect(response).to redirect_to('/bookmarks')
end
context 'if the origin is the account/digitization_requests page' do
around do |example|
Rails.application.env_config["omniauth.origin"] = '/users/sign_in?referer=%2Faccount%2Fdigitization_requests'
example.run
Rails.application.env_config.except!("omniauth.origin")
end

it 'sends the user back to the account/digitization_requests page' do
get '/users/sign_in'
expect(response).to redirect_to('/account/digitization_requests')
end
end
end
# rubocop:disable RSpec/AnyInstance
context 'only with devise stored_location_for' do
Expand Down

0 comments on commit 78df36a

Please sign in to comment.