2026-02-08 15:16:35 +07:00
|
|
|
<?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
|
2026-09-23 12:45:02 +07:00
|
|
|
use Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable; // Tambahkan ini
|
|
|
|
|
use Laragear\WebAuthn\WebAuthnAuthentication;
|
2026-02-08 15:16:35 +07:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
|
|
|
use HasFactory, Notifiable, HasPermissions; // Tambahan: Gunakan HasPermissions
|
|
|
|
|
|
2026-09-23 12:45:02 +07:00
|
|
|
use WebAuthnAuthentication;
|
|
|
|
|
|
2026-02-08 15:16:35 +07:00
|
|
|
/**
|
|
|
|
|
* 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',
|
2026-09-23 13:56:51 +07:00
|
|
|
'is_active',
|
|
|
|
|
'deactivated_at',
|
2026-02-08 15:16:35 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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',
|
2026-09-23 13:56:51 +07:00
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'deactivated_at' => 'datetime',
|
2026-02-08 15:16:35 +07:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tambahan: Relasi ke tabel Roles
|
|
|
|
|
* Setiap user memiliki satu Role.
|
|
|
|
|
*/
|
2026-09-23 12:45:02 +07:00
|
|
|
/**
|
|
|
|
|
* Relasi role memakai nama roleModel agar tidak bertabrakan dengan kolom legacy `role`.
|
|
|
|
|
*/
|
|
|
|
|
public function roleModel(): BelongsTo
|
2026-02-08 15:16:35 +07:00
|
|
|
{
|
2026-09-23 12:45:02 +07:00
|
|
|
return $this->belongsTo(Role::class, 'role_id');
|
2026-02-08 15:16:35 +07:00
|
|
|
}
|
|
|
|
|
|
2026-09-23 12:45:02 +07:00
|
|
|
public function hasAccess($module, $permission): bool
|
|
|
|
|
{
|
|
|
|
|
$role = $this->roleModel;
|
|
|
|
|
if ($role?->slug === 'admin' || $this->getRawOriginal('role') === 'admin') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-08 15:16:35 +07:00
|
|
|
|
2026-09-23 12:45:02 +07:00
|
|
|
return $role?->permissions()
|
|
|
|
|
->where('module_slug', $module)
|
|
|
|
|
->where($permission, 1)
|
|
|
|
|
->exists() ?? false;
|
|
|
|
|
}
|
2026-02-08 15:16:35 +07:00
|
|
|
|
|
|
|
|
}
|