66 lines
1.8 KiB
PHP
66 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Database\Factories\UserFactory;
|
|
use Filament\Models\Contracts\FilamentUser;
|
|
use Filament\Panel;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
#[Fillable(['name', 'username', 'email', 'mobile', 'password'])]
|
|
#[Hidden(['password', 'remember_token'])]
|
|
class User extends Authenticatable implements FilamentUser
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasApiTokens, HasFactory, Notifiable;
|
|
|
|
/**
|
|
* @return BelongsToMany<Role, $this>
|
|
*/
|
|
public function roles(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Role::class);
|
|
}
|
|
|
|
public function hasRole(string|array $roles): bool
|
|
{
|
|
$roles = (array) $roles;
|
|
|
|
return $this->roles()->whereIn('slug', $roles)->exists();
|
|
}
|
|
|
|
public function hasPermission(string|array $permissions): bool
|
|
{
|
|
$permissions = (array) $permissions;
|
|
|
|
return $this->roles()
|
|
->whereHas('permissions', fn ($query) => $query->whereIn('slug', $permissions))
|
|
->exists();
|
|
}
|
|
|
|
public function canAccessPanel(Panel $panel): bool
|
|
{
|
|
return $this->hasRole('admin') || $this->hasPermission('admin.access');
|
|
}
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
}
|