2018-11-20 02:01:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-21 12:05:37 +00:00
|
|
|
# Searches and reads file present on GitLab installation directory
|
2016-06-24 19:43:46 +00:00
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
module Finders
|
|
|
|
class GlobalTemplateFinder < BaseTemplateFinder
|
2021-01-19 21:10:45 +00:00
|
|
|
def initialize(base_dir, extension, categories = {}, include_categories_for_file = {}, excluded_patterns: [])
|
2016-06-24 19:43:46 +00:00
|
|
|
@categories = categories
|
|
|
|
@extension = extension
|
2021-01-19 21:10:45 +00:00
|
|
|
@include_categories_for_file = include_categories_for_file
|
2020-09-08 12:08:41 +00:00
|
|
|
@excluded_patterns = excluded_patterns
|
2020-02-18 15:08:51 +00:00
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
super(base_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(path)
|
|
|
|
File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(key)
|
2020-02-18 15:08:51 +00:00
|
|
|
return if excluded?(key)
|
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
file_name = "#{key}#{@extension}"
|
|
|
|
|
2018-12-04 15:59:01 +00:00
|
|
|
# The key is untrusted input, so ensure we can't be directed outside
|
|
|
|
# of base_dir
|
|
|
|
Gitlab::Utils.check_path_traversal!(file_name)
|
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
directory = select_directory(file_name)
|
|
|
|
directory ? File.join(category_directory(directory), file_name) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_files_for(dir)
|
2018-11-20 02:01:13 +00:00
|
|
|
dir = "#{dir}/" unless dir.end_with?('/')
|
2020-02-18 15:08:51 +00:00
|
|
|
|
|
|
|
Dir.glob(File.join(dir, "*#{@extension}")).select do |f|
|
|
|
|
next if excluded?(f)
|
|
|
|
|
|
|
|
f =~ self.class.filter_regex(@extension)
|
|
|
|
end
|
2016-06-24 19:43:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-18 15:08:51 +00:00
|
|
|
def excluded?(file_name)
|
2020-09-08 12:08:41 +00:00
|
|
|
@excluded_patterns.any? { |pattern| pattern.match?(file_name) }
|
2020-02-18 15:08:51 +00:00
|
|
|
end
|
|
|
|
|
2016-06-24 19:43:46 +00:00
|
|
|
def select_directory(file_name)
|
2021-01-19 21:10:45 +00:00
|
|
|
categories = @categories
|
|
|
|
categories.merge!(@include_categories_for_file[file_name]) if @include_categories_for_file[file_name].present?
|
|
|
|
categories.keys.find do |category|
|
2016-06-24 19:43:46 +00:00
|
|
|
File.exist?(File.join(category_directory(category), file_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|