-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move authorization checks outside of the backend "auth" adapter
Those are not dependent on the authentication system.
- Loading branch information
Showing
4 changed files
with
34 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
admin/app/controllers/solidus_admin/controller_helpers/authorization.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin::ControllerHelpers::Authorization | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :authorize_solidus_admin_user! | ||
end | ||
|
||
private | ||
|
||
def current_ability | ||
@current_ability ||= Spree::Ability.new(current_solidus_admin_user) | ||
end | ||
|
||
def authorize_solidus_admin_user! | ||
subject = authorization_subject | ||
|
||
authorize! :admin, subject | ||
authorize! action_name, subject | ||
end | ||
|
||
def authorization_subject | ||
"Spree::#{controller_name.classify}".constantize | ||
rescue NameError | ||
raise NotImplementedError, "Couldn't infer the model class from the controller name, " \ | ||
"please implement `#{self.class}#authorization_subject`." | ||
end | ||
end |