From a2f869b43e0740656ba51a483d640d58e341c8ff Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 25 Jan 2014 12:35:05 +0000 Subject: [PATCH] Redirect to back_url or referer when clicking "Sign in" while already logged-in (#15926). git-svn-id: http://svn.redmine.org/redmine/trunk@12705 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/account_controller.rb | 2 +- app/controllers/application_controller.rb | 5 ++++- test/functional/account_controller_test.rb | 20 +++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index a3768d403..034821107 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -34,7 +34,7 @@ class AccountController < ApplicationController def login if request.get? if User.current.logged? - redirect_to home_url + redirect_back_or_default home_url, :referer => true end else authenticate_user diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 152dc3f18..c4b01ed40 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -373,7 +373,7 @@ class ApplicationController < ActionController::Base url end - def redirect_back_or_default(default) + def redirect_back_or_default(default, options={}) back_url = params[:back_url].to_s if back_url.present? begin @@ -387,6 +387,9 @@ class ApplicationController < ActionController::Base logger.warn("Could not redirect to invalid URL #{back_url}") # redirect to default end + elsif options[:referer] + redirect_to_referer_or default + return end redirect_to default false diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 5423e065b..738371ab4 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -33,7 +33,25 @@ class AccountControllerTest < ActionController::TestCase assert_select 'input[name=password]' end - def test_get_login_while_logged_in_should_redirect_to_home + def test_get_login_while_logged_in_should_redirect_to_back_url_if_present + @request.session[:user_id] = 2 + @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1' + + get :login, :back_url => 'http://test.host/issues/show/1' + assert_redirected_to '/issues/show/1' + assert_equal 2, @request.session[:user_id] + end + + def test_get_login_while_logged_in_should_redirect_to_referer_without_back_url + @request.session[:user_id] = 2 + @request.env["HTTP_REFERER"] = 'http://test.host/issues/show/1' + + get :login + assert_redirected_to '/issues/show/1' + assert_equal 2, @request.session[:user_id] + end + + def test_get_login_while_logged_in_should_redirect_to_home_by_default @request.session[:user_id] = 2 get :login -- 2.39.5