sistemazione reports - cambio password - profilo utente

This commit is contained in:
2026-05-27 10:45:05 +02:00
parent fefc3bba2a
commit 3471befb1a
26 changed files with 1964 additions and 632 deletions
+68 -2
View File
@@ -7,14 +7,22 @@ use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
#[Fillable(['name', 'email', 'password', 'tenant_id', 'is_admin', 'permissions', 'role_preset_id', 'status'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
class User extends Authenticatable implements CanResetPasswordContract
{
use HasFactory, Notifiable;
use HasFactory, Notifiable, CanResetPassword;
protected $fillable = ['name', 'email', 'password', 'tenant_id', 'is_admin', 'permissions', 'role_preset_id', 'status'];
@@ -156,4 +164,62 @@ class User extends Authenticatable
$this->permissions = $preset->permissions;
return $this;
}
public function sendPasswordResetNotification($token): void
{
$settings = EmailSetting::getActive();
if (!$settings || empty($settings->smtp_host)) {
Log::warning('Impossibile inviare reset password: SMTP non configurato');
return;
}
try {
$smtpUsername = $settings->smtp_username ?: $settings->email_address;
$smtpPassword = $settings->smtp_password
? Crypt::decryptString($settings->smtp_password)
: $settings->getDecryptedPassword();
$encryption = $settings->smtp_encryption ?? 'tls';
$scheme = ($encryption === 'ssl') ? 'smtps' : 'smtp';
$dsn = sprintf(
'%s://%s:%s@%s:%d?encryption=%s',
$scheme,
rawurlencode($smtpUsername),
rawurlencode($smtpPassword),
$settings->smtp_host,
$settings->smtp_port ?? 587,
$encryption
);
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
$resetUrl = url('/password/reset/' . $token);
$fromName = $settings->email_name ?? config('app.name');
$fromName = preg_replace('/[^\p{L}\p{N}\s]/u', '', $fromName);
$fromName = trim($fromName) ?: config('app.name');
$email = (new Email())
->from(new Address($settings->email_address, $fromName))
->to($this->email)
->subject('Reset della password - ' . config('app.name'))
->html(view('auth.emails.reset-password', [
'user' => $this,
'token' => $token,
'resetUrl' => $resetUrl,
'appName' => config('app.name'),
])->render());
$mailer->send($email);
Log::info('Password reset email sent', ['email' => $this->email]);
} catch (\Exception $e) {
Log::error('Failed to send password reset email', [
'email' => $this->email,
'error' => $e->getMessage(),
]);
}
}
}