62 lines
2.1 KiB
PHP
62 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use App\Notifications\NetGesconResetPasswordNotification;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Password;
|
|
|
|
class SendPasswordResetEmailCommand extends Command
|
|
{
|
|
protected $signature = 'netgescon:send-password-reset {email : Indirizzo email dell\'utente}';
|
|
|
|
protected $description = 'Invia l\'email con il link di reimpostazione password a un utente registrato su NetGescon';
|
|
|
|
public function handle(): int
|
|
{
|
|
$email = trim((string) $this->argument('email'));
|
|
|
|
$this->info("Verifica esistenza utente per email: {$email}...");
|
|
|
|
$user = User::query()->where('email', $email)->first();
|
|
|
|
if (! $user instanceof User) {
|
|
$this->error("Errore: nessun utente trovato con l'indirizzo email {$email}.");
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->info("Utente trovato: {$user->name} (ID: {$user->id}, Ruoli: " . $user->roles->pluck('name')->join(', ') . ")");
|
|
|
|
$broker = Password::broker(Filament::getAuthPasswordBroker());
|
|
$token = $broker->createToken($user);
|
|
|
|
$notification = app(NetGesconResetPasswordNotification::class, ['token' => $token]);
|
|
|
|
try {
|
|
$panel = Filament::getPanel('admin-filament');
|
|
$url = $panel->getResetPasswordUrl($token, $user);
|
|
} catch (\Throwable) {
|
|
$url = url(route('filament.admin-filament.auth.password-reset.reset', [
|
|
'token' => $token,
|
|
'email' => $user->getEmailForPasswordReset(),
|
|
], false));
|
|
}
|
|
|
|
$notification->url = $url;
|
|
|
|
$this->info("Invio email in corso a {$email} via SMTP (Google Workspace / NetGescon)...");
|
|
|
|
try {
|
|
$user->notify($notification);
|
|
$this->info("Email inviata con successo!");
|
|
$this->line("Link di ripristino generato: {$url}");
|
|
return self::SUCCESS;
|
|
} catch (\Throwable $e) {
|
|
$this->error("Errore durante l'invio dell'email: " . $e->getMessage());
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
}
|