2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-21 19:59:54 -04:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class PagesDomains < ::API::Base
|
2017-08-21 19:59:54 -04:00
|
|
|
include PaginationParams
|
|
|
|
|
2020-10-30 11:08:59 -04:00
|
|
|
feature_category :pages
|
|
|
|
|
2018-11-08 07:18:17 -05:00
|
|
|
PAGES_DOMAINS_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(domain: API::NO_SLASH_URL_PART_REGEX)
|
2018-02-19 01:02:13 -05:00
|
|
|
|
2017-08-21 19:59:54 -04:00
|
|
|
before do
|
|
|
|
authenticate!
|
|
|
|
end
|
|
|
|
|
|
|
|
after_validation do
|
|
|
|
normalize_params_file_to_string
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-21 19:59:54 -04:00
|
|
|
def find_pages_domain!
|
|
|
|
user_project.pages_domains.find_by(domain: params[:domain]) || not_found!('PagesDomain')
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-21 19:59:54 -04:00
|
|
|
|
|
|
|
def pages_domain
|
|
|
|
@pages_domain ||= find_pages_domain!
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_params_file_to_string
|
|
|
|
params.each do |k, v|
|
|
|
|
if v.is_a?(Hash) && v.key?(:tempfile)
|
|
|
|
params[k] = v[:tempfile].to_a.join('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-13 11:05:44 -05:00
|
|
|
resource :pages do
|
|
|
|
before do
|
|
|
|
require_pages_config_enabled!
|
2019-12-17 04:07:48 -05:00
|
|
|
authenticated_with_can_read_all_resources!
|
2017-11-13 11:05:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "Get all pages domains" do
|
|
|
|
success Entities::PagesDomainBasic
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get "domains" do
|
|
|
|
present paginate(PagesDomain.all), with: Entities::PagesDomainBasic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-21 19:59:54 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-11-13 11:05:44 -05:00
|
|
|
before do
|
|
|
|
require_pages_enabled!
|
|
|
|
end
|
|
|
|
|
2017-08-21 19:59:54 -04:00
|
|
|
desc 'Get all pages domains' do
|
|
|
|
success Entities::PagesDomain
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-21 19:59:54 -04:00
|
|
|
get ":id/pages/domains" do
|
|
|
|
authorize! :read_pages, user_project
|
|
|
|
|
|
|
|
present paginate(user_project.pages_domains.order(:domain)), with: Entities::PagesDomain
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-21 19:59:54 -04:00
|
|
|
|
|
|
|
desc 'Get a single pages domain' do
|
|
|
|
success Entities::PagesDomain
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :domain, type: String, desc: 'The domain'
|
|
|
|
end
|
2018-02-19 01:02:13 -05:00
|
|
|
get ":id/pages/domains/:domain", requirements: PAGES_DOMAINS_ENDPOINT_REQUIREMENTS do
|
2017-08-21 19:59:54 -04:00
|
|
|
authorize! :read_pages, user_project
|
|
|
|
|
|
|
|
present pages_domain, with: Entities::PagesDomain
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a new pages domain' do
|
|
|
|
success Entities::PagesDomain
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :domain, type: String, desc: 'The domain'
|
2019-09-10 12:24:10 -04:00
|
|
|
# rubocop:disable Scalability/FileUploads
|
2019-09-18 10:02:45 -04:00
|
|
|
# TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960
|
2019-11-15 13:06:24 -05:00
|
|
|
optional :certificate, types: [File, String], desc: 'The certificate', as: :user_provided_certificate
|
|
|
|
optional :key, types: [File, String], desc: 'The key', as: :user_provided_key
|
|
|
|
optional :auto_ssl_enabled, allow_blank: false, type: Boolean, default: false,
|
|
|
|
desc: "Enables automatic generation of SSL certificates issued by Let's Encrypt for custom domains."
|
2019-09-10 12:24:10 -04:00
|
|
|
# rubocop:enable Scalability/FileUploads
|
2019-06-21 08:06:12 -04:00
|
|
|
all_or_none_of :user_provided_certificate, :user_provided_key
|
2017-08-21 19:59:54 -04:00
|
|
|
end
|
|
|
|
post ":id/pages/domains" do
|
|
|
|
authorize! :update_pages, user_project
|
|
|
|
|
|
|
|
pages_domain_params = declared(params, include_parent_namespaces: false)
|
2019-06-21 08:06:12 -04:00
|
|
|
|
2017-08-21 19:59:54 -04:00
|
|
|
pages_domain = user_project.pages_domains.create(pages_domain_params)
|
|
|
|
|
|
|
|
if pages_domain.persisted?
|
|
|
|
present pages_domain, with: Entities::PagesDomain
|
|
|
|
else
|
|
|
|
render_validation_error!(pages_domain)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Updates a pages domain'
|
|
|
|
params do
|
|
|
|
requires :domain, type: String, desc: 'The domain'
|
2019-09-10 12:24:10 -04:00
|
|
|
# rubocop:disable Scalability/FileUploads
|
2019-09-18 10:02:45 -04:00
|
|
|
# TODO: remove rubocop disable - https://gitlab.com/gitlab-org/gitlab/issues/14960
|
2019-11-15 13:06:24 -05:00
|
|
|
optional :certificate, types: [File, String], desc: 'The certificate', as: :user_provided_certificate
|
|
|
|
optional :key, types: [File, String], desc: 'The key', as: :user_provided_key
|
|
|
|
optional :auto_ssl_enabled, allow_blank: true, type: Boolean,
|
|
|
|
desc: "Enables automatic generation of SSL certificates issued by Let's Encrypt for custom domains."
|
2019-09-10 12:24:10 -04:00
|
|
|
# rubocop:enable Scalability/FileUploads
|
2017-08-21 19:59:54 -04:00
|
|
|
end
|
2018-02-19 01:02:13 -05:00
|
|
|
put ":id/pages/domains/:domain", requirements: PAGES_DOMAINS_ENDPOINT_REQUIREMENTS do
|
2017-08-21 19:59:54 -04:00
|
|
|
authorize! :update_pages, user_project
|
|
|
|
|
2019-11-15 13:06:24 -05:00
|
|
|
pages_domain_params = declared(params, include_parent_namespaces: false, include_missing: false)
|
2017-08-21 19:59:54 -04:00
|
|
|
|
|
|
|
# Remove empty private key if certificate is not empty.
|
2019-06-21 08:06:12 -04:00
|
|
|
if pages_domain_params[:user_provided_certificate] && !pages_domain_params[:user_provided_key]
|
|
|
|
pages_domain_params.delete(:user_provided_key)
|
2017-08-21 19:59:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if pages_domain.update(pages_domain_params)
|
|
|
|
present pages_domain, with: Entities::PagesDomain
|
|
|
|
else
|
|
|
|
render_validation_error!(pages_domain)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Delete a pages domain'
|
|
|
|
params do
|
|
|
|
requires :domain, type: String, desc: 'The domain'
|
|
|
|
end
|
2018-02-19 01:02:13 -05:00
|
|
|
delete ":id/pages/domains/:domain", requirements: PAGES_DOMAINS_ENDPOINT_REQUIREMENTS do
|
2017-08-21 19:59:54 -04:00
|
|
|
authorize! :update_pages, user_project
|
|
|
|
|
|
|
|
pages_domain.destroy
|
2020-01-16 13:08:46 -05:00
|
|
|
|
|
|
|
no_content!
|
2017-08-21 19:59:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|