-
Notifications
You must be signed in to change notification settings - Fork 67
Conversation
…athanl#118 Added option to enable logging for all authority interactions, as per nathanl#118 Logging will utilize Authority.info, and will only occur in instance within which a SecurityViolation is not generated
… actually happening.
lib/authority/controller.rb
Outdated
# This method can be overloaded inside the application controller, similar to authority_forbidden. | ||
def authority_success(user, action, resource) | ||
if Authority.configuration.log_success | ||
Authority.logger.info("#{user} successfully performed the #{action} action to this resource: #{resource}") |
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.
A couple of thoughts:
- We don't actually know that they successfully performed the action, only that they successfully made it through the authorization step. We should log something like "user was authorized to perform the #{action} to the resource #{resource}"
- Is there a good reason to make this a controller method, or a method the user can override?
authority_forbidden
renders403
(a controller responsibility) and some people might want to show404
or something else (so it probably needs to be overridable). But it seems likeAuthority.enforce
could just say "if thelog_attempts
option is true, log the attempt."
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.
-
Fair point. I think the function name makes sense, since we successfully passed through authority.. but the default message should be changed. Will fix that.
-
For my personal need I have to overwrite it because I need to capture specific information in the request such as path details and controller methods. Also figured that if it were a controller method other users would see the need to extend it as well since it's similar to the
authority_forbidden
action. At first I did have this being logged viaAuthority.enforce
, but overriding that was less clean than just making it a controller method.
Really up to you! It's easy to make the change, but I think it's more restrictive.
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.
Makes sense.
One other thought - if authority_success
can be overridden to do anything the user wants, the config option log_success
isn't necessarily an accurate name. What if we don't even have a config option, and authority_success
has a default implementation that does nothing? Then the instructions are "if you want to log successful authorization or do anything else about it, define authority_success
however you like."
…lways be called, but won't actually do anything unless the implementing app decides to override
@nathanl I think what you said makes sense. I made those changes. It's much simpler now. I also removed the test coverage since the function doesn't actually do anything. |
👍 |
This PR introduces the
authority_success
controller method, which can be utilized to log successful authorization events. This must be enabled within the configuration by settingconfig.log_success = true
within the initializer.This method will log via
Authority.logger.info
by default, but can be overloaded in the controller similar to howauthority_forbidden
is. As an example:This may not be the best way to handle things. For instance, you may prefer that the
authority_success
action operating on an object similar toSecurityViolation
. I didn't see the need for this for my use case.What do you think?