debian-mirror-gitlab/app/validators/dynamic_path_validator.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
# DynamicPathValidator
#
# Custom validator for GitLab path values.
# These paths are assigned to `Namespace` (& `Group` as a subclass) & `Project`
#
2017-09-10 17:25:29 +05:30
# Values are checked for formatting and exclusion from a list of illegal path
2017-08-17 22:00:37 +05:30
# names.
class DynamicPathValidator < ActiveModel::EachValidator
2017-09-10 17:25:29 +05:30
extend Gitlab::EncodingHelper
2017-08-17 22:00:37 +05:30
class << self
2017-09-10 17:25:29 +05:30
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
2017-08-17 22:00:37 +05:30
end
def valid_project_path?(path)
2017-09-10 17:25:29 +05:30
encode!(path)
"#{path}/" =~ Gitlab::PathRegex.full_project_path_regex
2017-08-17 22:00:37 +05:30
end
end
def path_valid_for_record?(record, value)
2017-09-10 17:25:29 +05:30
full_path = record.respond_to?(:build_full_path) ? record.build_full_path : value
2017-08-17 22:00:37 +05:30
return true unless full_path
case record
when Project
self.class.valid_project_path?(full_path)
2017-09-10 17:25:29 +05:30
when Group
self.class.valid_group_path?(full_path)
else # User or non-Group Namespace
self.class.valid_user_path?(full_path)
2017-08-17 22:00:37 +05:30
end
end
def validate_each(record, attribute, value)
2017-09-10 17:25:29 +05:30
unless value =~ Gitlab::PathRegex.namespace_format_regex
record.errors.add(attribute, Gitlab::PathRegex.namespace_format_message)
2017-08-17 22:00:37 +05:30
return
end
unless path_valid_for_record?(record, value)
record.errors.add(attribute, "#{value} is a reserved name")
end
end
end