Commit 82f5cd46 authored by James Lopez's avatar James Lopez

add event changes to users update service

parent dff7db5a
......@@ -16,6 +16,21 @@ class AuditEventService
self
end
def for_changes
@details =
{
change: @details[:as] || @details[:column],
from: @details[:from],
to: @details[:to],
author_name: @author.name,
target_id: @entity.id,
target_type: @entity.class,
target_details: @entity.name
}
self
end
def security_event
SecurityEvent.create(
author_id: @author.id,
......
module Users
class UpdateService < BaseService
include EE::Audit::Changes
include NewUserNotifier
def initialize(current_user, user, params = {})
......@@ -16,6 +17,9 @@ module Users
user_exists = @user.persisted?
if @user.save(validate: validate)
audit_changes :email, as: 'email address', column: :notification_email
audit_changes :encrypted_password, as: 'password', skip_changes: true
notify_new_user(@user, nil) unless user_exists
success
......
module EE
module Audit
module Changes
def audit_changes(current_user, column, options = {})
def audit_changes(column, options = {})
column = options[:column] || column
return unless changed?(column)
audit_event(current_user, parse_options(column, options))
audit_event(parse_options(column, options))
end
protected
......@@ -35,8 +37,8 @@ module EE
end
end
def audit_event(current_user, options)
AuditEventService.new(current_user, model, options).
def audit_event(options)
AuditEventService.new(@current_user, model, options).
for_changes.security_event
end
end
......
......@@ -3,10 +3,11 @@ require 'spec_helper'
describe EE::Audit::Changes do
describe '.audit_changes' do
let(:user) { create(:user) }
let(:foo_class) { Class.new { include EE::Audit::Changes } }
let(:foo_instance) { Class.new { include EE::Audit::Changes }.new }
before do
allow_any_instance_of(foo_class).to receive(:model).and_return(user)
foo_instance.instance_variable_set(:@current_user, user)
allow(foo_instance).to receive(:model).and_return(user)
end
describe 'non audit changes' do
......@@ -14,7 +15,7 @@ describe EE::Audit::Changes do
expect_any_instance_of(AuditEventService).not_to receive(:security_event)
user.update!(name: 'new name')
foo_class.new.audit_changes(user, :email)
foo_instance.audit_changes(:email)
end
end
......@@ -23,7 +24,7 @@ describe EE::Audit::Changes do
expect_any_instance_of(AuditEventService).to receive(:security_event)
user.update!(name: 'new name')
foo_class.new.audit_changes(user, :name)
foo_instance.audit_changes(:name)
end
end
end
......
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