demarches-normaliennes/app/controllers/administrateurs/administrateur_controller.rb

97 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Administrateurs
2018-04-13 16:56:00 +02:00
class AdministrateurController < ApplicationController
before_action :authenticate_administrateur!
2023-07-24 11:05:02 +02:00
before_action :alert_for_missing_siret_service
before_action :alert_for_missing_service
2022-07-28 15:12:24 +02:00
helper_method :administrateur_as_manager?
2018-11-14 16:24:34 +01:00
2023-07-12 16:50:28 +02:00
def nav_bar_profile
:administrateur
end
2018-11-14 16:24:34 +01:00
def retrieve_procedure
id = params[:procedure_id] || params[:id]
@procedure = current_administrateur.procedures.find(id)
Sentry.configure_scope do |scope|
scope.set_tags(procedure: @procedure.id)
end
2018-11-14 16:24:34 +01:00
rescue ActiveRecord::RecordNotFound
flash.alert = 'Démarche inexistante'
2019-09-24 16:38:58 +02:00
redirect_to admin_procedures_path, status: 404
2018-11-14 16:24:34 +01:00
end
def reset_procedure
@procedure.reset!
2018-11-14 16:24:34 +01:00
end
def preload_revisions
ProcedureRevisionPreloader.new(@procedure.revisions).all
@procedure.association(:draft_revision).target = @procedure.revisions.find { _1.id == @procedure.draft_revision.id }
if @procedure.published_revision
@procedure.association(:published_revision).target = @procedure.revisions.find { _1.id == @procedure.published_revision.id }
end
end
def ensure_not_super_admin!
2022-07-28 15:12:24 +02:00
if administrateur_as_manager?
redirect_back fallback_location: root_url, alert: "Interdit aux super admins", status: 403
end
end
private
2022-07-28 15:12:24 +02:00
def administrateur_as_manager?
id = params[:procedure_id] || params[:id]
current_administrateur.administrateurs_procedures
.exists?(procedure_id: id, manager: true)
end
2023-07-24 11:05:02 +02:00
def alert_for_missing_siret_service
return if flash[:alert].present?
2023-07-24 11:05:02 +02:00
procedures = missing_siret_services
if procedures.any?
errors = []
errors << I18n.t('shared.procedures.no_siret')
procedures.each do |p|
errors << I18n.t('shared.procedures.add_siret_to_service_without_siret_html', link: edit_admin_service_path(p.service, procedure_id: p.id), nom: p.service.nom)
end
flash.now.alert = errors
end
end
def missing_siret_services
current_administrateur
.procedures.publiees
.joins(:service)
.where(service: { siret: nil })
end
def alert_for_missing_service
return if flash[:alert].present?
procedures = missing_service
if procedures.any?
errors = []
errors << I18n.t('shared.procedures.no_service')
procedures.each do |p|
errors << I18n.t('shared.procedures.add_service_html', link: admin_services_path(procedure_id: p.id), id: p.id)
end
flash.now.alert = errors
end
end
def missing_service
current_administrateur
.procedures.publiees
.where(service_id: nil)
end
2018-04-13 16:56:00 +02:00
end
end