debian-mirror-gitlab/app/services/users/update_service.rb

125 lines
3.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Users
class UpdateService < BaseService
include NewUserNotifier
2019-09-30 21:07:59 +05:30
attr_reader :user, :identity_params
2017-09-10 17:25:29 +05:30
2021-11-18 22:05:49 +05:30
ATTRS_REQUIRING_PASSWORD_CHECK = %w[email].freeze
2018-03-17 18:26:18 +05:30
def initialize(current_user, params = {})
@current_user = current_user
2021-11-18 22:05:49 +05:30
@validation_password = params.delete(:validation_password)
2018-03-17 18:26:18 +05:30
@user = params.delete(:user)
2018-11-18 11:00:15 +05:30
@status_params = params.delete(:status)
2019-09-30 21:07:59 +05:30
@identity_params = params.slice(*identity_attributes)
2017-09-10 17:25:29 +05:30
@params = params.dup
end
2021-11-18 22:05:49 +05:30
def execute(validate: true, check_password: false, &block)
2022-08-27 11:52:29 +05:30
yield(@user) if block
2017-09-10 17:25:29 +05:30
user_exists = @user.persisted?
2021-09-04 01:27:46 +05:30
@user.user_detail # prevent assignment
2020-03-13 15:44:24 +05:30
discard_read_only_attributes
2019-02-15 15:39:39 +05:30
assign_attributes
2021-11-18 22:05:49 +05:30
if check_password && require_password_check? && !@user.valid_password?(@validation_password)
return error(s_("Profiles|Invalid password"))
end
2019-09-30 21:07:59 +05:30
assign_identity
2020-04-22 19:07:51 +05:30
build_canonical_email
2023-01-10 11:22:00 +05:30
reset_unconfirmed_email
2017-09-10 17:25:29 +05:30
2018-11-18 11:00:15 +05:30
if @user.save(validate: validate) && update_status
2018-03-17 18:26:18 +05:30
notify_success(user_exists)
2017-09-10 17:25:29 +05:30
else
2018-11-18 11:00:15 +05:30
messages = @user.errors.full_messages + Array(@user.status&.errors&.full_messages)
error(messages.uniq.join('. '))
2017-09-10 17:25:29 +05:30
end
end
2021-11-18 22:05:49 +05:30
def execute!(*args, **kargs, &block)
result = execute(*args, **kargs, &block)
2017-09-10 17:25:29 +05:30
2021-06-08 01:23:25 +05:30
raise ActiveRecord::RecordInvalid, @user unless result[:status] == :success
2017-09-10 17:25:29 +05:30
true
end
private
2021-11-18 22:05:49 +05:30
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
2020-04-22 19:07:51 +05:30
def build_canonical_email
return unless @user.email_changed?
Users::UpdateCanonicalEmailService.new(user: @user).execute
end
2023-01-10 11:22:00 +05:30
def reset_unconfirmed_email
return unless @user.persisted?
return unless @user.email_changed?
@user.update_column(:unconfirmed_email, nil)
end
2018-11-18 11:00:15 +05:30
def update_status
return true unless @status_params
Users::SetStatusService.new(current_user, @status_params.merge(user: @user)).execute
end
2018-03-17 18:26:18 +05:30
def notify_success(user_exists)
notify_new_user(@user, nil) unless user_exists
success
end
2020-03-13 15:44:24 +05:30
def discard_read_only_attributes
2020-04-22 19:07:51 +05:30
discard_synced_attributes
2020-03-13 15:44:24 +05:30
end
def discard_synced_attributes
2019-02-15 15:39:39 +05:30
if (metadata = @user.user_synced_attributes_metadata)
read_only = metadata.read_only_attributes
params.reject! { |key, _| read_only.include?(key.to_sym) }
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
end
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
def assign_attributes
2019-12-04 20:38:33 +05:30
@user.assign_attributes(params.except(*identity_attributes)) unless params.empty?
2019-09-30 21:07:59 +05:30
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)
2017-09-10 17:25:29 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Users::UpdateService.prepend_mod_with('Users::UpdateService')