From b2508de39b6139544c413414ffe74ad02974f0ad Mon Sep 17 00:00:00 2001 From: f Date: Sun, 8 Aug 2021 21:38:33 -0300 Subject: [PATCH] Redirigir al nombre del sitio sin incluir dominio --- app/controllers/application_controller.rb | 46 ++++++++++++++++---- app/controllers/collaborations_controller.rb | 4 +- app/controllers/sites_controller.rb | 6 ++- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index acd0134d..b3d355bb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,13 +8,14 @@ class ApplicationController < ActionController::Base before_action :prepare_exception_notifier before_action :configure_permitted_parameters, if: :devise_controller? + before_action :redirect_to_site_name!, only: %i[index show edit new], if: :site_id_contains_hostname? around_action :set_locale rescue_from ActionController::RoutingError, with: :page_not_found rescue_from ActionController::ParameterMissing, with: :page_not_found before_action do - Rack::MiniProfiler.authorize_request if current_usuarie&.email&.ends_with?('@' + ENV.fetch('SUTTY', 'sutty.nl')) + Rack::MiniProfiler.authorize_request if Rails.env.development? end # No tenemos índice de sutty, vamos directamente a ver el listado de @@ -29,15 +30,11 @@ class ApplicationController < ActionController::Base /[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/ =~ string end - # Encontrar un sitio por su nombre + # Encontrar un sitio por su nombre. def find_site - id = params[:site_id] || params[:id] - - unless (site = current_usuarie.sites.find_by_name(id)) - raise SiteNotFound + current_usuarie.sites.find_by_name(site_id).tap do |site| + raise SiteNotFound unless site end - - site end # Devuelve el idioma actual y si no lo encuentra obtiene uno por @@ -62,6 +59,39 @@ class ApplicationController < ActionController::Base render 'application/page_not_found', status: :not_found end + # Retrocompatibilidad con sitios cuyo nombre era su hostname. + # + # @see Deploy + def site_id_contains_hostname? + site_id&.end_with? '.' + end + + # Redirigir a la misma URL con el site_id cambiado. + # + # TODO: Eliminar cuando detectemos que no hay más redirecciones. + def redirect_to_site_name! + params.permit! + params[:site_id] = Deploy.site_name_from_hostname(site_id[0..-2]) + + redirect_to params, status: :moved_permanently + end + + # Los controladores dentro de SitesController van a usar site_id + # mientras que SiteController va a usar ID. + # + # @see SitesController + # @return [String,Nil] + def site_id + @site_id ||= params[:site_id] + end + + # El sitio actual + # + # @return [Site] + def site + @site ||= find_site + end + protected def configure_permitted_parameters diff --git a/app/controllers/collaborations_controller.rb b/app/controllers/collaborations_controller.rb index 2caa1272..ab13ef58 100644 --- a/app/controllers/collaborations_controller.rb +++ b/app/controllers/collaborations_controller.rb @@ -7,7 +7,7 @@ class CollaborationsController < ApplicationController include Pundit def collaborate - @site = Site.find_by_name(params[:site_id]) + @site = find_site authorize Collaboration.new(@site) @invitade = current_usuarie || @site.usuaries.build @@ -21,7 +21,7 @@ class CollaborationsController < ApplicationController # # * Si le usuarie existe y no está logueade, pedirle la contraseña def accept_collaboration - @site = Site.find_by_name(params[:site_id]) + @site = find_site authorize Collaboration.new(@site) @invitade = current_usuarie diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index bdaa9011..d9914232 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -139,8 +139,10 @@ class SitesController < ApplicationController private - def site - @site ||= find_site + # En los controladores dentro de este controlador vamos a usar :id + # para obtener el nombre. + def site_id + @site_id ||= params[:id] end def site_params