debian-mirror-gitlab/app/controllers/projects/templates_controller.rb

51 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
class Projects::TemplatesController < Projects::ApplicationController
2019-07-07 11:18:12 +05:30
before_action :authenticate_user!
before_action :authorize_can_read_issuable!
before_action :get_template_class
2016-09-13 17:45:13 +05:30
2021-01-03 14:25:43 +05:30
feature_category :templates
2021-01-29 00:20:46 +05:30
def index
templates = @template_type.template_subsets(project)
respond_to do |format|
format.json { render json: templates.to_json }
end
end
2016-09-13 17:45:13 +05:30
def show
template = @template_type.find(params[:key], project)
respond_to do |format|
format.json { render json: template.to_json }
end
end
2019-12-21 20:55:43 +05:30
def names
respond_to do |format|
2021-04-17 20:07:23 +05:30
format.json { render json: TemplateFinder.all_template_names_hash_or_array(project, params[:template_type].to_s) }
2019-12-21 20:55:43 +05:30
end
end
2016-09-13 17:45:13 +05:30
private
2019-07-07 11:18:12 +05:30
# User must have:
# - `read_merge_request` to see merge request templates, or
# - `read_issue` to see issue templates
#
# Note params[:template_type] has a route constraint to limit it to
# `merge_request` or `issue`
def authorize_can_read_issuable!
action = [:read_, params[:template_type]].join
authorize_action!(action)
end
2016-09-13 17:45:13 +05:30
def get_template_class
template_types = { issue: Gitlab::Template::IssueTemplate, merge_request: Gitlab::Template::MergeRequestTemplate }.with_indifferent_access
@template_type = template_types[params[:template_type]]
end
end