gitlab-org--gitlab-foss/app/services/users/update_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

117 lines
3 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
2017-06-13 05:32:21 -04:00
module Users
class UpdateService < BaseService
include NewUserNotifier
attr_reader :user, :identity_params
ATTRS_REQUIRING_PASSWORD_CHECK = %w[email].freeze
2017-09-27 05:48:33 -04:00
def initialize(current_user, params = {})
2017-09-20 06:00:33 -04:00
@current_user = current_user
@validation_password = params.delete(:validation_password)
2017-09-27 06:08:10 -04:00
@user = params.delete(:user)
@status_params = params.delete(:status)
@identity_params = params.slice(*identity_attributes)
2017-06-13 05:32:21 -04:00
@params = params.dup
end
def execute(validate: true, check_password: false, &block)
yield(@user) if block
2017-06-23 11:11:31 -04:00
2017-09-20 06:31:35 -04:00
user_exists = @user.persisted?
@user.user_detail # prevent assignment
discard_read_only_attributes
assign_attributes
if check_password && require_password_check? && !@user.valid_password?(@validation_password)
return error(s_("Profiles|Invalid password"))
end
assign_identity
build_canonical_email
2017-06-13 05:32:21 -04:00
if @user.save(validate: validate) && update_status
2017-09-20 06:31:35 -04:00
notify_success(user_exists)
2017-06-13 05:32:21 -04:00
else
messages = @user.errors.full_messages + Array(@user.status&.errors&.full_messages)
error(messages.uniq.join('. '))
2017-06-13 05:32:21 -04:00
end
end
def execute!(*args, **kargs, &block)
result = execute(*args, **kargs, &block)
raise ActiveRecord::RecordInvalid, @user unless result[:status] == :success
2017-06-22 06:10:28 -04:00
true
end
private
def require_password_check?
return false unless @user.persisted?
return false if @user.password_automatically_set?
changes = @user.changed
ATTRS_REQUIRING_PASSWORD_CHECK.any? { |param| changes.include?(param) }
end
def build_canonical_email
return unless @user.email_changed?
Users::UpdateCanonicalEmailService.new(user: @user).execute
end
def update_status
return true unless @status_params
Users::SetStatusService.new(current_user, @status_params.merge(user: @user)).execute
end
2017-09-20 06:31:35 -04:00
def notify_success(user_exists)
notify_new_user(@user, nil) unless user_exists
success
end
def discard_read_only_attributes
discard_synced_attributes
end
def discard_synced_attributes
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 04:57:41 -04:00
end
end
2017-08-29 04:57:41 -04:00
def assign_attributes
@user.assign_attributes(params.except(*identity_attributes)) unless params.empty?
end
def assign_identity
return unless identity_params.present?
identity = user.identities.find_or_create_by(provider_params) # rubocop: disable CodeReuse/ActiveRecord
identity.update(identity_params)
end
def identity_attributes
[:provider, :extern_uid]
end
def provider_attributes
[:provider]
end
def provider_params
identity_params.slice(*provider_attributes)
end
2017-06-13 05:32:21 -04:00
end
end
Users::UpdateService.prepend_mod_with('Users::UpdateService')