debian-mirror-gitlab/app/services/audit_event_service.rb

50 lines
980 B
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
class AuditEventService
def initialize(author, entity, details = {})
@author, @entity, @details = author, entity, details
end
def for_authentication
@details = {
with: @details[:with],
target_id: @author.id,
2016-08-24 12:49:21 +05:30
target_type: 'User',
2017-09-10 17:25:29 +05:30
target_details: @author.name
2015-09-11 14:41:01 +05:30
}
self
end
def security_event
2018-12-13 13:39:08 +05:30
log_security_event_to_file
log_security_event_to_database
end
private
def base_payload
{
2015-09-11 14:41:01 +05:30
author_id: @author.id,
entity_id: @entity.id,
2018-12-13 13:39:08 +05:30
entity_type: @entity.class.name
}
end
def file_logger
@file_logger ||= Gitlab::AuditJsonLogger.build
end
2019-09-30 21:07:59 +05:30
def formatted_details
@details.merge(@details.slice(:from, :to).transform_values(&:to_s))
end
2018-12-13 13:39:08 +05:30
def log_security_event_to_file
2019-09-30 21:07:59 +05:30
file_logger.info(base_payload.merge(formatted_details))
2018-12-13 13:39:08 +05:30
end
def log_security_event_to_database
SecurityEvent.create(base_payload.merge(details: @details))
2015-09-11 14:41:01 +05:30
end
end