Files
laravel-gost-template/app/Console/Commands/AuditPurgeCommand.php
2026-06-24 17:20:43 +09:00

40 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Models\AuditLog;
use Illuminate\Console\Command;
/**
* Удаление устаревших записей журнала по сроку хранения (мера РСБ.4, РСБ.8).
*
* Срок хранения задаётся AUDIT_RETENTION_DAYS (для УЗ-1 — не менее 3 лет).
* Перед удалением записи должны быть выгружены в архив/SIEM.
*/
class AuditPurgeCommand extends Command
{
protected $signature = 'audit:purge {--dry-run : Показать количество без удаления}';
protected $description = 'Удалить записи журнала аудита старше срока хранения';
public function handle(): int
{
$days = (int) config('audit.retention_days');
$threshold = now()->subDays($days);
$query = AuditLog::query()->where('created_at', '<', $threshold);
$count = $query->count();
if ($this->option('dry-run')) {
$this->info("К удалению (старше {$days} дн.): {$count} записей.");
return self::SUCCESS;
}
$query->delete();
$this->info("Удалено записей журнала: {$count}.");
return self::SUCCESS;
}
}