2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class AuditEvent < ApplicationRecord
|
2019-09-26 05:06:04 -04:00
|
|
|
include CreatedAtFilterable
|
2020-07-09 14:10:09 -04:00
|
|
|
include BulkInsertSafe
|
2020-10-19 11:08:58 -04:00
|
|
|
include EachBatch
|
2020-12-21 07:10:22 -05:00
|
|
|
include PartitionedTable
|
2020-06-11 08:08:54 -04:00
|
|
|
|
2020-09-23 11:10:14 -04:00
|
|
|
PARALLEL_PERSISTENCE_COLUMNS = [
|
|
|
|
:author_name,
|
|
|
|
:entity_path,
|
|
|
|
:target_details,
|
|
|
|
:target_type,
|
|
|
|
:target_id
|
|
|
|
].freeze
|
2020-07-16 14:09:35 -04:00
|
|
|
|
2020-12-02 07:09:46 -05:00
|
|
|
self.primary_key = :id
|
|
|
|
|
2020-12-21 07:10:22 -05:00
|
|
|
partitioned_by :created_at, strategy: :monthly
|
|
|
|
|
2017-07-03 10:01:41 -04:00
|
|
|
serialize :details, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
2015-07-03 07:54:50 -04:00
|
|
|
|
|
|
|
belongs_to :user, foreign_key: :author_id
|
|
|
|
|
|
|
|
validates :author_id, presence: true
|
|
|
|
validates :entity_id, presence: true
|
|
|
|
validates :entity_type, presence: true
|
2020-09-24 11:09:51 -04:00
|
|
|
validates :ip_address, ip_address: true
|
2015-07-03 07:54:50 -04:00
|
|
|
|
2019-09-26 05:06:04 -04:00
|
|
|
scope :by_entity_type, -> (entity_type) { where(entity_type: entity_type) }
|
|
|
|
scope :by_entity_id, -> (entity_id) { where(entity_id: entity_id) }
|
2020-06-22 11:09:27 -04:00
|
|
|
scope :by_author_id, -> (author_id) { where(author_id: author_id) }
|
2019-09-26 05:06:04 -04:00
|
|
|
|
2015-07-03 07:54:50 -04:00
|
|
|
after_initialize :initialize_details
|
2021-07-01 17:08:38 -04:00
|
|
|
|
|
|
|
before_validation :sanitize_message
|
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
# Note: The intention is to remove this once refactoring of AuditEvent
|
|
|
|
# has proceeded further.
|
|
|
|
#
|
|
|
|
# See further details in the epic:
|
|
|
|
# https://gitlab.com/groups/gitlab-org/-/epics/2765
|
|
|
|
after_validation :parallel_persist
|
2015-07-03 07:54:50 -04:00
|
|
|
|
2020-03-18 14:09:35 -04:00
|
|
|
def self.order_by(method)
|
|
|
|
case method.to_s
|
|
|
|
when 'created_asc'
|
|
|
|
order(id: :asc)
|
|
|
|
else
|
|
|
|
order(id: :desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-03 07:54:50 -04:00
|
|
|
def initialize_details
|
2020-10-19 11:08:58 -04:00
|
|
|
return unless self.has_attribute?(:details)
|
|
|
|
|
|
|
|
self.details = {} if details&.nil?
|
2015-07-03 07:54:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2021-01-25 13:09:03 -05:00
|
|
|
author&.name
|
2015-07-03 07:54:50 -04:00
|
|
|
end
|
2019-09-26 05:06:04 -04:00
|
|
|
|
|
|
|
def formatted_details
|
|
|
|
details.merge(details.slice(:from, :to).transform_values(&:to_s))
|
|
|
|
end
|
2020-04-03 08:09:52 -04:00
|
|
|
|
2021-01-25 13:09:03 -05:00
|
|
|
def author
|
|
|
|
lazy_author&.itself.presence ||
|
|
|
|
::Gitlab::Audit::NullAuthor.for(author_id, (self[:author_name] || details[:author_name]))
|
|
|
|
end
|
|
|
|
|
2020-04-03 08:09:52 -04:00
|
|
|
def lazy_author
|
2021-01-25 13:09:03 -05:00
|
|
|
BatchLoader.for(author_id).batch(replace_methods: false) do |author_ids, loader|
|
2020-09-25 14:09:46 -04:00
|
|
|
User.select(:id, :name, :username).where(id: author_ids).find_each do |user|
|
2020-04-03 08:09:52 -04:00
|
|
|
loader.call(user.id, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-30 14:09:39 -04:00
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
|
|
|
json['ip_address'] = self.ip_address.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-03 08:09:52 -04:00
|
|
|
private
|
|
|
|
|
2021-07-01 17:08:38 -04:00
|
|
|
def sanitize_message
|
|
|
|
message = details[:custom_message]
|
|
|
|
|
|
|
|
return unless message
|
|
|
|
|
|
|
|
self.details = details.merge(custom_message: Sanitize.clean(message))
|
|
|
|
end
|
|
|
|
|
2020-04-03 08:09:52 -04:00
|
|
|
def default_author_value
|
2020-07-07 08:09:16 -04:00
|
|
|
::Gitlab::Audit::NullAuthor.for(author_id, (self[:author_name] || details[:author_name]))
|
2020-04-03 08:09:52 -04:00
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
|
|
|
|
def parallel_persist
|
2021-06-22 11:06:55 -04:00
|
|
|
PARALLEL_PERSISTENCE_COLUMNS.each do |name|
|
|
|
|
original = self[name] || self.details[name]
|
|
|
|
next unless original
|
|
|
|
|
|
|
|
self[name] = self.details[name] = original
|
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
end
|
2015-07-03 07:54:50 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
AuditEvent.prepend_mod_with('AuditEvent')
|