I due piu' gravi potevano causare perdita di dati silenziosa. AppServiceProvider chiamava key:generate --force a ogni richiesta quando APP_KEY risultava vuota. Poiche' nome e cognome dei proclamatori sono cifrati con quella chiave, ogni riga scritta sotto una chiave poi sostituita diventava irrecuperabile. La chiave e' ora risolta una sola volta all'avvio del container e il provider solleva un'eccezione se manca, invece di rigenerarla. importTerritoryPdfFolder() caricava i PDF, costruiva l'elenco dei file e poi lo scartava senza mai accodare il job: nessun errore, nessun territorio aggiornato, e i file restavano su disco a crescita illimitata perche' la pulizia viveva solo nel finally del job mai eseguito. Sicurezza: - i permessi non venivano riapplicati alle richieste /livewire/update, per cui un permesso revocato restava attivo fino al reload della pagina - host header injection: nginx passava l'Host del client come X-Forwarded-Host e Laravel si fidava di tutti i proxy, abbastanza per far generare a route() link verso domini arbitrari, inclusi i PDF pubblici che contengono un codice di accesso valido - lo schema forwarded e' ora deciso dal server e non dall'header del client - campagna_id in Rientra e' #[Locked]: era derivato lato server ma manipolabile dal client, permettendo di conteggiare un rientro su una campagna arbitraria - sortField del registro passava non validato a orderBy(), con 500 su colonna inesistente - SESSION_SECURE_COOKIE ora vale true fuori da local - limiti su dimensione e numero di voci negli import ZIP Integrita' e prestazioni: - il default di assignment_link_ttl_hours era rimasto 24 dopo la conversione da ore a mesi: i link PDF pubblici duravano due anni invece di uno mese - il seeder usava syncPermissions(), che a un secondo giro azzerava i permessi personalizzati dei ruoli; ora e' additivo - pdftoppm senza timeout poteva bloccare il worker indefinitamente - nomi file deterministici nell'import: "1 A" e "1-A" scrivevano sullo stesso PDF, sovrascrivendosi - Setting::flushCache() dopo le cancellazioni massive, che non emettono eventi e lasciavano la cache puntata a una riga inesistente - riga settings creata dal seeder, per evitare due singleton concorrenti - rimosse due migrazioni ridondanti che rompevano migrate:rollback - indici su assigned_at, (attivo, deleted_at) e deleted_at - risolti gli N+1 negli elenchi territori e proclamatori Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
249 lines
8.9 KiB
PHP
249 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use App\Models\Assegnazione;
|
|
use App\Models\AnnoTeocratico;
|
|
use App\Models\Campagna;
|
|
use App\Models\Proclamatore;
|
|
use App\Models\Territorio;
|
|
use App\Models\Zona;
|
|
use App\Models\Tipologia;
|
|
|
|
class Registro extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
public string $search = '';
|
|
public string $filtroAnno = '';
|
|
public string $filtroZona = '';
|
|
public string $filtroTipologia = '';
|
|
public string $filtroStato = ''; // aperte, chiuse
|
|
public string $sortField = 'territorio_numero';
|
|
public string $sortDirection = 'asc';
|
|
|
|
// ─── Modal create/edit ──────────────────────────────────────
|
|
public bool $showModal = false;
|
|
public ?int $editingId = null;
|
|
|
|
public string $form_territorio_id = '';
|
|
public string $form_proclamatore_id = '';
|
|
public string $form_anno_id = '';
|
|
public string $form_assigned_at = '';
|
|
public string $form_returned_at = '';
|
|
public bool $form_counted_in_campaign = false;
|
|
public string $form_campaign_id = '';
|
|
public string $form_note = '';
|
|
|
|
// ─── Delete confirm ─────────────────────────────────────────
|
|
public bool $showDeleteConfirm = false;
|
|
public ?int $deleteId = null;
|
|
|
|
protected $queryString = [
|
|
'search' => ['except' => ''],
|
|
'filtroAnno' => ['except' => ''],
|
|
'filtroZona' => ['except' => ''],
|
|
'filtroTipologia' => ['except' => ''],
|
|
'filtroStato' => ['except' => ''],
|
|
];
|
|
|
|
public function updatingSearch()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
/**
|
|
* Columns that may be sorted on.
|
|
*
|
|
* $sortField reaches orderBy() directly, and it is a public Livewire
|
|
* property, so the client controls it. Laravel's grammar prevents SQL
|
|
* injection, but an unknown column still raises "Column not found" and
|
|
* returns a 500 — trivially triggerable from the browser console.
|
|
*/
|
|
private const SORTABLE_FIELDS = [
|
|
'territorio_numero',
|
|
'assigned_at',
|
|
'returned_at',
|
|
];
|
|
|
|
public function sortBy(string $field)
|
|
{
|
|
if (! in_array($field, self::SORTABLE_FIELDS, true)) {
|
|
return;
|
|
}
|
|
|
|
if ($this->sortField === $field) {
|
|
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sortField = $field;
|
|
$this->sortDirection = 'desc';
|
|
}
|
|
}
|
|
|
|
// ─── Admin CRUD ─────────────────────────────────────────────
|
|
|
|
public function openCreate(): void
|
|
{
|
|
abort_if(!auth()->user()->can('settings.manage'), 403);
|
|
$this->editingId = null;
|
|
$this->form_territorio_id = '';
|
|
$this->form_proclamatore_id = '';
|
|
$this->form_anno_id = '';
|
|
$this->form_assigned_at = now()->format('Y-m-d');
|
|
$this->form_returned_at = '';
|
|
$this->form_counted_in_campaign = false;
|
|
$this->form_campaign_id = '';
|
|
$this->form_note = '';
|
|
$this->resetValidation();
|
|
$this->showModal = true;
|
|
}
|
|
|
|
public function openEdit(int $id): void
|
|
{
|
|
abort_if(!auth()->user()->can('settings.manage'), 403);
|
|
$a = Assegnazione::findOrFail($id);
|
|
$this->editingId = $id;
|
|
$this->form_territorio_id = (string) $a->territorio_id;
|
|
$this->form_proclamatore_id = (string) $a->proclamatore_id;
|
|
$this->form_anno_id = (string) $a->anno_teocratico_id;
|
|
$this->form_assigned_at = $a->assigned_at?->format('Y-m-d') ?? '';
|
|
$this->form_returned_at = $a->returned_at?->format('Y-m-d') ?? '';
|
|
$this->form_counted_in_campaign = (bool) $a->counted_in_campaign;
|
|
$this->form_campaign_id = (string) ($a->campaign_id ?? '');
|
|
$this->form_note = $a->note ?? '';
|
|
$this->resetValidation();
|
|
$this->showModal = true;
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
abort_if(!auth()->user()->can('settings.manage'), 403);
|
|
$this->validate([
|
|
'form_territorio_id' => 'required|exists:territori,id',
|
|
'form_proclamatore_id' => 'required|exists:proclamatori,id',
|
|
'form_anno_id' => 'required|exists:anni_teocratici,id',
|
|
'form_assigned_at' => 'required|date',
|
|
'form_returned_at' => 'nullable|date|after_or_equal:form_assigned_at',
|
|
'form_campaign_id' => 'nullable|exists:campagne,id',
|
|
]);
|
|
|
|
$data = [
|
|
'territorio_id' => $this->form_territorio_id,
|
|
'proclamatore_id' => $this->form_proclamatore_id,
|
|
'anno_teocratico_id' => $this->form_anno_id,
|
|
'assigned_at' => $this->form_assigned_at,
|
|
'returned_at' => $this->form_returned_at ?: null,
|
|
'counted_in_campaign' => $this->form_counted_in_campaign,
|
|
'campaign_id' => $this->form_campaign_id ?: null,
|
|
'note' => $this->form_note ?: null,
|
|
];
|
|
|
|
if ($this->editingId) {
|
|
Assegnazione::findOrFail($this->editingId)->update($data);
|
|
} else {
|
|
$data['created_by'] = auth()->id();
|
|
Assegnazione::create($data);
|
|
}
|
|
|
|
$this->showModal = false;
|
|
}
|
|
|
|
public function askDelete(int $id): void
|
|
{
|
|
abort_if(!auth()->user()->can('settings.manage'), 403);
|
|
$this->deleteId = $id;
|
|
$this->showDeleteConfirm = true;
|
|
}
|
|
|
|
public function deleteConfirmed(): void
|
|
{
|
|
abort_if(!auth()->user()->can('settings.manage'), 403);
|
|
if ($this->deleteId) {
|
|
Assegnazione::findOrFail($this->deleteId)->delete();
|
|
}
|
|
$this->deleteId = null;
|
|
$this->showDeleteConfirm = false;
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
if ($this->filtroAnno === '') {
|
|
$annoCorrente = AnnoTeocratico::corrente();
|
|
$this->filtroAnno = (string) $annoCorrente->id;
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$query = Assegnazione::with(['territorio.zona', 'territorio.assegnazioneCorrente', 'proclamatore', 'annoTeocratico', 'campagna']);
|
|
|
|
if ($this->filtroAnno) {
|
|
$query->where('anno_teocratico_id', $this->filtroAnno);
|
|
}
|
|
|
|
if ($this->filtroStato === 'aperte') {
|
|
$query->aperte();
|
|
} elseif ($this->filtroStato === 'chiuse') {
|
|
$query->chiuse();
|
|
}
|
|
|
|
if ($this->filtroZona) {
|
|
$query->whereHas('territorio', fn($q) => $q->where('zona_id', $this->filtroZona));
|
|
}
|
|
|
|
if ($this->filtroTipologia) {
|
|
$query->whereHas('territorio', fn($q) => $q->where('tipologia_id', $this->filtroTipologia));
|
|
}
|
|
|
|
// Re-check here too: sortBy() guards the action, but the property can
|
|
// also be set directly in a Livewire update payload.
|
|
$sortField = in_array($this->sortField, self::SORTABLE_FIELDS, true)
|
|
? $this->sortField
|
|
: 'territorio_numero';
|
|
$dir = $this->sortDirection === 'asc' ? 'ASC' : 'DESC';
|
|
|
|
if ($sortField === 'territorio_numero') {
|
|
$query->orderByRaw(
|
|
"CAST((SELECT numero FROM territori WHERE territori.id = assegnazioni.territorio_id) AS UNSIGNED) $dir"
|
|
);
|
|
} else {
|
|
$query->orderBy($sortField, $dir);
|
|
}
|
|
|
|
// In-memory search for encrypted proclamatore fields / territorio numero
|
|
if ($this->search !== '') {
|
|
$all = $query->get();
|
|
$filtered = $all->filter(function ($a) {
|
|
$haystack = mb_strtolower(
|
|
($a->territorio?->numero ?? '') . ' ' .
|
|
($a->proclamatore?->nome ?? '') . ' ' .
|
|
($a->proclamatore?->cognome ?? '')
|
|
);
|
|
return str_contains($haystack, mb_strtolower($this->search));
|
|
});
|
|
|
|
$page = $this->getPage();
|
|
$perPage = 25;
|
|
$items = $filtered->slice(($page - 1) * $perPage, $perPage)->values();
|
|
$assegnazioni = new \Illuminate\Pagination\LengthAwarePaginator(
|
|
$items, $filtered->count(), $perPage, $page,
|
|
['path' => request()->url()]
|
|
);
|
|
} else {
|
|
$assegnazioni = $query->paginate(25);
|
|
}
|
|
|
|
return view('livewire.registro', [
|
|
'assegnazioni' => $assegnazioni,
|
|
'anni' => AnnoTeocratico::orderByDesc('start_date')->get(),
|
|
'zone' => Zona::attive()->orderBy('nome')->get(),
|
|
'tipologie' => Tipologia::orderBy('nome')->get(),
|
|
'territori' => Territorio::attivi()->orderBy('numero')->get(),
|
|
'proclamatori' => Proclamatore::attivi()->orderBy('cognome')->orderBy('nome')->get(),
|
|
'campagne' => Campagna::orderByDesc('created_at')->get(),
|
|
]);
|
|
}
|
|
}
|