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