2018-07-18 16:03:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-13 09:32:21 +00:00
|
|
|
module Users
|
|
|
|
class UpdateService < BaseService
|
2017-08-25 23:15:43 +00:00
|
|
|
include NewUserNotifier
|
2019-06-19 20:27:34 +00:00
|
|
|
attr_reader :user, :identity_params
|
2017-08-25 23:15:43 +00:00
|
|
|
|
2017-09-27 09:48:33 +00:00
|
|
|
def initialize(current_user, params = {})
|
2017-09-20 10:00:33 +00:00
|
|
|
@current_user = current_user
|
2017-09-27 10:08:10 +00:00
|
|
|
@user = params.delete(:user)
|
2018-07-13 15:52:31 +00:00
|
|
|
@status_params = params.delete(:status)
|
2019-06-19 20:27:34 +00:00
|
|
|
@identity_params = params.slice(*identity_attributes)
|
2017-06-13 09:32:21 +00:00
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
def execute(validate: true, &block)
|
2017-06-23 15:11:31 +00:00
|
|
|
yield(@user) if block_given?
|
|
|
|
|
2017-09-20 10:31:35 +00:00
|
|
|
user_exists = @user.persisted?
|
2020-01-13 06:08:10 +00:00
|
|
|
|
|
|
|
discard_read_only_attributes
|
2018-12-14 22:58:57 +00:00
|
|
|
assign_attributes
|
2019-06-19 20:27:34 +00:00
|
|
|
assign_identity
|
2020-03-24 09:09:25 +00:00
|
|
|
build_canonical_email
|
2017-06-13 09:32:21 +00:00
|
|
|
|
2018-07-13 15:52:31 +00:00
|
|
|
if @user.save(validate: validate) && update_status
|
2017-09-20 10:31:35 +00:00
|
|
|
notify_success(user_exists)
|
2017-06-13 09:32:21 +00:00
|
|
|
else
|
2018-07-13 15:52:31 +00:00
|
|
|
messages = @user.errors.full_messages + Array(@user.status&.errors&.full_messages)
|
|
|
|
error(messages.uniq.join('. '))
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 10:10:28 +00:00
|
|
|
def execute!(*args, &block)
|
2017-06-22 09:27:37 +00:00
|
|
|
result = execute(*args, &block)
|
2017-06-14 09:35:58 +00:00
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
raise ActiveRecord::RecordInvalid.new(@user) unless result[:status] == :success
|
2017-06-22 10:10:28 +00:00
|
|
|
|
|
|
|
true
|
2017-06-14 09:35:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-24 09:09:25 +00:00
|
|
|
def build_canonical_email
|
|
|
|
return unless @user.email_changed?
|
|
|
|
|
|
|
|
Users::UpdateCanonicalEmailService.new(user: @user).execute
|
|
|
|
end
|
|
|
|
|
2018-07-13 15:52:31 +00:00
|
|
|
def update_status
|
|
|
|
return true unless @status_params
|
|
|
|
|
|
|
|
Users::SetStatusService.new(current_user, @status_params.merge(user: @user)).execute
|
|
|
|
end
|
|
|
|
|
2017-09-20 10:31:35 +00:00
|
|
|
def notify_success(user_exists)
|
|
|
|
notify_new_user(@user, nil) unless user_exists
|
2017-09-20 09:00:06 +00:00
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2020-01-13 06:08:10 +00:00
|
|
|
def discard_read_only_attributes
|
2020-04-02 00:08:11 +00:00
|
|
|
discard_synced_attributes
|
2020-01-13 06:08:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def discard_synced_attributes
|
2018-12-14 22:58:57 +00:00
|
|
|
if (metadata = @user.user_synced_attributes_metadata)
|
|
|
|
read_only = metadata.read_only_attributes
|
|
|
|
|
|
|
|
params.reject! { |key, _| read_only.include?(key.to_sym) }
|
2017-08-29 08:57:41 +00:00
|
|
|
end
|
2020-01-13 06:08:10 +00:00
|
|
|
end
|
2017-08-29 08:57:41 +00:00
|
|
|
|
2020-01-13 06:08:10 +00:00
|
|
|
def assign_attributes
|
2019-09-18 14:02:45 +00:00
|
|
|
@user.assign_attributes(params.except(*identity_attributes)) unless params.empty?
|
2019-06-19 20:27:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assign_identity
|
|
|
|
return unless identity_params.present?
|
|
|
|
|
2019-06-26 20:11:30 +00:00
|
|
|
identity = user.identities.find_or_create_by(provider_params) # rubocop: disable CodeReuse/ActiveRecord
|
2019-06-19 20:27:34 +00:00
|
|
|
identity.update(identity_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def identity_attributes
|
|
|
|
[:provider, :extern_uid]
|
2017-06-14 09:35:58 +00:00
|
|
|
end
|
2019-06-26 20:11:30 +00:00
|
|
|
|
|
|
|
def provider_attributes
|
|
|
|
[:provider]
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider_params
|
|
|
|
identity_params.slice(*provider_attributes)
|
|
|
|
end
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Users::UpdateService.prepend_if_ee('EE::Users::UpdateService')
|