55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Actions\PurgeUserAction;
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
|
|
/**
|
|
* Anonimizza (GDPR) gli utenti soft-deleted oltre la retention, riassegnandone
|
|
* i contenuti all'utente di sistema. Idempotente, pensato per esecuzione
|
|
* schedulata giornaliera (vedi routes/console.php).
|
|
*/
|
|
class PurgeAnonymizableUsers extends Command
|
|
{
|
|
protected $signature = 'users:purge
|
|
{--days=30 : Days kept in the trash before anonymization}
|
|
{--dry-run : Show the affected users without anonymizing}';
|
|
|
|
protected $description = 'Anonymize users deleted beyond the retention window (GDPR right to be forgotten)';
|
|
|
|
public function handle(PurgeUserAction $purge): int
|
|
{
|
|
$days = (int) $this->option('days');
|
|
$dryRun = (bool) $this->option('dry-run');
|
|
|
|
$users = User::onlyTrashed()
|
|
->where('is_system', false)
|
|
->whereNull('anonymized_at')
|
|
->where('deleted_at', '<=', now()->subDays($days))
|
|
->get();
|
|
|
|
if ($users->isEmpty()) {
|
|
$this->info('No users to anonymize.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
foreach ($users as $user) {
|
|
if ($dryRun) {
|
|
$this->line(sprintf(' [dry-run] #%d %s', $user->id, $user->email));
|
|
|
|
continue;
|
|
}
|
|
|
|
$purge->execute($user);
|
|
$this->line(sprintf(' ✓ anonymized user #%d', $user->id));
|
|
}
|
|
|
|
$this->info(($dryRun ? '[dry-run] ' : '').'Processed users: '.$users->count());
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|