Integrate Spatie Laravel Permission to replace the legacy role system. This includes: - Adding `spatie/laravel-permission` dependency. - Implementing `Role` and `User` model updates with `HasRoles` trait. - Adding migrations for permission and role tables. - Creating `RoleResource` and `UserResource` for Filament administration. - Adding a `RoleAndPermissionSeeder` for initial setup. - Updating `User` model helper methods to utilize role checks.
73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable, HasRoles;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'phone',
|
|
'role', // فیلد قدیمی - بعداً حذف میکنیم
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
// ─── Relationships ───────────────────────────────
|
|
|
|
public function wallet()
|
|
{
|
|
return $this->hasOne(Wallet::class);
|
|
}
|
|
|
|
// ─── Helper Methods (Role Checks) ────────────────
|
|
|
|
public function isSuperAdmin(): bool
|
|
{
|
|
return $this->hasRole('super_admin');
|
|
}
|
|
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->hasAnyRole(['super_admin', 'admin']);
|
|
}
|
|
|
|
public function isStaff(): bool
|
|
{
|
|
return $this->hasRole('staff');
|
|
}
|
|
|
|
public function isCustomer(): bool
|
|
{
|
|
return $this->hasRole('customer');
|
|
}
|
|
|
|
// ─── Filament Panel Access ───────────────────────
|
|
|
|
public function canAccessPanel(\Filament\Panel $panel): bool
|
|
{
|
|
// فقط super_admin, admin, staff میتوانند به پنل ادمین دسترسی داشته باشند
|
|
// customer فقط از API استفاده میکند
|
|
return $this->hasAnyRole(['super_admin', 'admin', 'staff']);
|
|
}
|
|
} |