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

Login return to #805

Merged
merged 7 commits into from
Jul 2, 2024
Merged
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
10 changes: 9 additions & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class AdminController < ApplicationController

def set_session
if current_remote_user.nil?
session[:return_to] = request.url
# Its important that the return_to is NOT stored if:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate these comments! A nice gift to future us

# - The request method is not GET (non idempotent)
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an infinite redirect loop
# - The request is an Ajax request as this can lead to very unexpected behaviour
session[:return_to] = if request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
request.url
else
admin_root_path
end
redirect_to '/login'
end
session[:user_role] = 'admin'
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/approver_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class ApproverController < ApplicationController

def set_session
if current_remote_user.nil?
session[:return_to] = request.url
# Its important that the return_to is NOT stored if:
# - The request method is not GET (non idempotent)
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an infinite redirect loop
# - The request is an Ajax request as this can lead to very unexpected behaviour
session[:return_to] = if request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
request.url
else
approver_root_path
end
redirect_to '/login'
end
session[:user_role] = 'approver'
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/author_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class AuthorController < ApplicationController

def set_session
if current_remote_user.nil?
session[:return_to] = request.url
# Its important that the return_to is NOT stored if:
# - The request method is not GET (non idempotent)
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an infinite redirect loop
# - The request is an Ajax request as this can lead to very unexpected behaviour
session[:return_to] = if request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
request.url
else
author_root_path
end
redirect_to '/login'
end
session[:user_role] = 'author'
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,22 @@
assert_response :redirect, "<302: Found> redirect to </>"
end
end

describe 'storing session[:return_to]' do
it 'stores request url if valid url to return to or else it stores user root path' do
# urls with .json appended to the end is a common occcurence (datatables) that we consider to be invalid
get '/approver'
expect(session[:return_to]).to eq 'http://www.example.com/approver'
get '/approver.json'
expect(session[:return_to]).to eq '/approver'
get '/author'
expect(session[:return_to]).to eq 'http://www.example.com/author'
get '/author.json'
expect(session[:return_to]).to eq '/author'
get '/admin'
expect(session[:return_to]).to eq 'http://www.example.com/admin'
get '/admin.json'
expect(session[:return_to]).to eq '/admin'
end
end
end