Files
ilycats/app/Models/User.php
T
2026-02-08 15:16:35 +07:00

77 lines
1.9 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
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasPermissions; // Tambahan: Gunakan HasPermissions
/**
* 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',
];
/**
* 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',
];
}
/**
* Tambahan: Relasi ke tabel Roles
* Setiap user memiliki satu Role.
*/
public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}
public function hasAccess($module, $permission)
{
// Jika admin, berikan akses penuh secara otomatis
if ($this->role && $this->role->slug === '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();
}
}