Files
2026-06-24 17:02:11 +02:00

121 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\Lists\UserRole;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Contracts\Auditable;
class User extends Authenticatable implements Auditable, MustVerifyEmail
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
use \OwenIt\Auditing\Auditable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $appends = ['setup_status'];
protected $auditExclude = ['password', 'remember_token', 'two_factor_secret'];
protected $fillable = [
'name',
'email',
'password',
'role_id',
'is_system',
'must_change_password',
'two_factor_secret',
'two_factor_recovery_codes',
'two_factor_setup_completed_at',
'email_verified_at',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_secret',
'two_factor_recovery_codes',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'role_id' => 'integer',
'must_change_password' => 'boolean',
'two_factor_setup_completed_at' => 'datetime',
'two_factor_confirmed_at' => 'datetime',
'is_system' => 'boolean',
'anonymized_at' => 'datetime',
'disabled_at' => 'datetime',
];
}
/**
* Ruolo applicativo dell'utente.
*
* @return BelongsTo<UserRole, $this>
*/
public function role(): BelongsTo
{
return $this->belongsTo(UserRole::class, 'role_id');
}
/**
* Storico delle affiliazioni (incarichi) di questo utente.
*
* @return HasMany<UserAffiliation, $this>
*/
public function affiliations(): HasMany
{
return $this->hasMany(UserAffiliation::class);
}
/**
* Send the password reset notification.
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
public function getSetupStatusAttribute(): string
{
if ($this->must_change_password) {
return 'password_required';
}
if (is_null($this->two_factor_setup_completed_at)) {
return '2fa_setup_required';
}
return 'complete';
}
}