diff options
author | Nick Thomas <nick@gitlab.com> | 2017-08-28 23:58:36 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2017-08-30 22:50:44 +0300 |
commit | eb05bdc6f589f6f0713df12582eb9f18fc4022b3 (patch) | |
tree | 2a975a959d0ad419362f7b04d0329581af2bc366 /app/validators/key_restriction_validator.rb | |
parent | b84ca08e351fc9238bef4e6b4bf74158d25d4f1d (diff) |
Move the key restriction validation to its own class
Diffstat (limited to 'app/validators/key_restriction_validator.rb')
-rw-r--r-- | app/validators/key_restriction_validator.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/app/validators/key_restriction_validator.rb b/app/validators/key_restriction_validator.rb new file mode 100644 index 00000000000..204be827941 --- /dev/null +++ b/app/validators/key_restriction_validator.rb @@ -0,0 +1,29 @@ +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]) + sizes.to_sentence(last_word_connector: ', or ', two_words_connector: ' or ') + end + + def valid_restriction?(value) + choices = self.class.supported_key_restrictions(options[:type]) + choices.include?(value) + end +end |