80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
use App\Traits\BelongsToStructure;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory, Notifiable, BelongsToStructure;
|
|
use HasRoles {
|
|
hasRole as traitHasRole;
|
|
}
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'structure_id',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Override de Spatie HasRoles pour qu'un SuperAdmin valide toutes les vérifications de rôle
|
|
* Cela permet notamment de parcourir les locataires (Tenant) sans être bloqué par les "hasRole('Admin')"
|
|
*/
|
|
public function hasRole($roles, string $guard = null): bool
|
|
{
|
|
// Si on ne demande pas explicitement le rôle SuperAdmin, on vérifie si l'utilisateur l'a globalement.
|
|
// On passe par DB::table pour éviter que le GlobalScope 'structure' ne filtre nos propres rôles
|
|
// lorsqu'on est en train de simuler une autre structure.
|
|
if ($roles !== 'SuperAdmin') {
|
|
$isSuperAdmin = \Illuminate\Support\Facades\DB::table('model_has_roles')
|
|
->join('roles', 'roles.id', '=', 'model_has_roles.role_id')
|
|
->where('model_has_roles.model_id', $this->id)
|
|
->where('model_has_roles.model_type', self::class)
|
|
->where('roles.name', 'SuperAdmin')
|
|
->exists();
|
|
|
|
if ($isSuperAdmin) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return $this->traitHasRole($roles, $guard);
|
|
}
|
|
}
|