2019-09-05 18:56:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
# API para sitios
|
|
|
|
class SitesController < BaseController
|
2024-12-23 20:08:55 +00:00
|
|
|
SUBDOMAIN = ".#{Site.domain}"
|
|
|
|
PARTS = Site.domain.split('.').count
|
2020-02-06 16:11:17 +00:00
|
|
|
|
2024-12-23 20:09:17 +00:00
|
|
|
if Rails.env.production?
|
|
|
|
http_basic_authenticate_with name: ENV['HTTP_BASIC_USER'],
|
|
|
|
password: ENV['HTTP_BASIC_PASSWORD']
|
|
|
|
end
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
# Lista de nombres de dominios a emitir certificados
|
2019-09-06 23:40:33 +00:00
|
|
|
def index
|
2024-12-23 19:36:27 +00:00
|
|
|
render json: alternative_names.concat(api_names).concat(www_names)
|
2019-09-06 23:40:33 +00:00
|
|
|
end
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
private
|
2019-09-05 18:56:24 +00:00
|
|
|
|
2024-12-23 19:36:27 +00:00
|
|
|
# @param query [ActiveRecord::Relation]
|
|
|
|
# @return [Array<String>]
|
|
|
|
def hostname_of(query)
|
|
|
|
query.pluck(Arel.sql("values->>'hostname'")).compact.uniq
|
|
|
|
end
|
|
|
|
|
2022-03-08 18:09:31 +00:00
|
|
|
def canonicalize(name)
|
|
|
|
name.end_with?('.') ? name[0..-2] : "#{name}.#{Site.domain}"
|
|
|
|
end
|
|
|
|
|
2024-12-23 20:08:55 +00:00
|
|
|
# Es un subdominio directo del dominio principal
|
|
|
|
#
|
|
|
|
# @param name [String]
|
|
|
|
# @return [Bool]
|
2023-04-19 17:56:56 +00:00
|
|
|
def subdomain?(name)
|
2024-12-23 20:08:55 +00:00
|
|
|
name.end_with?(SUBDOMAIN) && name.split('.').count == (PARTS + 1)
|
2023-04-19 17:56:56 +00:00
|
|
|
end
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
# Dominios alternativos
|
2024-12-23 20:08:55 +00:00
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
2020-05-29 15:42:39 +00:00
|
|
|
def alternative_names
|
2024-12-23 19:36:27 +00:00
|
|
|
hostname_of(DeployAlternativeDomain.all).concat(hostname_of(DeployLocalizedDomain)).map do |name|
|
2022-03-08 18:09:31 +00:00
|
|
|
canonicalize name
|
2023-04-19 17:56:56 +00:00
|
|
|
end.reject do |name|
|
|
|
|
subdomain? name
|
2022-03-08 18:09:31 +00:00
|
|
|
end
|
2020-05-29 15:42:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Obtener todos los sitios con API habilitada, es decir formulario
|
|
|
|
# de contacto y/o colaboración anónima.
|
2024-12-23 20:08:55 +00:00
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
2020-05-29 15:42:39 +00:00
|
|
|
def api_names
|
|
|
|
Site.where(contact: true)
|
|
|
|
.or(Site.where(colaboracion_anonima: true))
|
2024-12-23 19:36:27 +00:00
|
|
|
.pluck(:name).map do |name|
|
|
|
|
"api.#{name}"
|
|
|
|
end.map do |name|
|
2022-03-08 18:09:31 +00:00
|
|
|
canonicalize name
|
2023-04-19 17:56:56 +00:00
|
|
|
end.reject do |name|
|
|
|
|
subdomain? name
|
2022-03-08 18:09:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-16 16:17:39 +00:00
|
|
|
# Todos los dominios con WWW habilitado
|
2022-03-08 18:09:31 +00:00
|
|
|
def www_names
|
2024-12-23 19:36:27 +00:00
|
|
|
Site.where(id: DeployWww.all.pluck(:site_id)).pluck(:name).map do |name|
|
|
|
|
canonicalize "www.#{name}"
|
2023-03-16 16:17:39 +00:00
|
|
|
end
|
2019-09-05 18:56:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|