auth tests
This commit is contained in:
57
backend/app/Models/Lists/UserRole.php
Normal file
57
backend/app/Models/Lists/UserRole.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Lists;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use OwenIt\Auditing\Auditable as AuditableTrait;
|
||||
use OwenIt\Auditing\Contracts\Auditable;
|
||||
|
||||
class UserRole extends Model implements Auditable
|
||||
{
|
||||
use AuditableTrait;
|
||||
use HasFactory;
|
||||
|
||||
public const ADMIN = 'Admin';
|
||||
|
||||
public const SUPERVISOR = 'Supervisor';
|
||||
|
||||
public const USER = 'User';
|
||||
|
||||
public const GUEST = 'Guest';
|
||||
|
||||
protected $table = 'user_roles';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* Utenti che hanno questo ruolo.
|
||||
*
|
||||
* @return HasMany<User, $this>
|
||||
*/
|
||||
public function users(): HasMany
|
||||
{
|
||||
return $this->hasMany(User::class, 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ruolo predefinito di sistema: non eliminabile.
|
||||
*/
|
||||
public function isSystemRole(): bool
|
||||
{
|
||||
return in_array($this->name, [self::ADMIN, self::SUPERVISOR, self::USER, self::GUEST], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Il ruolo è assegnato ad almeno un utente.
|
||||
*/
|
||||
public function isInUse(): bool
|
||||
{
|
||||
return $this->users()->exists();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user