Skip to content

Commit

Permalink
Add Solidus admin unauthorized redirect handler
Browse files Browse the repository at this point in the history
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 AlchemyCMS#32
  • Loading branch information
tvdeyen committed Mar 11, 2019
1 parent b2d497c commit 4b3b419
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/alchemy/solidus/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 9 additions & 0 deletions lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/features/alchemy/alchemy_admin_integrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 4b3b419

Please sign in to comment.