debian-mirror-gitlab/app/models/audit_event.rb

96 lines
2.3 KiB
Ruby
Raw Normal View History

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-07-28 23:09:34 +05:30
include BulkInsertSafe
2021-01-03 14:25:43 +05:30
include EachBatch
2021-03-08 18:12:59 +05:30
include PartitionedTable
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
PARALLEL_PERSISTENCE_COLUMNS = [
:author_name,
:entity_path,
:target_details,
:target_type,
:target_id
].freeze
2020-07-28 23:09:34 +05:30
2021-02-22 17:27:13 +05:30
self.primary_key = :id
2021-03-08 18:12:59 +05:30
partitioned_by :created_at, strategy: :monthly
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
2021-01-03 14:25:43 +05:30
validates :ip_address, ip_address: true
2015-09-11 14:41:01 +05:30
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-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
2021-01-03 14:25:43 +05:30
return unless self.has_attribute?(:details)
self.details = {} if details&.nil?
2015-09-11 14:41:01 +05:30
end
def author_name
2021-03-11 19:13:27 +05:30
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
2021-03-11 19:13:27 +05:30
def author
lazy_author&.itself.presence ||
::Gitlab::Audit::NullAuthor.for(author_id, (self[:author_name] || details[:author_name]))
end
2020-04-22 19:07:51 +05:30
def lazy_author
2021-03-11 19:13:27 +05:30
BatchLoader.for(author_id).batch(replace_methods: false) do |author_ids, loader|
2021-01-03 14:25:43 +05:30
User.select(:id, :name, :username).where(id: author_ids).find_each do |user|
2020-04-22 19:07:51 +05:30
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')