From e2940e75ca3b2b461916e556d39d3ba5be4a2370 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Fri, 25 Mar 2022 14:25:13 -0400 Subject: [PATCH] Adding :internal auditing for admin/spaces#update (#17010) There are two existing listeners for the `Audit::Logger`: `:moderator` and `:internal`. (Note: during tests we ignore the :moderator and :internal logs as defined in [config/initializers/audit_events.rb][1].) Using `rg "Audit::Logger\.log\(:internal," --files-with-matches`, the `:internal` listener is found in: - app/controllers/admin/secrets_controller.rb - app/controllers/admin/settings/base_controller.rb - app/controllers/admin/settings/general_settings_controller.rb Using `rg "Audit::Logger\.log\(:moderator," --files-with-matches`, the `:moderator` listener is used in: - app/controllers/rating_votes_controller.rb - app/controllers/comments_controller.rb - app/controllers/stories/pinned_articles_controller.rb - app/controllers/admin/response_templates_controller.rb - app/controllers/admin/tags_controller.rb - app/controllers/admin/articles_controller.rb - app/controllers/admin/users_controller.rb - app/controllers/admin/reactions_controller.rb - app/controllers/admin/tags/moderators_controller.rb - app/controllers/tag_adjustments_controller.rb - app/controllers/reactions_controller.rb The `admin/spaces#update` action is most similar to the `admin#settings` actions, which is why I chose `:internal`. I am looking for further guidance on documenting this little area of the application (in particular providing a data dictionary of :internal and :moderator). Closes forem/forem#16957 [1]:https://github.com/forem/forem/blob/main/config/initializers/audit_events.rb#L9-L11 --- app/controllers/admin/spaces_controller.rb | 7 +++---- app/services/audit/notification.rb | 15 ++++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/controllers/admin/spaces_controller.rb b/app/controllers/admin/spaces_controller.rb index 43a8d232f..6cb7bdfe6 100644 --- a/app/controllers/admin/spaces_controller.rb +++ b/app/controllers/admin/spaces_controller.rb @@ -5,10 +5,9 @@ module Admin class SpacesController < Admin::ApplicationController layout "admin" - # TODO: What kind of logging do we need? Any? Looking for guidance. I can assume we want to - # log changes to a space. - # - # after_action only: %i[update] { Audit::Logger.log(:moderator, current_user, params.dup) } + after_action only: %i[update] do + Audit::Logger.log(:internal, current_user, params.dup) + end # @note I'm instantiating the @space because in the index view I'm rendering a form that then # PUTs to the update action. diff --git a/app/services/audit/notification.rb b/app/services/audit/notification.rb index 0b127c4c9..1385cde26 100644 --- a/app/services/audit/notification.rb +++ b/app/services/audit/notification.rb @@ -1,12 +1,11 @@ module Audit + ## + # Main class for wrapping ActiveSupport Instrumentation API. + # + # This class represent main entry point for receiving and notifying custom + # events, implemented according to + # https://guides.rubyonrails.org/active_support_instrumentation.html#creating-custom-events class Notification - ## - # Main class for wrapping ActiveSupport Instrumentation API. - # - # This class represent main entry point for receiving and notifying custom - # events, implemented according to - # https://guides.rubyonrails.org/active_support_instrumentation.html#creating-custom-events - class << self include Audit::Helper @@ -22,7 +21,6 @@ module Audit # payload.user_id = current_user.id # payload.roles = current_user.roles.pluck(:name) # end - def notify(listener, &block) return unless block @@ -32,7 +30,6 @@ module Audit ## # Audit::Notification.listen receives Events sent from ActiveSupport Instrumentation API. # Then, this event is serialized and send to background job. - def listen(*args) event = ActiveSupport::Notifications::Event.new(*args) AuditLog.create!(params_hash(event))