Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Ensure action_name is passed as symbol for cancancan authorization #5399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -17,7 +21,7 @@ def authorize_solidus_admin_user!
subject = authorization_subject

authorize! :admin, subject
authorize! action_name, subject
authorize! action_name.to_sym, subject
end

def authorization_subject
Expand Down
4 changes: 4 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,4 @@
<div class="p-4">
<h1 class="text-3xl font-semibold text-solidusRed mb-4"><%= t('solidus_admin.errors.authorization.access_denied.title') %></h1>
<p class="text-lg text-gray-700"><%= t('solidus_admin.errors.authorization.access_denied.description') %></p>
</div>
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