'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(); // } }