Files

87 lines
2.3 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Traits\HasPermissions; // Tambahan: Import Trait Permission
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\BelongsTo; // Tambahan: Import untuk relasi
use Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable; // Tambahkan ini
use Laragear\WebAuthn\WebAuthnAuthentication;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasPermissions; // Tambahan: Gunakan HasPermissions
use WebAuthnAuthentication;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
'role', // Kode existing Anda
'role_id', // Tambahan: ID untuk relasi tabel Role
'phone',
'is_active',
'deactivated_at',
];
/**
* 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',
'is_active' => 'boolean',
'deactivated_at' => 'datetime',
];
}
/**
* Tambahan: Relasi ke tabel Roles
* Setiap user memiliki satu Role.
*/
/**
* Relasi role memakai nama roleModel agar tidak bertabrakan dengan kolom legacy `role`.
*/
public function roleModel(): BelongsTo
{
return $this->belongsTo(Role::class, 'role_id');
}
public function hasAccess($module, $permission): bool
{
$role = $this->roleModel;
if ($role?->slug === 'admin' || $this->getRawOriginal('role') === 'admin') {
return true;
}
return $role?->permissions()
->where('module_slug', $module)
->where($permission, 1)
->exists() ?? false;
}
}