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 | |
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')
-rw-r--r-- | app/models/namespace.rb | 2 | ||||
-rw-r--r-- | app/models/project.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-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 |
8 files changed, 94 insertions, 58 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 0601a61a926..4d401e7ba18 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -36,7 +36,7 @@ class Namespace < ActiveRecord::Base validates :path, presence: true, length: { maximum: 255 }, - dynamic_path: true + namespace_path: true validate :nesting_level_allowed diff --git a/app/models/project.rb b/app/models/project.rb index 2f9b80d0514..5a0cbfeb282 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -240,10 +240,8 @@ class Project < ActiveRecord::Base message: Gitlab::Regex.project_name_regex_message } validates :path, presence: true, - dynamic_path: true, + project_path: true, length: { maximum: 255 }, - format: { with: Gitlab::PathRegex.project_path_format_regex, - message: Gitlab::PathRegex.project_path_format_message }, uniqueness: { scope: :namespace_id } validates :namespace, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index bcda4564595..87a6c63cfaa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -146,7 +146,7 @@ class User < ActiveRecord::Base presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: Gitlab::Database::MAX_INT_VALUE } validates :username, - dynamic_path: true, + user_path: true, presence: true, uniqueness: { case_sensitive: false } 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 |