From 93b35fcecc14d186a732865629e40b05606f7a82 Mon Sep 17 00:00:00 2001 From: taitus Date: Fri, 23 Dec 2022 13:46:42 +0100 Subject: [PATCH] Redirect root path requests to the tenants administration When the `multitenancy_management_mode` is enabled. In order to avoid infinite redirects when regular users try to access the admin section, we're redirecting to the account page in this case. Otherwise, the admin section would redirect to the root path, which would redirect to the admin section, which would redirect to the root path, and so on. --- .../concerns/access_denied_handler.rb | 8 ++++- config/routes.rb | 4 +++ .../multitenancy_management_mode_spec.rb | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/access_denied_handler.rb b/app/controllers/concerns/access_denied_handler.rb index 080a77283..95edbbfe9 100644 --- a/app/controllers/concerns/access_denied_handler.rb +++ b/app/controllers/concerns/access_denied_handler.rb @@ -4,7 +4,13 @@ module AccessDeniedHandler included do rescue_from CanCan::AccessDenied do |exception| respond_to do |format| - format.html { redirect_to main_app.root_path, alert: exception.message } + format.html do + if Rails.application.multitenancy_management_mode? + redirect_to main_app.account_path, alert: exception.message + else + redirect_to main_app.root_path, alert: exception.message + end + end format.json { render json: { error: exception.message }, status: :forbidden } end end diff --git a/config/routes.rb b/config/routes.rb index becc72b7d..969150e6a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,10 @@ Rails.application.routes.draw do draw :admin draw :devise + constraints lambda { |request| Rails.application.multitenancy_management_mode? } do + get "/", to: "admin/tenants#index" + end + constraints lambda { |request| !Rails.application.multitenancy_management_mode? } do draw :budget draw :comment diff --git a/spec/system/multitenancy_management_mode_spec.rb b/spec/system/multitenancy_management_mode_spec.rb index 0780cff14..943ed8331 100644 --- a/spec/system/multitenancy_management_mode_spec.rb +++ b/spec/system/multitenancy_management_mode_spec.rb @@ -25,4 +25,34 @@ describe "Multitenancy management mode", :admin do expect(page).to have_css "li", count: 2 end end + + scenario "redirects root path requests to the admin tenants path" do + visit root_path + + expect(page).to have_content "CONSUL ADMINISTRATION", normalize_ws: true + expect(page).to have_content "Multitenancy" + expect(page).not_to have_content "Most active proposals" + end + + scenario "does not redirect other tenants when visiting the root path", :seed_tenants do + create(:tenant, schema: "mars") + + with_subdomain("mars") do + visit root_path + + expect(page).to have_content "Most active proposals" + expect(page).not_to have_content "Multitenancy" + expect(page).not_to have_content "CONSUL ADMINISTRATION", normalize_ws: true + end + end + + scenario "redirects to account path when regular users try to access the admin section" do + logout + login_as(create(:user)) + + visit admin_root_path + + expect(page).to have_current_path account_path + expect(page).to have_content "You do not have permission to access this page." + end end