Commit e2b45367 authored by Robert Speicher's avatar Robert Speicher Committed by Felipe Artur

Merge branch '28609-fix-redirect-to-home-page-url' into 'master'

Fix the redirect to custom home page URL and move it to RootController

Closes #28609

See merge request !9518
parent af064cde
...@@ -72,14 +72,6 @@ class ApplicationController < ActionController::Base ...@@ -72,14 +72,6 @@ class ApplicationController < ActionController::Base
end end
end end
def authenticate_user!(*args)
if redirect_to_home_page_url?
redirect_to current_application_settings.home_page_url and return
end
super(*args)
end
def log_exception(exception) def log_exception(exception)
application_trace = ActionDispatch::ExceptionWrapper.new(env, exception).application_trace application_trace = ActionDispatch::ExceptionWrapper.new(env, exception).application_trace
application_trace.map!{ |t| " #{t}\n" } application_trace.map!{ |t| " #{t}\n" }
...@@ -287,19 +279,6 @@ class ApplicationController < ActionController::Base ...@@ -287,19 +279,6 @@ class ApplicationController < ActionController::Base
session[:skip_tfa] && session[:skip_tfa] > Time.current session[:skip_tfa] && session[:skip_tfa] > Time.current
end end
def redirect_to_home_page_url?
# If user is not signed-in and tries to access root_path - redirect him to landing page
# Don't redirect to the default URL to prevent endless redirections
return false unless current_application_settings.home_page_url.present?
home_page_url = current_application_settings.home_page_url.chomp('/')
root_urls = [Gitlab.config.gitlab['url'].chomp('/'), root_url.chomp('/')]
return false if root_urls.include?(home_page_url)
current_user.nil? && root_path == request.path
end
# U2F (universal 2nd factor) devices need a unique identifier for the application # U2F (universal 2nd factor) devices need a unique identifier for the application
# to perform authentication. # to perform authentication.
# https://developers.yubico.com/U2F/App_ID.html # https://developers.yubico.com/U2F/App_ID.html
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
# `DashboardController#show`, which is the default. # `DashboardController#show`, which is the default.
class RootController < Dashboard::ProjectsController class RootController < Dashboard::ProjectsController
skip_before_action :authenticate_user!, only: [:index] skip_before_action :authenticate_user!, only: [:index]
before_action :redirect_to_custom_dashboard, only: [:index]
before_action :redirect_unlogged_user, if: -> { current_user.nil? }
before_action :redirect_logged_user, if: -> { current_user.present? }
def index def index
super super
...@@ -16,23 +18,38 @@ class RootController < Dashboard::ProjectsController ...@@ -16,23 +18,38 @@ class RootController < Dashboard::ProjectsController
private private
def redirect_to_custom_dashboard def redirect_unlogged_user
return redirect_to new_user_session_path unless current_user if redirect_to_home_page_url?
redirect_to(current_application_settings.home_page_url)
else
redirect_to(new_user_session_path)
end
end
def redirect_logged_user
case current_user.dashboard case current_user.dashboard
when 'stars' when 'stars'
flash.keep flash.keep
redirect_to starred_dashboard_projects_path redirect_to(starred_dashboard_projects_path)
when 'project_activity' when 'project_activity'
redirect_to activity_dashboard_path redirect_to(activity_dashboard_path)
when 'starred_project_activity' when 'starred_project_activity'
redirect_to activity_dashboard_path(filter: 'starred') redirect_to(activity_dashboard_path(filter: 'starred'))
when 'groups' when 'groups'
redirect_to dashboard_groups_path redirect_to(dashboard_groups_path)
when 'todos' when 'todos'
redirect_to dashboard_todos_path redirect_to(dashboard_todos_path)
else
return
end end
end end
def redirect_to_home_page_url?
# If user is not signed-in and tries to access root_path - redirect him to landing page
# Don't redirect to the default URL to prevent endless redirections
return false unless current_application_settings.home_page_url.present?
home_page_url = current_application_settings.home_page_url.chomp('/')
root_urls = [Gitlab.config.gitlab['url'].chomp('/'), root_url.chomp('/')]
root_urls.exclude?(home_page_url)
end
end end
---
title: Fix the redirect to custom home page URL
merge_request: 9518
author:
...@@ -2,6 +2,26 @@ require 'spec_helper' ...@@ -2,6 +2,26 @@ require 'spec_helper'
describe RootController do describe RootController do
describe 'GET index' do describe 'GET index' do
context 'when user is not logged in' do
it 'redirects to the sign-in page' do
get :index
expect(response).to redirect_to(new_user_session_path)
end
context 'when a custom home page URL is defined' do
before do
stub_application_setting(home_page_url: 'https://gitlab.com')
end
it 'redirects the user to the custom home page URL' do
get :index
expect(response).to redirect_to('https://gitlab.com')
end
end
end
context 'with a user' do context 'with a user' do
let(:user) { create(:user) } let(:user) { create(:user) }
...@@ -12,55 +32,60 @@ describe RootController do ...@@ -12,55 +32,60 @@ describe RootController do
context 'who has customized their dashboard setting for starred projects' do context 'who has customized their dashboard setting for starred projects' do
before do before do
user.update_attribute(:dashboard, 'stars') user.dashboard = 'stars'
end end
it 'redirects to their specified dashboard' do it 'redirects to their specified dashboard' do
get :index get :index
expect(response).to redirect_to starred_dashboard_projects_path expect(response).to redirect_to starred_dashboard_projects_path
end end
end end
context 'who has customized their dashboard setting for project activities' do context 'who has customized their dashboard setting for project activities' do
before do before do
user.update_attribute(:dashboard, 'project_activity') user.dashboard = 'project_activity'
end end
it 'redirects to the activity list' do it 'redirects to the activity list' do
get :index get :index
expect(response).to redirect_to activity_dashboard_path expect(response).to redirect_to activity_dashboard_path
end end
end end
context 'who has customized their dashboard setting for starred project activities' do context 'who has customized their dashboard setting for starred project activities' do
before do before do
user.update_attribute(:dashboard, 'starred_project_activity') user.dashboard = 'starred_project_activity'
end end
it 'redirects to the activity list' do it 'redirects to the activity list' do
get :index get :index
expect(response).to redirect_to activity_dashboard_path(filter: 'starred') expect(response).to redirect_to activity_dashboard_path(filter: 'starred')
end end
end end
context 'who has customized their dashboard setting for groups' do context 'who has customized their dashboard setting for groups' do
before do before do
user.update_attribute(:dashboard, 'groups') user.dashboard = 'groups'
end end
it 'redirects to their group list' do it 'redirects to their group list' do
get :index get :index
expect(response).to redirect_to dashboard_groups_path expect(response).to redirect_to dashboard_groups_path
end end
end end
context 'who has customized their dashboard setting for todos' do context 'who has customized their dashboard setting for todos' do
before do before do
user.update_attribute(:dashboard, 'todos') user.dashboard = 'todos'
end end
it 'redirects to their todo list' do it 'redirects to their todo list' do
get :index get :index
expect(response).to redirect_to dashboard_todos_path expect(response).to redirect_to dashboard_todos_path
end end
end end
...@@ -68,6 +93,7 @@ describe RootController do ...@@ -68,6 +93,7 @@ describe RootController do
context 'who uses the default dashboard setting' do context 'who uses the default dashboard setting' do
it 'renders the default dashboard' do it 'renders the default dashboard' do
get :index get :index
expect(response).to render_template 'dashboard/projects/index' expect(response).to render_template 'dashboard/projects/index'
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment