Add callbacks for logging pundit scope resolutions and authorizations. #687
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found it useful in my development environment rails logs to log authorization and scoping actions from the controller. In my controller code, I have the following methods:
Which replaces the no-op callbacks from within this commit. This hook would also be useful if the application requires audit logging in a production environment.
Why don't I just log it from the authorization PORO? Great question: I could add a method that logs the objects that the policy is initialized with, but I then can't log the action that is subsequently called on the policy (known as the
query
from the pundit code) because its a basicpolicy.public_send(query)
.There's a lot I don't like about how this code is currently structured, so I'm posting the PR here for feedback before moving this forward on approaches.
Keep it as-is: log via callbacks
I don't particularly like this approach because it pollutes the controllers with more methods that don't seem necessary. It does work and requires minimal re-architecture to the internal instance variables.
Replace boolean
authorized
variables with anAuthorization
object.Within the Pundit code, there's various booleans set that tell Pundit if an authorization happened (or not). It looks like this:
I could replace the boolean values with an object that stores more information on the result of the authorization:
and some of the callback checks would be rewritten as:
The authorization object would store the response of whether or not the action is authorized:
This object could be passed around to a development logger or production audit logger and could then be checked by the callbacks that want to verify authorization happened.
Move the responsibility of authorization logging & callbacks into the policy itself
Pundit could be rigged up to call a method on the policy and scope object itself like
before_authorization
. That would look like this in the controller:And the policy would then optionally implement the following method:
The Scope object would have a similar method for logging as well.
The big question is where does the responsibility of logging for authorization actions go? Ideally it can be moved to one place with a single responsibility so the users of Pundit can override the behavior and customize authorization logging.