104 lines
3.1 KiB
PHP
Executable File
104 lines
3.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// Import necessari
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Lab404\Impersonate\Models\Impersonate; // Per la funzionalità di impersonificazione
|
|
use Spatie\Permission\Traits\HasRoles; // Per la gestione dei ruoli e permessi
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
// Aggiungi il trait Impersonate
|
|
use HasApiTokens, HasFactory, Notifiable, Impersonate, HasRoles; // Aggiungi HasRoles qui
|
|
|
|
|
|
protected $fillable = ['name', 'email', 'password', 'email_verified_at', 'expires_at']; // Aggiunto expires_at
|
|
protected $hidden = ['password', 'remember_token'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'expires_at' => 'datetime',
|
|
'password' => 'hashed'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Ritorna true se l'utente è scaduto.
|
|
*/
|
|
public function isExpired(): bool
|
|
{
|
|
return $this->expires_at !== null && now()->greaterThan($this->expires_at);
|
|
}
|
|
|
|
/**
|
|
* Get the amministratore record associated with the user.
|
|
*/
|
|
public function amministratore(): HasOne
|
|
{
|
|
return $this->hasOne(Amministratore::class, 'user_id');
|
|
}
|
|
|
|
public function soggetto(): HasOne
|
|
{
|
|
return $this->hasOne(Soggetto::class, 'email', 'email');
|
|
}
|
|
|
|
public function tickets(): HasMany
|
|
{
|
|
return $this->hasMany(Ticket::class, 'aperto_da_user_id');
|
|
}
|
|
|
|
/**
|
|
* Stabili assegnati al collaboratore tramite pivot
|
|
*/
|
|
public function stabiliAssegnati(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Stabile::class, 'collaboratore_stabile', 'user_id', 'stabile_id')
|
|
->withPivot(['ruolo', 'permessi'])
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Regola per il pacchetto: definisce se l'utente ATTUALE
|
|
* ha il permesso di impersonare altri.
|
|
*/
|
|
public function canImpersonate(): bool
|
|
|
|
{
|
|
// Solo il Super-Admin può farlo.
|
|
return $this->hasRole('super-admin');
|
|
}
|
|
|
|
/**
|
|
* Regola per il pacchetto: definisce se questo specifico
|
|
* utente PUÒ ESSERE impersonato.
|
|
*/
|
|
public function canBeImpersonated(): bool
|
|
{
|
|
// Possono essere impersonati gli utenti operativi (amministratore/collaboratore/admin)
|
|
return $this->hasAnyRole(['admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
// public function roles()
|
|
// {
|
|
// return $this->belongsToMany(\App\Models\Role::class, 'role_user')->withTimestamps();
|
|
// // Relazione legacy rimossa: ora i ruoli sono gestiti solo tramite Spatie/Permission
|
|
// }
|
|
// public function hasRole($role, $stabileId = null)
|
|
// {
|
|
// return $this->roles()->where('name', $role)
|
|
// ->when($stabileId, fn($q) => $q->wherePivot('stabile_id', $stabileId))
|
|
// ->exists();
|
|
// }
|
|
}
|