initial commit
This commit is contained in:
+20
-14
@@ -8,12 +8,16 @@ 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.
|
||||
*
|
||||
@@ -55,23 +59,25 @@ class User extends Authenticatable
|
||||
* Tambahan: Relasi ke tabel Roles
|
||||
* Setiap user memiliki satu Role.
|
||||
*/
|
||||
public function role(): BelongsTo
|
||||
/**
|
||||
* Relasi role memakai nama roleModel agar tidak bertabrakan dengan kolom legacy `role`.
|
||||
*/
|
||||
public function roleModel(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Role::class);
|
||||
return $this->belongsTo(Role::class, 'role_id');
|
||||
}
|
||||
|
||||
public function hasAccess($module, $permission)
|
||||
{
|
||||
// Jika admin, berikan akses penuh secara otomatis
|
||||
if ($this->role && $this->role->slug === 'admin') {
|
||||
return true;
|
||||
}
|
||||
public function hasAccess($module, $permission): bool
|
||||
{
|
||||
$role = $this->roleModel;
|
||||
if ($role?->slug === 'admin' || $this->getRawOriginal('role') === 'admin') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cek di tabel role_permissions melalui relasi role
|
||||
return $this->role->permissions()
|
||||
->where('module_slug', $module)
|
||||
->where($permission, 1) // $permission berisi 'can_read', 'can_update', dll
|
||||
->exists();
|
||||
}
|
||||
return $role?->permissions()
|
||||
->where('module_slug', $module)
|
||||
->where($permission, 1)
|
||||
->exists() ?? false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user