Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dynamic_path_validator.rb « validators « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6819886ebf42acd8bfcf29fb0926e4c9247b1c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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
  class << self
    def valid_user_path?(path)
      "#{path}/" =~ Gitlab::PathRegex.root_namespace_path_regex
    end

    def valid_group_path?(path)
      "#{path}/" =~ Gitlab::PathRegex.full_namespace_path_regex
    end

    def valid_project_path?(path)
      "#{path}/" =~ Gitlab::PathRegex.full_project_path_regex
    end
  end

  def path_valid_for_record?(record, value)
    full_path = record.respond_to?(:full_path) ? record.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