# frozen_string_literal: true # Controlador de sitios class SitesController < ApplicationController include Pundit before_action :authenticate_usuarie! # Ver un listado de sitios def index authorize Site @sites = current_usuarie.sites end # No tenemos propiedades de un sitio aún, así que vamos al listado de # artículos def show site = find_site authorize site redirect_to site_posts_path(site) end def new @site = Site.new authorize @site @site.deploys.build type: 'DeployLocal' @site.deploys.build type: 'DeployZip' end def create @site = Site.new(site_params) @site.roles << Rol.new(site: @site, usuarie: current_usuarie, temporal: false, rol: 'usuarie') # XXX: Necesitamos escribir la configuración después porque queremos # registrar quién hizo los cambios en el repositorio if @site.save && @site.config.write(current_usuarie) redirect_to site_path(@site) else render 'new' end end def edit @site = find_site authorize @site end def update @site = find_site authorize @site # XXX: Necesitamos escribir la configuración después porque queremos # registrar quién hizo los cambios en el repositorio if @site.update(site_params) && @site.config.write(current_usuarie) redirect_to sites_path else render 'edit' end end # Envía un archivo del directorio público de Jekyll def send_public_file authorize Site @site = find_site file = [params[:basename], params[:format]].join('.') path = File.join(@site.path, 'public', params[:type], file) path = Pathname.new path # TODO: Verificar que no nos estén sacando archivos del sistema, como # /etc/passwd # # En sí el matcher de la ruta no admite .. así que estaríamos semi # cubiertas. if path.exist? send_file path, 'x-sendfile': true else render status: 404 end end def enqueue site = find_site authorize site # XXX: Convertir en una máquina de estados? DeployWorker.perform_async site.id if site.enqueue! redirect_to sites_path end def reorder_posts @site = find_site authorize @site lang = params.require(:posts).require(:lang) if params[:posts][:force].present? result = @site.reorder_collection! lang else result = @site .reorder_collection(lang, params.require(:posts).require(:order)) end if result flash[:info] = I18n.t('info.posts.reorder') else flash[:danger] = I18n.t('errors.posts.reorder') end redirect_to site_posts_path @site end def fetch @site = find_site authorize @site @commits = @site.repository.commits end def merge @site = find_site authorize @site if @site.repository.merge(current_usuarie) flash[:success] = I18n.t('sites.fetch.merge.success') else flash[:error] = I18n.t('sites.fetch.merge.error') end redirect_to sites_path end private def site_params params.require(:site) .permit(:name, :design_id, :licencia_id, :description, :title, deploys_attributes: %i[type id _destroy]) end end