diff --git a/app/models/spree/auth/unauthorized_admin_access_handler.rb b/app/models/spree/auth/unauthorized_admin_access_handler.rb new file mode 100644 index 0000000..11ca58f --- /dev/null +++ b/app/models/spree/auth/unauthorized_admin_access_handler.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Spree + module Auth + # This service object is responsible for handling unauthorized redirects + class UnauthorizedAdminAccessHandler + # @param controller [ApplicationController] an instance of ApplicationController + # or its subclasses. + def initialize(controller) + @controller = controller + end + + # This method is responsible for handling unauthorized redirects + def call + if spree_current_user + flash[:error] = I18n.t('spree.authorization_failure') + + redirect_to(spree.admin_unauthorized_path) + else + store_location + + redirect_to(spree.admin_login_path) + end + end + + private + + attr_reader :controller + + delegate :flash, :redirect_to, :spree_current_user, :store_location, :spree, to: :controller + end + end +end diff --git a/app/models/spree/auth/unauthorized_customer_access_handler.rb b/app/models/spree/auth/unauthorized_customer_access_handler.rb new file mode 100644 index 0000000..dd51069 --- /dev/null +++ b/app/models/spree/auth/unauthorized_customer_access_handler.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Spree + module Auth + # This service object is responsible for handling unauthorized redirects + class UnauthorizedCustomerAccessHandler + # @param controller [ApplicationController] an instance of ApplicationController + # or its subclasses. + def initialize(controller) + @controller = controller + end + + # This method is responsible for handling unauthorized redirects + def call + if spree_current_user + flash[:error] = I18n.t('spree.authorization_failure') + + redirect_back(fallback_location: spree.unauthorized_path) + else + store_location + + redirect_back(fallback_location: spree.login_path) + end + end + + private + + attr_reader :controller + + delegate :flash, :redirect_back, :spree_current_user, :store_location, :spree, to: :controller + end + end +end diff --git a/lib/spree/auth/engine.rb b/lib/spree/auth/engine.rb index c22b3a1..0c2aa82 100644 --- a/lib/spree/auth/engine.rb +++ b/lib/spree/auth/engine.rb @@ -30,29 +30,13 @@ class Engine < Rails::Engine def self.prepare_backend Spree::Admin::BaseController.unauthorized_redirect = -> do - if spree_current_user - flash[:error] = I18n.t('spree.authorization_failure') - - redirect_to(spree.admin_unauthorized_path) - else - store_location - - redirect_to(spree.admin_login_path) - end + Spree::Auth::UnauthorizedAdminAccessHandler.new(self).call end end def self.prepare_frontend Spree::BaseController.unauthorized_redirect = -> do - if spree_current_user - flash[:error] = I18n.t('spree.authorization_failure') - - redirect_back(fallback_location: spree.unauthorized_path) - else - store_location - - redirect_back(fallback_location: spree.login_path) - end + Spree::Auth::UnauthorizedCustomerAccessHandler.new(self).call end end end