-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Support default Pundit policy class. #3323
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ def scope_collection(collection, action = Auth::READ) | |
# scoping is appliable only to read/index action | ||
# which means there is no way how to scope other actions | ||
Pundit.policy_scope!(user, collection) | ||
rescue Pundit::NotDefinedError | ||
default_policy_class::Scope.new(user, collection).resolve | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be: rescue Pundit::NotDefinedError => e
if default_policy_class && default_policy_class.const_defined?(:Scope)
default_policy_class::Scope.new(user, collection).resolve
else
raise e
end
end |
||
end | ||
|
||
def retrieve_policy(subject) | ||
|
@@ -25,6 +27,12 @@ def retrieve_policy(subject) | |
when Class then Pundit.policy!(user, subject.new) | ||
else Pundit.policy!(user, subject) | ||
end | ||
rescue Pundit::NotDefinedError => e | ||
if default_policy_class | ||
default_policy(user, subject) | ||
else | ||
raise e | ||
end | ||
end | ||
|
||
def format_action(action, subject) | ||
|
@@ -38,6 +46,16 @@ def format_action(action, subject) | |
end | ||
end | ||
|
||
private | ||
|
||
def default_policy_class | ||
ActiveAdmin.application.pundit_default_policy | ||
end | ||
|
||
def default_policy(user, subject) | ||
default_policy_class.new(user, subject) | ||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,11 @@ ActiveAdmin.setup do |config| | |
# CanCanAdapter or make your own. Please refer to documentation. | ||
# config.authorization_adapter = ActiveAdmin::CanCanAdapter | ||
|
||
# In case you prefer Pundit over other solutions you can here pass | ||
# the name of default policy class. This policy will be used in every | ||
# case when Pundit is unable to find suitable policy. | ||
# config.pundit_default_policy = MyDefaultPunditPolicy | ||
|
||
# You can customize your CanCan Ability class name here. | ||
# config.cancan_ability_class = "Ability" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's weird that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is even another reason for that, setting a class here results in a caching problem in dev env. |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,10 @@ | |
expect(application.allow_comments).to eq true | ||
end | ||
|
||
it "should set default Pundit policy class" do | ||
application.default_pundit_policy = policy_klass | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't only check the setting, you should check that is set too. |
||
|
||
describe "authentication settings" do | ||
|
||
it "should have no default current_user_method" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of being in
application.rb
, this should be at the top ofpundit_adapter.rb
similar tocancan_adapter.rb