Commit ade602fd authored by Simon Knox's avatar Simon Knox

Merge branch '9-5-stable' into security-9-5

parents a587bf29 7e7d2524
module Users
module NewUserNotifier
def notify_new_user(user, reset_token)
log_info("User \"#{user.name}\" (#{user.email}) was created")
notification_service.new_user(user, reset_token) if reset_token
system_hook_service.execute_hooks_for(user, :create)
end
end
end
module Users module Users
class CreateService < BaseService class CreateService < BaseService
include NewUserNotifier
def initialize(current_user, params = {}) def initialize(current_user, params = {})
@current_user = current_user @current_user = current_user
@params = params.dup @params = params.dup
...@@ -10,11 +12,7 @@ module Users ...@@ -10,11 +12,7 @@ module Users
@reset_token = user.generate_reset_token if user.recently_sent_password_reset? @reset_token = user.generate_reset_token if user.recently_sent_password_reset?
if user.save notify_new_user(user, @reset_token) if user.save
log_info("User \"#{user.name}\" (#{user.email}) was created")
notification_service.new_user(user, @reset_token) if @reset_token
system_hook_service.execute_hooks_for(user, :create)
end
user user
end end
......
module Users module Users
class UpdateService < BaseService class UpdateService < BaseService
include NewUserNotifier
def initialize(user, params = {}) def initialize(user, params = {})
@user = user @user = user
@params = params.dup @params = params.dup
...@@ -10,7 +12,11 @@ module Users ...@@ -10,7 +12,11 @@ module Users
assign_attributes(&block) assign_attributes(&block)
user_exists = @user.persisted?
if @user.save(validate: validate) if @user.save(validate: validate)
notify_new_user(@user, nil) unless user_exists
success success
else else
error(@user.errors.full_messages.uniq.join('. ')) error(@user.errors.full_messages.uniq.join('. '))
......
---
title: Fire system hooks when a user is created via LDAP
merge_request:
author:
type: fixed
...@@ -24,7 +24,10 @@ describe Users::UpdateService do ...@@ -24,7 +24,10 @@ describe Users::UpdateService do
describe '#execute!' do describe '#execute!' do
it 'updates the name' do it 'updates the name' do
result = update_user(user, name: 'New Name') service = described_class.new(user, name: 'New Name')
expect(service).not_to receive(:notify_new_user)
result = service.execute!
expect(result).to be true expect(result).to be true
expect(user.name).to eq('New Name') expect(user.name).to eq('New Name')
...@@ -36,6 +39,18 @@ describe Users::UpdateService do ...@@ -36,6 +39,18 @@ describe Users::UpdateService do
end.to raise_error(ActiveRecord::RecordInvalid) end.to raise_error(ActiveRecord::RecordInvalid)
end end
it 'fires system hooks when a new user is saved' do
system_hook_service = spy(:system_hook_service)
user = build(:user)
service = described_class.new(user, name: 'John Doe')
expect(service).to receive(:notify_new_user).and_call_original
expect(service).to receive(:system_hook_service).and_return(system_hook_service)
service.execute
expect(system_hook_service).to have_received(:execute_hooks_for).with(user, :create)
end
def update_user(user, opts) def update_user(user, opts)
described_class.new(user, opts).execute! described_class.new(user, opts).execute!
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