Skip to content

Commit

Permalink
Refactor unauthorized access handling in SolidusAdmin
Browse files Browse the repository at this point in the history
Enhance the SolidusAdmin authorization mechanism to improve user 
experience during unauthorized access attempts. Now, instead of 
previous behavior, users are redirected to a dedicated unauthorized 
page when attempting to access a resource for which they do not have 
permission.
  • Loading branch information
rainerdema committed Oct 3, 2023
1 parent 89617d8 commit b06342d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module SolidusAdmin::ControllerHelpers::Authorization

included do
before_action :authorize_solidus_admin_user!

rescue_from CanCan::AccessDenied do
render 'unauthorized', status: :forbidden
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions admin/app/views/solidus_admin/base/unauthorized.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1><%= t('solidus_admin.errors.authorization.access_denied.title') %></h1>
<p><%= t('solidus_admin.errors.authorization.access_denied.description') %></p>
7 changes: 7 additions & 0 deletions admin/config/locales/errors.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
solidus_admin:
errors:
authorization:
access_denied:
title: "Access Denied"
description: "You are not authorized to access this page."
14 changes: 13 additions & 1 deletion admin/spec/controllers/solidus_admin/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ def index
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(nil)
end

it "redirects to unauthorized" do
it "redirects to unauthorized for no user" do
get :index
expect(response).to redirect_to '/unauthorized'
end

context "with a user without update permission" do
before do
user = create(:user, email: '[email protected]')
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(user)
end

it "redirects to unauthorized" do
get :index
expect(response).to have_http_status(:forbidden)
end
end
end

context "successful request" do
Expand Down

0 comments on commit b06342d

Please sign in to comment.