Skip to content

Commit

Permalink
Extract redirection behavior from lambda to classes
Browse files Browse the repository at this point in the history
We have to lambdas, so we need two classes.
  • Loading branch information
mamhoff committed Dec 21, 2024
1 parent 8cf0eec commit fad97b8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
33 changes: 33 additions & 0 deletions app/models/spree/auth/unauthorized_admin_access_handler.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions app/models/spree/auth/unauthorized_customer_access_handler.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 2 additions & 18 deletions lib/spree/auth/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fad97b8

Please sign in to comment.