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