2017-06-13 05:32:21 -04:00
|
|
|
module Users
|
2017-06-16 05:12:06 -04:00
|
|
|
# Service for updating a user.
|
2017-06-13 05:32:21 -04:00
|
|
|
class UpdateService < BaseService
|
|
|
|
def initialize(current_user, user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@user = user
|
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2017-06-16 05:12:06 -04:00
|
|
|
def execute(skip_authorization: false, validate: true, &block)
|
2017-06-14 05:35:58 -04:00
|
|
|
assign_attributes(skip_authorization, &block)
|
2017-06-13 05:32:21 -04:00
|
|
|
|
2017-06-22 05:27:37 -04:00
|
|
|
if @user.save(validate: validate) || @user.errors.empty?
|
2017-06-13 05:32:21 -04:00
|
|
|
success
|
|
|
|
else
|
2017-06-14 09:32:25 -04:00
|
|
|
error(@user.errors.full_messages.uniq.join('. '))
|
2017-06-13 05:32:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 06:10:28 -04:00
|
|
|
def execute!(*args, &block)
|
2017-06-22 05:27:37 -04:00
|
|
|
result = execute(*args, &block)
|
2017-06-14 05:35:58 -04:00
|
|
|
|
2017-06-22 06:10:28 -04:00
|
|
|
raise ActiveRecord::RecordInvalid(result[:message]) unless result[:status] == :success
|
|
|
|
|
|
|
|
true
|
2017-06-14 05:35:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assign_attributes(skip_authorization, &block)
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_update_user?
|
|
|
|
|
|
|
|
yield(@user) if block_given?
|
|
|
|
|
|
|
|
@user.assign_attributes(params) if params.any?
|
|
|
|
end
|
|
|
|
|
2017-06-13 05:32:21 -04:00
|
|
|
def can_update_user?
|
|
|
|
current_user == @user || current_user&.admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|