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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
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
2022-03-02 08:16:31 +05:30
include AfterCommitQueue
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
2023-03-04 22:38:38 +05:30
scope :by_entity_type, ->(entity_type) { where(entity_type: entity_type) }
scope :by_entity_id, ->(entity_id) { where(entity_id: entity_id) }
scope :by_author_id, ->(author_id) { where(author_id: author_id) }
scope :by_entity_username, ->(username) { where(entity_id: find_user_id(username)) }
scope :by_author_username, ->(username) { where(author_id: find_user_id(username)) }
2019-12-21 20:55:43 +05:30
2015-09-11 14:41:01 +05:30
after_initialize :initialize_details
2021-07-02 01:05:55 +05:30
before_validation :sanitize_message
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
2023-04-23 21:23:45 +05:30
return unless has_attribute?(:details)
2021-01-03 14:25:43 +05:30
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
2023-04-23 21:23:45 +05:30
details
.merge(details.slice(:from, :to).transform_values(&:to_s))
.merge(author_email: author.try(:email))
2019-12-21 20:55:43 +05:30
end
2020-04-22 19:07:51 +05:30
2021-03-11 19:13:27 +05:30
def author
2022-04-04 11:22:00 +05:30
lazy_author&.itself.presence || default_author_value
2021-03-11 19:13:27 +05:30
end
2020-04-22 19:07:51 +05:30
def lazy_author
2021-11-18 22:05:49 +05:30
BatchLoader.for(author_id).batch do |author_ids, loader|
2023-04-23 21:23:45 +05:30
User.select(:id, :name, :username, :email).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|
2023-04-23 21:23:45 +05:30
json['ip_address'] = ip_address.to_s
2020-10-24 23:57:45 +05:30
end
end
2022-08-27 11:52:29 +05:30
def target_type
super || details[:target_type]
end
def target_id
details[:target_id]
end
def target_details
super || details[:target_details]
end
2020-04-22 19:07:51 +05:30
private
2021-07-02 01:05:55 +05:30
def sanitize_message
message = details[:custom_message]
return unless message
self.details = details.merge(custom_message: Sanitize.clean(message))
end
2020-04-22 19:07:51 +05:30
def default_author_value
2022-04-04 11:22:00 +05:30
::Gitlab::Audit::NullAuthor.for(author_id, self)
2020-07-28 23:09:34 +05:30
end
def parallel_persist
2021-09-30 23:02:18 +05:30
PARALLEL_PERSISTENCE_COLUMNS.each do |name|
2023-04-23 21:23:45 +05:30
original = self[name] || details[name]
2021-09-30 23:02:18 +05:30
next unless original
2023-04-23 21:23:45 +05:30
self[name] = details[name] = original
2021-09-30 23:02:18 +05:30
end
2020-04-22 19:07:51 +05:30
end
2021-12-11 22:18:48 +05:30
def self.find_user_id(username)
User.find_by_username(username)&.id
end
2015-09-11 14:41:01 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
AuditEvent.prepend_mod_with('AuditEvent')