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

Add a separate permission to access the admin panel #303

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions app/controllers/spree/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BaseController < ApplicationController
helper 'spree/currency'
layout 'spree/layouts/admin'

before_action :ensure_can_read_admin_panel
before_action :authorize_admin
before_action :load_stores

Expand All @@ -37,6 +38,17 @@ def authorize_admin
authorize! action, record
end

def ensure_can_read_admin_panel
begin
authorize! :read, Spree::Admin
rescue CanCan::AccessDenied
redirect_to spree.admin_access_denied_path
false
else
true
end
end

def redirect_unauthorized_access
if try_spree_current_user
flash[:error] = Spree.t(:authorization_failure)
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/spree/admin/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ module Spree
module Admin
class ErrorsController < BaseController
skip_before_action :authorize_admin
skip_before_action :ensure_can_read_admin_panel

def forbidden
render status: 403
render status: 403 if ensure_can_read_admin_panel
end

def access_denied
render status: 403, layout: nil
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/views/spree/admin/errors/access_denied.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
resources :webhooks_subscribers

get '/forbidden', to: 'errors#forbidden', as: :forbidden
get '/access-denied', to: 'errors#access_denied', as: :access_denied
resource :dashboard, controller: 'dashboard'

root to: 'dashboard#show'
Expand Down
Loading