Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Admin: Support new `spree_admin_login_path` helper and `Spree.admin_u… (#12251)

Damian Legawiec71071a440110
diff --git a/admin/app/controllers/spree/admin/base_controller.rb b/admin/app/controllers/spree/admin/base_controller.rb
index 2dd6f15104..0d7f4d5414 100644
--- a/admin/app/controllers/spree/admin/base_controller.rb
+++ b/admin/app/controllers/spree/admin/base_controller.rb
@@ -39,21 +39,30 @@ module Spree
def redirect_unauthorized_access
if try_spree_current_user
flash[:error] = Spree.t(:authorization_failure)
- redirect_to spree.admin_forbidden_path
+ redirect_to spree.admin_forbidden_path, allow_other_host: true
else
store_location
- if defined?(spree.admin_login_path)
- redirect_to spree.admin_login_path
+ if defined?(spree_admin_login_path)
+ redirect_to spree_admin_login_path, allow_other_host: true
elsif respond_to?(:spree_login_path)
- redirect_to spree_login_path
+ redirect_to spree_login_path, allow_other_host: true
elsif spree.respond_to?(:root_path)
- redirect_to spree.root_path
+ redirect_to spree.root_path, allow_other_host: true
else
redirect_to main_app.respond_to?(:root_path) ? main_app.root_path : '/'
end
end
end
+ def try_spree_current_user
+ if Spree.admin_user_class
+ send("current_#{Spree.admin_user_class.model_name.singular_route_key}")
+ else
+ # use Spree::Core::ControllerHelpers::Auth#try_spree_current_user
+ super
+ end
+ end
+
def flash_message_for(object, event_sym)
if object.is_a?(ActiveRecord::Relation)
resource_desc = object.model.model_name.human.pluralize
diff --git a/admin/app/controllers/spree/admin/errors_controller.rb b/admin/app/controllers/spree/admin/errors_controller.rb
new file mode 100644
index 0000000000..1182704050
--- /dev/null
+++ b/admin/app/controllers/spree/admin/errors_controller.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Admin
+ class ErrorsController < BaseController
+ skip_before_action :authorize_admin
+
+ def forbidden
+ render status: 403
+ end
+ end
+ end
+end
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index bbc779f1e0..3d3fc786eb 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -42,5 +42,8 @@ Spree::Core::Engine.add_routes do
# developer tools
resources :oauth_applications
resources :webhooks_subscribers
+
+ # errors
+ get '/forbidden', to: 'errors#forbidden', as: :forbidden
end
end
diff --git a/admin/spec/controllers/spree/admin/base_controller_spec.rb b/admin/spec/controllers/spree/admin/base_controller_spec.rb
new file mode 100644
index 0000000000..2af196d9b2
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/base_controller_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+class AdminFakesController < Spree::Admin::BaseController
+ def index
+ render plain: 'index'
+ end
+end
+
+describe Spree::Admin::BaseController, type: :controller do
+ controller(Spree::Admin::BaseController) do
+ def index
+ authorize! :update, Spree::Order
+ render plain: 'test'
+ end
+ end
+
+ describe '#redirect_unauthorized_access' do
+ controller(AdminFakesController) do
+ def index
+ redirect_unauthorized_access
+ end
+ end
+ context 'when logged in' do
+ before do
+ allow(controller).to receive_messages(try_spree_current_user: double('User', id: 1, last_incomplete_spree_order: nil,
+ persisted?: true, selected_locale: nil))
+ end
+
+ it 'redirects forbidden path' do
+ get :index
+ expect(response).to redirect_to('/admin/forbidden')
+ end
+ end
+
+ context 'when guest user' do
+ before do
+ allow(controller).to receive_messages(try_spree_current_user: nil)
+ end
+
+ it 'redirects admin login path' do
+ allow(controller).to receive_messages(spree_admin_login_path: '/admin/login')
+ get :index
+ expect(response).to redirect_to('/admin/login')
+ end
+
+ it 'redirects login path' do
+ allow(controller).to receive_messages(spree_login_path: '/login')
+ get :index
+ expect(response).to redirect_to('/login')
+ end
+
+ context 'redirects to root' do
+ it 'of spree' do
+ allow(controller).to receive_message_chain(:spree, :root_path).and_return('/root')
+ get :index
+ expect(response).to redirect_to '/root'
+ end
+
+ it 'of main app' do
+ get :index
+ expect(response).to redirect_to '/'
+ end
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/core/controller_helpers/auth.rb b/core/lib/spree/core/controller_helpers/auth.rb
index 517b23e975..88f4e87974 100644
--- a/core/lib/spree/core/controller_helpers/auth.rb
+++ b/core/lib/spree/core/controller_helpers/auth.rb
@@ -70,31 +70,6 @@ module Spree
current_spree_user
end
end
-
- # Redirect as appropriate when an access request fails. The default action is to redirect to the login screen.
- # Override this method in your controllers if you want to have special behavior in case the user is not authorized
- # to access the requested action. For example, a popup window might simply close itself.
- def redirect_unauthorized_access
- Spree::Deprecation.warn(<<-DEPRECATION, caller)
- Core::ControllerHelpers#redirect_unauthorized_access is deprecated and will be removed in Spree 5.0.
- This method is implemented differently for Storefront and Admin
- DEPRECATION
- if try_spree_current_user
- flash[:error] = Spree.t(:authorization_failure)
- redirect_to spree.forbidden_path
- else
- store_location
- if Spree.respond_to?(:admin_path) && request.fullpath.match(Spree.admin_path) && defined?(spree.admin_login_path)
- redirect_to spree.admin_login_path
- elsif respond_to?(:spree_login_path)
- redirect_to spree_login_path
- elsif spree.respond_to?(:root_path)
- redirect_to spree.root_path
- else
- redirect_to main_app.respond_to?(:root_path) ? main_app.root_path : '/'
- end
- end
- end
end
end
end
diff --git a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
index d082c57cfc..909574de29 100644
--- a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
@@ -85,42 +85,4 @@ describe Spree::Core::ControllerHelpers::Auth, type: :controller do
expect(controller.try_spree_current_user).to eq nil
end
end
-
- describe '#redirect_unauthorized_access' do
- controller(FakesController) do
- def index
- redirect_unauthorized_access
- end
- end
-
- context 'when logged in' do
- before do
- allow(controller).to receive_messages(try_spree_current_user: double('User', id: 1, last_incomplete_spree_order: nil, selected_locale: nil))
- end
-
- it 'redirects forbidden path' do
- allow(controller).to receive_message_chain(:spree, :forbidden_path).and_return('/forbidden')
- get :index
- expect(response).to redirect_to('/forbidden')
- end
- end
-
- context 'when guest user' do
- before do
- allow(controller).to receive_messages(try_spree_current_user: nil)
- end
-
- it 'redirects login path' do
- allow(controller).to receive_messages(spree_login_path: '/login')
- get :index
- expect(response).to redirect_to('/login')
- end
-
- it 'redirects root path' do
- allow(controller).to receive_message_chain(:spree, :root_path).and_return('/root_path')
- get :index
- expect(response).to redirect_to('/root_path')
- end
- end
- end
end