2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
class KeyRestrictionValidator < ActiveModel::EachValidator
|
|
|
|
FORBIDDEN = -1
|
|
|
|
|
|
|
|
def self.supported_sizes(type)
|
|
|
|
Gitlab::SSHPublicKey.supported_sizes(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_key_restrictions(type)
|
|
|
|
[0, *supported_sizes(type), FORBIDDEN]
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless valid_restriction?(value)
|
|
|
|
record.errors.add(attribute, "must be forbidden, allowed, or one of these sizes: #{supported_sizes_message}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def supported_sizes_message
|
|
|
|
sizes = self.class.supported_sizes(options[:type])
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
Gitlab::Utils.to_exclusive_sentence(sizes)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid_restriction?(value)
|
|
|
|
choices = self.class.supported_key_restrictions(options[:type])
|
|
|
|
choices.include?(value)
|
|
|
|
end
|
|
|
|
end
|