2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class AuditEvent < ApplicationRecord
|
2019-12-21 20:55:43 +05:30
|
|
|
include CreatedAtFilterable
|
2020-06-23 00:09:42 +05:30
|
|
|
include IgnorableColumns
|
2020-07-28 23:09:34 +05:30
|
|
|
include BulkInsertSafe
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
PARALLEL_PERSISTENCE_COLUMNS = [:author_name, :entity_path, :target_details, :target_type].freeze
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
ignore_column :type, remove_with: '13.6', remove_after: '2020-11-22'
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
serialize :details, Hash # rubocop:disable Cop/ActiveRecordSerialize
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
belongs_to :user, foreign_key: :author_id
|
|
|
|
|
|
|
|
validates :author_id, presence: true
|
|
|
|
validates :entity_id, presence: true
|
|
|
|
validates :entity_type, presence: true
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
scope :by_entity_type, -> (entity_type) { where(entity_type: entity_type) }
|
|
|
|
scope :by_entity_id, -> (entity_id) { where(entity_id: entity_id) }
|
2020-07-28 23:09:34 +05:30
|
|
|
scope :by_author_id, -> (author_id) { where(author_id: author_id) }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
after_initialize :initialize_details
|
2020-07-28 23:09:34 +05:30
|
|
|
# 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-09-11 14:41:01 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# Note: After loading records, do not attempt to type cast objects it finds.
|
|
|
|
# We are in the process of deprecating STI (i.e. SecurityEvent) out of AuditEvent.
|
|
|
|
#
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/216845
|
|
|
|
def self.inheritance_column
|
|
|
|
:_type_disabled
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def self.order_by(method)
|
|
|
|
case method.to_s
|
|
|
|
when 'created_asc'
|
|
|
|
order(id: :asc)
|
|
|
|
else
|
|
|
|
order(id: :desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def initialize_details
|
|
|
|
self.details = {} if details.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2020-04-22 19:07:51 +05:30
|
|
|
lazy_author.name
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
def formatted_details
|
|
|
|
details.merge(details.slice(:from, :to).transform_values(&:to_s))
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def lazy_author
|
|
|
|
BatchLoader.for(author_id).batch(default_value: default_author_value) do |author_ids, loader|
|
|
|
|
User.where(id: author_ids).find_each do |user|
|
|
|
|
loader.call(user.id, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
|
|
|
json['ip_address'] = self.ip_address.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def default_author_value
|
2020-07-28 23:09:34 +05:30
|
|
|
::Gitlab::Audit::NullAuthor.for(author_id, (self[:author_name] || details[:author_name]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def parallel_persist
|
|
|
|
PARALLEL_PERSISTENCE_COLUMNS.each { |col| self[col] = details[col] }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
AuditEvent.prepend_if_ee('EE::AuditEvent')
|