feat: implement super admin role with spatie rbac and categorize sidebar by management

This commit is contained in:
Ray1111-aa
2026-10-15 15:03:24 +07:00
parent 9c00515a73
commit 2e1f05b494
29 changed files with 5334 additions and 144 deletions
+27 -6
View File
@@ -9,11 +9,27 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
use HasFactory, Notifiable, HasRoles;
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::created(function (User $user) {
if ($user->role && \Illuminate\Support\Facades\Schema::hasTable('roles')) {
$role = \Spatie\Permission\Models\Role::where('name', $user->role)->first();
if ($role && !$user->hasRole($user->role)) {
$user->assignRole($role);
}
}
});
}
/**
* The attributes that are mass assignable.
@@ -62,28 +78,33 @@ class User extends Authenticatable
return $this->hasOne(SecurityGuard::class);
}
public function isSuperAdmin(): bool
{
return $this->role === 'super_admin' || $this->hasRole('super_admin');
}
public function isGateOfficer(): bool
{
return $this->role === 'gate_officer';
return $this->role === 'gate_officer' || $this->hasRole('gate_officer');
}
public function isHseAdmin(): bool
{
return $this->role === 'hse_admin';
return $this->isSuperAdmin() || $this->role === 'hse_admin' || $this->hasRole('hse_admin');
}
public function isQmaAdmin(): bool
{
return $this->role === 'qma_admin';
return $this->isSuperAdmin() || $this->role === 'qma_admin' || $this->hasRole('qma_admin');
}
public function isPgAdmin(): bool
{
return $this->role === 'pg_admin';
return $this->isSuperAdmin() || $this->role === 'pg_admin' || $this->hasRole('pg_admin');
}
public function isAdmin(): bool
{
return $this->role === 'admin';
return $this->isSuperAdmin() || $this->role === 'admin' || $this->hasRole('admin');
}
}