diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-12-07 16:17:12 -0500 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-12-07 16:57:26 -0500 |
commit | 9321d382bd5a0697e0e15a5065ec274e75541851 (patch) | |
tree | 98c24ffe03b2bbd5212935ec7680d1bb1f122ec1 /app | |
parent | ad6a771dc680b52e4b46c73f20bc39340d08bf32 (diff) | |
download | gitlab-ce-9321d382bd5a0697e0e15a5065ec274e75541851.tar.gz |
Add custom NamespaceValidator
Diffstat (limited to 'app')
-rw-r--r-- | app/models/namespace.rb | 8 | ||||
-rw-r--r-- | app/models/user.rb | 6 | ||||
-rw-r--r-- | app/validators/namespace_validator.rb | 22 |
3 files changed, 27 insertions, 9 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 20b92e68d61..e07c676a9f3 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -30,12 +30,10 @@ class Namespace < ActiveRecord::Base validates :description, length: { within: 0..255 } validates :path, - uniqueness: { case_sensitive: false }, - presence: true, length: { within: 1..255 }, - exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_regex, - message: Gitlab::Regex.namespace_regex_message } + namespace: true, + presence: true, + uniqueness: { case_sensitive: false } delegate :name, to: :owner, allow_nil: true, prefix: true diff --git a/app/models/user.rb b/app/models/user.rb index 719b49b16fe..cfed797e725 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -148,11 +148,9 @@ class User < ActiveRecord::Base validates :bio, length: { maximum: 255 }, allow_blank: true validates :projects_limit, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :username, + namespace: true, presence: true, - uniqueness: { case_sensitive: false }, - exclusion: { in: Gitlab::Blacklist.path }, - format: { with: Gitlab::Regex.namespace_regex, - message: Gitlab::Regex.namespace_regex_message } + uniqueness: { case_sensitive: false } validates :notification_level, inclusion: { in: Notification.notification_levels }, presence: true validate :namespace_uniq, if: ->(user) { user.username_changed? } diff --git a/app/validators/namespace_validator.rb b/app/validators/namespace_validator.rb new file mode 100644 index 00000000000..4ab1706abda --- /dev/null +++ b/app/validators/namespace_validator.rb @@ -0,0 +1,22 @@ +# NamespaceValidator +# +# Custom validator for GitLab namespace values. +# +# Values are checked for formatting and exclusion from `Gitlab::Blacklist.path`. +class NamespaceValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless value =~ Gitlab::Regex.namespace_regex + record.errors.add(attribute, Gitlab::Regex.namespace_regex_message) + end + + if blacklisted?(value) + record.errors.add(attribute, "#{value} is a reserved name") + end + end + + private + + def blacklisted?(value) + Gitlab::Blacklist.path.include?(value) + end +end |