-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove staging db dump * Adding auditing capabilities * fixed mispelled word
- Loading branch information
1 parent
d0aae0d
commit 5e880b1
Showing
21 changed files
with
376 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module SystemAdmin | ||
class AuditsController < AdminController | ||
include Pagy::Backend | ||
def index | ||
@pagy, @audits = pagy(Audited::Audit.all.order(created_at: :desc), items: 10) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module AuditMessageHelper | ||
def audit_message(audit) | ||
method = routing_map[audit.auditable_type] || :generic_audit_message | ||
message = send(method, audit) | ||
message += "#{timestamp(audit)} (#{time_ago_in_words(audit.created_at)} ago)" | ||
message.html_safe # rubocop:disable Rails/OutputSafety | ||
end | ||
|
||
private | ||
|
||
def routing_map | ||
{ | ||
"ApplicationProgress" => :application_progress_audit_message, | ||
"User" => :user_audit_message, | ||
"AppSettings" => :app_settings_audit_message, | ||
} | ||
end | ||
|
||
def application_progress_audit_message(audit) | ||
email_tag = govuk_tag(text: audit.user&.email || "unknown", colour: "grey") | ||
urn_text = govuk_link_to(audit.auditable&.application&.urn, applicant_path(audit.auditable&.application&.applicant)) | ||
urn_tag = govuk_tag(text: urn_text, colour: "orange") | ||
"#{email_tag} #{action_tag(audit)} application progress #{urn_tag}.</br>It changed: <ul>#{audited_changes_summary(audit)}</ul>" | ||
end | ||
|
||
def app_settings_audit_message(audit) | ||
email_tag = govuk_tag(text: audit.user&.email || "unknown", colour: "grey") | ||
"#{email_tag} #{action_tag(audit)} application settings.</br>It changed: <ul>#{audited_changes_summary(audit)}</ul>" | ||
end | ||
|
||
def user_audit_message(audit) | ||
email_tag = govuk_tag(text: audit.user&.email || "unknown", colour: "grey") | ||
case audit.action | ||
when "create" | ||
"#{email_tag} #{action_tag(audit)} a new User entity #{govuk_tag(text: audit.audited_changes['email'], colour: 'yellow')}" | ||
when "update" | ||
"#{email_tag} #{action_tag(audit)} User entity.</br>It changed: <ul>#{audited_changes_summary(audit)}</ul>" | ||
when "destroy" | ||
"#{email_tag} #{action_tag(audit)} User entity #{govuk_tag(text: audit.audited_changes['email'], colour: 'yellow')}<br />" | ||
end | ||
end | ||
|
||
def generic_audit_message(audit) | ||
email_tag = govuk_tag(text: audit.user&.email || "unknown", colour: "grey") | ||
"#{email_tag} #{action_tag(audit)}<br />" | ||
end | ||
|
||
def audited_changes_summary(audit) | ||
audit.audited_changes.except("status").map { |key, value| | ||
"<li>#{key} (from: \"#{changed_value(value.first)}\", to: \"#{changed_value(value.last)}\")</li>" | ||
}.join | ||
end | ||
|
||
def timestamp(audit) | ||
govuk_tag(text: audit.created_at.strftime("%d/%m/%Y %H:%M:%S"), colour: "grey") | ||
end | ||
|
||
def action_tag(audit) | ||
govuk_tag(text: past_tense_action(audit.action), colour: action_colour(audit.action)) | ||
end | ||
|
||
def past_tense_action(action) | ||
{ | ||
"create" => "created", | ||
"update" => "updated", | ||
"destroy" => "destroyed", | ||
}[action] || action | ||
end | ||
|
||
def action_colour(action) | ||
{ | ||
"create" => "green", | ||
"update" => "blue", | ||
"destroy" => "red", | ||
}[action] | ||
end | ||
|
||
def changed_value(value) | ||
return "<empty>" if value.nil? | ||
return "<empty>" if value.blank? | ||
|
||
value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= govuk_table(classes: "audits-table") do |table| | ||
table.with_caption(size: 'l', text: 'Audits') | ||
table.with_body do |body| | ||
@audits.each do |audit| | ||
body.with_row do |row| | ||
row.with_cell(text: audit_message(audit), colspan: 6) | ||
end | ||
end | ||
end | ||
end %> | ||
|
||
<%= govuk_pagination(pagy: @pagy) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Audited.store_synthesized_enums = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class InstallAudited < ActiveRecord::Migration[7.0] | ||
def self.up | ||
create_table :audits, force: true do |t| # rubocop:disable Rails/CreateTableWithTimestamps | ||
t.column :auditable_id, :integer | ||
t.column :auditable_type, :string | ||
t.column :associated_id, :integer | ||
t.column :associated_type, :string | ||
t.column :user_id, :integer | ||
t.column :user_type, :string | ||
t.column :username, :string | ||
t.column :action, :string | ||
t.column :audited_changes, :jsonb | ||
t.column :version, :integer, default: 0 | ||
t.column :comment, :string | ||
t.column :remote_address, :string | ||
t.column :request_uuid, :string | ||
t.column :created_at, :datetime | ||
end | ||
|
||
add_index :audits, %i[auditable_type auditable_id version], name: "auditable_index" | ||
add_index :audits, %i[associated_type associated_id], name: "associated_index" | ||
add_index :audits, %i[user_id user_type], name: "user_index" | ||
add_index :audits, :request_uuid | ||
add_index :audits, :created_at | ||
end | ||
|
||
def self.down | ||
drop_table :audits | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.