2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-21 08:05:37 -04:00
|
|
|
# Searches and reads file present on GitLab installation directory
|
2016-06-24 15:43:46 -04:00
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
module Finders
|
|
|
|
class GlobalTemplateFinder < BaseTemplateFinder
|
2020-02-18 10:08:51 -05:00
|
|
|
def initialize(base_dir, extension, categories = {}, exclusions: [])
|
2016-06-24 15:43:46 -04:00
|
|
|
@categories = categories
|
|
|
|
@extension = extension
|
2020-02-18 10:08:51 -05:00
|
|
|
@exclusions = exclusions
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
super(base_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(path)
|
|
|
|
File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(key)
|
2020-02-18 10:08:51 -05:00
|
|
|
return if excluded?(key)
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
file_name = "#{key}#{@extension}"
|
|
|
|
|
2018-12-04 10:59:01 -05: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 15:43:46 -04:00
|
|
|
directory = select_directory(file_name)
|
|
|
|
directory ? File.join(category_directory(directory), file_name) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_files_for(dir)
|
2018-11-19 21:01:13 -05:00
|
|
|
dir = "#{dir}/" unless dir.end_with?('/')
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
Dir.glob(File.join(dir, "*#{@extension}")).select do |f|
|
|
|
|
next if excluded?(f)
|
|
|
|
|
|
|
|
f =~ self.class.filter_regex(@extension)
|
|
|
|
end
|
2016-06-24 15:43:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-18 10:08:51 -05:00
|
|
|
def excluded?(file_name)
|
|
|
|
@exclusions.include?(file_name)
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def select_directory(file_name)
|
|
|
|
@categories.keys.find do |category|
|
|
|
|
File.exist?(File.join(category_directory(category), file_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|