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

43 lines
1 KiB
Ruby
Raw Normal View History

2017-06-13 05:32:21 -04:00
module Users
# 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
def execute(skip_authorization: false, validate: true, &block)
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
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-22 06:10:28 -04:00
raise ActiveRecord::RecordInvalid(result[:message]) unless result[:status] == :success
true
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