2017-04-12 09:28:23 +00:00
|
|
|
# DynamicPathValidator
|
2015-12-07 21:17:12 +00:00
|
|
|
#
|
2017-04-12 09:28:23 +00:00
|
|
|
# Custom validator for GitLab path values.
|
|
|
|
# These paths are assigned to `Namespace` (& `Group` as a subclass) & `Project`
|
2015-12-07 21:17:12 +00:00
|
|
|
#
|
2017-05-24 20:59:26 +00:00
|
|
|
# Values are checked for formatting and exclusion from a list of illegal path
|
2015-12-07 21:29:39 +00:00
|
|
|
# names.
|
2017-04-12 09:28:23 +00:00
|
|
|
class DynamicPathValidator < ActiveModel::EachValidator
|
2017-06-01 21:21:14 +00:00
|
|
|
extend Gitlab::EncodingHelper
|
2017-05-30 15:05:52 +00:00
|
|
|
|
2017-05-20 00:46:40 +00:00
|
|
|
class << self
|
2017-05-24 20:59:26 +00:00
|
|
|
def valid_user_path?(path)
|
2017-05-30 15:05:52 +00:00
|
|
|
encode!(path)
|
2017-05-24 20:59:26 +00:00
|
|
|
"#{path}/" =~ Gitlab::PathRegex.root_namespace_path_regex
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_group_path?(path)
|
2017-05-30 15:05:52 +00:00
|
|
|
encode!(path)
|
2017-05-24 20:59:26 +00:00
|
|
|
"#{path}/" =~ Gitlab::PathRegex.full_namespace_path_regex
|
2017-05-20 00:46:40 +00:00
|
|
|
end
|
2017-04-28 16:09:01 +00:00
|
|
|
|
2017-05-20 00:46:40 +00:00
|
|
|
def valid_project_path?(path)
|
2017-05-30 15:05:52 +00:00
|
|
|
encode!(path)
|
2017-05-24 20:59:26 +00:00
|
|
|
"#{path}/" =~ Gitlab::PathRegex.full_project_path_regex
|
2017-05-20 00:46:40 +00:00
|
|
|
end
|
2017-04-18 14:27:11 +00:00
|
|
|
end
|
|
|
|
|
2017-05-20 00:46:40 +00:00
|
|
|
def path_valid_for_record?(record, value)
|
2017-04-28 16:09:01 +00:00
|
|
|
full_path = record.respond_to?(:full_path) ? record.full_path : value
|
|
|
|
|
2017-05-20 00:46:40 +00:00
|
|
|
return true unless full_path
|
|
|
|
|
|
|
|
case record
|
|
|
|
when Project
|
|
|
|
self.class.valid_project_path?(full_path)
|
2017-05-24 20:59:26 +00:00
|
|
|
when Group
|
|
|
|
self.class.valid_group_path?(full_path)
|
|
|
|
else # User or non-Group Namespace
|
|
|
|
self.class.valid_user_path?(full_path)
|
2017-04-28 16:09:01 +00:00
|
|
|
end
|
|
|
|
end
|
2016-11-14 14:55:31 +00:00
|
|
|
|
2015-12-07 21:17:12 +00:00
|
|
|
def validate_each(record, attribute, value)
|
2017-05-24 20:59:26 +00:00
|
|
|
unless value =~ Gitlab::PathRegex.namespace_format_regex
|
|
|
|
record.errors.add(attribute, Gitlab::PathRegex.namespace_format_message)
|
2017-05-02 15:26:32 +00:00
|
|
|
return
|
2015-12-07 21:17:12 +00:00
|
|
|
end
|
|
|
|
|
2017-05-20 00:46:40 +00:00
|
|
|
unless path_valid_for_record?(record, value)
|
2017-04-18 14:27:11 +00:00
|
|
|
record.errors.add(attribute, "#{value} is a reserved name")
|
2017-04-05 13:41:00 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-07 21:17:12 +00:00
|
|
|
end
|