Commit 7d144520 authored by Tan Le's avatar Tan Le Committed by Dmytro Zaporozhets

Audit change model that is different than entity

This extends `for_changes` method to accept reference of target object
being audited. Previously the `entity` is used to record both scope and
target change object. While that is true in some context (i.e. the
target model happens to be a User/Project/Group), it does not work for
`ApprovalProjectRule` (i.e. the target model is `ApprovalProjectRule`
and the entity is the associated Project)
parent eb57028b
...@@ -99,17 +99,19 @@ module EE ...@@ -99,17 +99,19 @@ module EE
# Builds the @details attribute for changes # Builds the @details attribute for changes
# #
# @param model [Object] the target model being audited
#
# @return [AuditEventService] # @return [AuditEventService]
def for_changes def for_changes(model)
@details = @details =
{ {
change: @details[:as] || @details[:column], change: @details[:as] || @details[:column],
from: @details[:from], from: @details[:from],
to: @details[:to], to: @details[:to],
author_name: @author.name, author_name: @author.name,
target_id: @entity.id, target_id: model.id,
target_type: @entity.class.name, target_type: model.class.name,
target_details: @details[:target_details] || @entity.name target_details: @details[:target_details] || model.name
} }
self self
......
...@@ -17,7 +17,7 @@ module EE ...@@ -17,7 +17,7 @@ module EE
def audit_changes(column, options = {}) def audit_changes(column, options = {})
column = options[:column] || column column = options[:column] || column
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
@target_model = options[:target_model] @entity = options[:entity]
@model = options[:model] @model = options[:model]
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
...@@ -28,8 +28,8 @@ module EE ...@@ -28,8 +28,8 @@ module EE
protected protected
def target_model def entity
@target_model || model # rubocop:disable Gitlab/ModuleWithInstanceVariables @entity || model # rubocop:disable Gitlab/ModuleWithInstanceVariables
end end
def model def model
...@@ -67,8 +67,8 @@ module EE ...@@ -67,8 +67,8 @@ module EE
end end
def audit_event(options) def audit_event(options)
::AuditEventService.new(@current_user, target_model, options) # rubocop:disable Gitlab/ModuleWithInstanceVariables ::AuditEventService.new(@current_user, entity, options) # rubocop:disable Gitlab/ModuleWithInstanceVariables
.for_changes.security_event .for_changes(model).security_event
end end
end end
end end
......
...@@ -22,7 +22,7 @@ module EE ...@@ -22,7 +22,7 @@ module EE
def execute def execute
COLUMNS.each do |column| COLUMNS.each do |column|
audit_changes(column, as: column.to_s, target_model: @project, model: model) audit_changes(column, as: column.to_s, entity: @project, model: model)
end end
end end
......
...@@ -63,11 +63,11 @@ describe EE::Audit::Changes do ...@@ -63,11 +63,11 @@ describe EE::Audit::Changes do
end end
end end
context 'when target_model is provided' do context 'when entity is provided' do
let(:project) { Project.new } let(:project) { Project.new }
let(:options) { { model: user, target_model: project } } let(:options) { { model: user, entity: project } }
it 'instantiates audit event service with the given target_model' do it 'instantiates audit event service with the given entity' do
user.update!(name: 'Scrooge McDuck') user.update!(name: 'Scrooge McDuck')
audit! audit!
......
...@@ -269,6 +269,27 @@ describe AuditEventService do ...@@ -269,6 +269,27 @@ describe AuditEventService do
end end
end end
describe '#for_changes' do
let(:author_name) { 'Administrator' }
let(:current_user) { User.new(name: author_name) }
let(:changed_model) { ApprovalProjectRule.new(id: 6, name: 'Security') }
let(:options) { { as: 'required approvers', from: 3, to: 4 } }
subject(:service) { described_class.new(current_user, project, options).for_changes(changed_model) }
it 'sets the details attribute' do
expect(service.instance_variable_get(:@details)).to eq(
change: 'required approvers',
from: 3,
to: 4,
author_name: author_name,
target_id: 6,
target_type: 'ApprovalProjectRule',
target_details: 'Security'
)
end
end
describe 'license' do describe 'license' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment