2015-12-07 21:17:12 +00:00
|
|
|
# NamespaceValidator
|
|
|
|
#
|
|
|
|
# Custom validator for GitLab namespace values.
|
|
|
|
#
|
2015-12-07 21:29:39 +00:00
|
|
|
# Values are checked for formatting and exclusion from a list of reserved path
|
|
|
|
# names.
|
2015-12-07 21:17:12 +00:00
|
|
|
class NamespaceValidator < ActiveModel::EachValidator
|
2016-09-29 08:36:38 +00:00
|
|
|
RESERVED = %w[
|
|
|
|
.well-known
|
2015-12-07 21:29:39 +00:00
|
|
|
admin
|
|
|
|
all
|
|
|
|
assets
|
|
|
|
ci
|
|
|
|
dashboard
|
|
|
|
files
|
|
|
|
groups
|
|
|
|
help
|
|
|
|
hooks
|
|
|
|
issues
|
|
|
|
merge_requests
|
2016-01-08 09:19:22 +00:00
|
|
|
new
|
2015-12-07 21:29:39 +00:00
|
|
|
notes
|
|
|
|
profile
|
|
|
|
projects
|
|
|
|
public
|
|
|
|
repository
|
2016-10-11 01:58:26 +00:00
|
|
|
robots.txt
|
2015-12-07 21:29:39 +00:00
|
|
|
s
|
|
|
|
search
|
|
|
|
services
|
|
|
|
snippets
|
|
|
|
teams
|
|
|
|
u
|
|
|
|
unsubscribes
|
|
|
|
users
|
2016-09-29 08:36:38 +00:00
|
|
|
].freeze
|
2015-12-07 21:29:39 +00:00
|
|
|
|
2017-03-06 18:26:58 +00:00
|
|
|
WILDCARD_ROUTES = %w[tree commits wikis new edit create update logs_tree
|
|
|
|
preview blob blame raw files create_dir find_file].freeze
|
|
|
|
|
|
|
|
STRICT_RESERVED = (RESERVED + WILDCARD_ROUTES).freeze
|
|
|
|
|
2016-11-14 14:55:31 +00:00
|
|
|
def self.valid?(value)
|
|
|
|
!reserved?(value) && follow_format?(value)
|
|
|
|
end
|
|
|
|
|
2017-03-06 18:26:58 +00:00
|
|
|
def self.reserved?(value, strict: false)
|
|
|
|
if strict
|
|
|
|
STRICT_RESERVED.include?(value)
|
|
|
|
else
|
|
|
|
RESERVED.include?(value)
|
|
|
|
end
|
2016-11-14 14:55:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.follow_format?(value)
|
|
|
|
value =~ Gitlab::Regex.namespace_regex
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :reserved?, :follow_format?, to: :class
|
|
|
|
|
2015-12-07 21:17:12 +00:00
|
|
|
def validate_each(record, attribute, value)
|
2016-11-14 14:55:31 +00:00
|
|
|
unless follow_format?(value)
|
2015-12-07 21:17:12 +00:00
|
|
|
record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
|
|
|
|
end
|
|
|
|
|
2017-03-06 18:26:58 +00:00
|
|
|
strict = record.is_a?(Group) && record.parent_id
|
|
|
|
|
|
|
|
if reserved?(value, strict: strict)
|
2015-12-07 21:17:12 +00:00
|
|
|
record.errors.add(attribute, "#{value} is a reserved name")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|