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

41 lines
1,010 B
Ruby
Raw Normal View History

2017-06-13 05:32:21 -04:00
module Users
# Service for creating a new user.
class UpdateService < BaseService
def initialize(current_user, user, params = {})
@current_user = current_user
@user = user
@params = params.dup
end
def execute(skip_authorization: false, &block)
assign_attributes(skip_authorization, &block)
2017-06-13 05:32:21 -04:00
if @user.save || !@user.changed?
2017-06-13 05:32:21 -04:00
success
else
error("User could not be updated #{@user.errors.full_messages.uniq.join('. ')}" )
2017-06-13 05:32:21 -04:00
end
end
def execute!(skip_authorization: false, &block)
assign_attributes(skip_authorization, &block)
@user.save! if @user.changed?
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