From 4b3b4194a85acb9b266cd4caafda3454276f69e2 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 11 Mar 2019 22:27:49 +0100 Subject: [PATCH] Add Solidus admin unauthorized redirect handler If someone just requests '/admin' without being logged in Solidus renders '/unauthorized'. In solidus_auth_devise there is a redirect handler for this case that redirects to '/login'. In case we do not have solidus_auth_devise we need to provide a handler that redirects to Alchemy.login_path. Refs #32 --- lib/alchemy/solidus/engine.rb | 1 + lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb | 9 +++++++++ .../alchemy/solidus/install/install_generator.rb | 2 +- spec/features/alchemy/alchemy_admin_integrations_spec.rb | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index cfd4aa4..4de5635 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -26,6 +26,7 @@ class Engine < ::Rails::Engine if Alchemy.user_class_name == 'Alchemy::User' require 'alchemy/solidus/alchemy_user_extension' Alchemy::User.include Alchemy::Solidus::AlchemyUserExtension + require 'alchemy/solidus/spree_admin_unauthorized_redirect' end if SolidusSupport.solidus_gem_version < Gem::Version.new('2.5') diff --git a/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb b/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb new file mode 100644 index 0000000..6679543 --- /dev/null +++ b/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb @@ -0,0 +1,9 @@ +Spree::Admin::BaseController.unauthorized_redirect = -> do + if try_spree_current_user + flash[:error] = I18n.t('spree.authorization_failure') + redirect_to spree.root_path + else + store_location + redirect_to alchemy.login_path + end +end diff --git a/lib/generators/alchemy/solidus/install/install_generator.rb b/lib/generators/alchemy/solidus/install/install_generator.rb index 120cbfd..4390161 100644 --- a/lib/generators/alchemy/solidus/install/install_generator.rb +++ b/lib/generators/alchemy/solidus/install/install_generator.rb @@ -45,7 +45,7 @@ def run_spree_custom_user_generator if Kernel.const_defined?('Alchemy::Devise') && !options[:skip_spree_custom_user_generator] arguments = options[:auto_accept] ? ['Alchemy::User', '--force'] : ['Alchemy::User'] Spree::CustomUserGenerator.start(arguments) - gsub_file 'lib/spree/authentication_helpers.rb', /main_app\./, 'alchemy.' + gsub_file 'lib/spree/authentication_helpers.rb', /main_app\./, 'Alchemy.' if SolidusSupport.solidus_gem_version < Gem::Version.new('2.5.0') gsub_file 'config/initializers/spree.rb', /Spree\.user_class.?=.?.+$/, 'Spree.user_class = "Alchemy::User"' end diff --git a/spec/features/alchemy/alchemy_admin_integrations_spec.rb b/spec/features/alchemy/alchemy_admin_integrations_spec.rb index 03e3dfc..4ca4a82 100644 --- a/spec/features/alchemy/alchemy_admin_integrations_spec.rb +++ b/spec/features/alchemy/alchemy_admin_integrations_spec.rb @@ -7,6 +7,12 @@ require 'spree/testing_support/factories/address_factory' RSpec.feature "Admin Integration", type: :feature do + it 'gets redirected to login if accessing admin' do + visit '/admin' + + expect(page).to have_field 'user_login' + end + it 'it is possible to login and visit Alchemy admin' do login! visit '/admin/dashboard'