diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-11-03 19:48:15 +0100 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-11-06 14:46:53 +0100 |
commit | a10925e1c37e7dab19de346c553311adfaccb86c (patch) | |
tree | ee211cc4bc6a830284a23583cd3d06e529793b10 /app/validators | |
parent | 97b80fefeb5da20798423b62b63fa9faa08ac118 (diff) | |
download | gitlab-ce-a10925e1c37e7dab19de346c553311adfaccb86c.tar.gz |
Reallow project paths ending in periodsdm-reallow-project-path-ending-in-period
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/abstract_path_validator.rb | 38 | ||||
-rw-r--r-- | app/validators/dynamic_path_validator.rb | 53 | ||||
-rw-r--r-- | app/validators/namespace_path_validator.rb | 19 | ||||
-rw-r--r-- | app/validators/project_path_validator.rb | 19 | ||||
-rw-r--r-- | app/validators/user_path_validator.rb | 15 |
5 files changed, 91 insertions, 53 deletions
diff --git a/app/validators/abstract_path_validator.rb b/app/validators/abstract_path_validator.rb new file mode 100644 index 00000000000..adbccb65a84 --- /dev/null +++ b/app/validators/abstract_path_validator.rb @@ -0,0 +1,38 @@ +class AbstractPathValidator < ActiveModel::EachValidator + extend Gitlab::EncodingHelper + + def self.path_regex + raise NotImplementedError + end + + def self.format_regex + raise NotImplementedError + end + + def self.format_error_message + raise NotImplementedError + end + + def self.full_path(record, value) + value + end + + def self.valid_path?(path) + encode!(path) + "#{path}/" =~ path_regex + end + + def validate_each(record, attribute, value) + unless value =~ self.class.format_regex + record.errors.add(attribute, self.class.format_error_message) + return + end + + full_path = self.class.full_path(record, value) + return unless full_path + + unless self.class.valid_path?(full_path) + record.errors.add(attribute, "#{value} is a reserved name") + end + end +end diff --git a/app/validators/dynamic_path_validator.rb b/app/validators/dynamic_path_validator.rb deleted file mode 100644 index 4688aabc2a8..00000000000 --- a/app/validators/dynamic_path_validator.rb +++ /dev/null @@ -1,53 +0,0 @@ -# DynamicPathValidator -# -# Custom validator for GitLab path values. -# These paths are assigned to `Namespace` (& `Group` as a subclass) & `Project` -# -# Values are checked for formatting and exclusion from a list of illegal path -# names. -class DynamicPathValidator < ActiveModel::EachValidator - extend Gitlab::EncodingHelper - - class << self - def valid_user_path?(path) - encode!(path) - "#{path}/" =~ Gitlab::PathRegex.root_namespace_path_regex - end - - def valid_group_path?(path) - encode!(path) - "#{path}/" =~ Gitlab::PathRegex.full_namespace_path_regex - end - - def valid_project_path?(path) - encode!(path) - "#{path}/" =~ Gitlab::PathRegex.full_project_path_regex - end - end - - def path_valid_for_record?(record, value) - full_path = record.respond_to?(:build_full_path) ? record.build_full_path : value - - return true unless full_path - - case record - when Project - self.class.valid_project_path?(full_path) - when Group - self.class.valid_group_path?(full_path) - else # User or non-Group Namespace - self.class.valid_user_path?(full_path) - end - end - - def validate_each(record, attribute, value) - unless value =~ Gitlab::PathRegex.namespace_format_regex - record.errors.add(attribute, Gitlab::PathRegex.namespace_format_message) - return - end - - unless path_valid_for_record?(record, value) - record.errors.add(attribute, "#{value} is a reserved name") - end - end -end diff --git a/app/validators/namespace_path_validator.rb b/app/validators/namespace_path_validator.rb new file mode 100644 index 00000000000..4a0aa64ae0c --- /dev/null +++ b/app/validators/namespace_path_validator.rb @@ -0,0 +1,19 @@ +class NamespacePathValidator < AbstractPathValidator + extend Gitlab::EncodingHelper + + def self.path_regex + Gitlab::PathRegex.full_namespace_path_regex + end + + def self.format_regex + Gitlab::PathRegex.namespace_format_regex + end + + def self.format_error_message + Gitlab::PathRegex.namespace_format_message + end + + def self.full_path(record, value) + record.build_full_path + end +end diff --git a/app/validators/project_path_validator.rb b/app/validators/project_path_validator.rb new file mode 100644 index 00000000000..829b596ad3c --- /dev/null +++ b/app/validators/project_path_validator.rb @@ -0,0 +1,19 @@ +class ProjectPathValidator < AbstractPathValidator + extend Gitlab::EncodingHelper + + def self.path_regex + Gitlab::PathRegex.full_project_path_regex + end + + def self.format_regex + Gitlab::PathRegex.project_path_format_regex + end + + def self.format_error_message + Gitlab::PathRegex.project_path_format_message + end + + def self.full_path(record, value) + record.build_full_path + end +end diff --git a/app/validators/user_path_validator.rb b/app/validators/user_path_validator.rb new file mode 100644 index 00000000000..adf02901802 --- /dev/null +++ b/app/validators/user_path_validator.rb @@ -0,0 +1,15 @@ +class UserPathValidator < AbstractPathValidator + extend Gitlab::EncodingHelper + + def self.path_regex + Gitlab::PathRegex.root_namespace_path_regex + end + + def self.format_regex + Gitlab::PathRegex.namespace_format_regex + end + + def self.format_error_message + Gitlab::PathRegex.namespace_format_message + end +end |