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 callbacks for logging pundit scope resolutions and authorizations. #687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ def policy_scope(scope)

protected

# @return [void] called by authorize after the policy is queried for purposes
# like logging, etc.
def after_pundit_authorize_resolution(query:, record:, policy:, user:)
end

# @return [void] called by policy_scope after the policy scope is resolved for
# purposes like logging, etc.
def after_pundit_policy_scope_resolution(policy_scope:, user:, scope:)
end

# @return [Boolean] whether authorization has been performed, i.e. whether
# one {#authorize} or {#skip_authorization} has been called
def pundit_policy_authorized?
Expand Down Expand Up @@ -220,6 +230,8 @@ def authorize(record, query = nil, policy_class: nil)

policy = policy_class ? policy_class.new(pundit_user, record) : policy(record)

after_pundit_authorize_resolution query: query, record: record, policy: policy, user: pundit_user

raise NotAuthorizedError, query: query, record: record, policy: policy unless policy.public_send(query)

record.is_a?(Array) ? record.last : record
Expand Down Expand Up @@ -249,7 +261,9 @@ def skip_policy_scope
# @return [Scope{#resolve}, nil] instance of scope class which can resolve to a scope
def policy_scope(scope, policy_scope_class: nil)
@_pundit_policy_scoped = true
policy_scope_class ? policy_scope_class.new(pundit_user, scope).resolve : pundit_policy_scope(scope)
policy_scope = policy_scope_class ? policy_scope_class.new(pundit_user, scope).resolve : pundit_policy_scope(scope)
after_pundit_policy_scope_resolution policy_scope: policy_scope, user: pundit_user, scope: scope
policy_scope
end

# Retrieves the policy for the given record.
Expand Down