95 lines
1.9 KiB
PHP
95 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory, Notifiable;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'role',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
// Role constants
|
|
const ROLE_ADMIN = 'admin';
|
|
const ROLE_STAFF = 'staff';
|
|
const ROLE_VIEWER = 'viewer';
|
|
|
|
// === Role Checks ===
|
|
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->role === self::ROLE_ADMIN;
|
|
}
|
|
|
|
public function isStaff(): bool
|
|
{
|
|
return in_array($this->role, [self::ROLE_ADMIN, self::ROLE_STAFF]);
|
|
}
|
|
|
|
public function hasRole(string $role): bool
|
|
{
|
|
return $this->role === $role;
|
|
}
|
|
|
|
public function canManageDocuments(): bool
|
|
{
|
|
return $this->isStaff();
|
|
}
|
|
|
|
public function canManageCategories(): bool
|
|
{
|
|
return $this->isAdmin();
|
|
}
|
|
|
|
public function canViewAuditLogs(): bool
|
|
{
|
|
return $this->isAdmin();
|
|
}
|
|
|
|
// === Relationships ===
|
|
|
|
public function auditLogs(): HasMany
|
|
{
|
|
return $this->hasMany(AuditLog::class);
|
|
}
|
|
|
|
public function chatLogs(): HasMany
|
|
{
|
|
return $this->hasMany(ChatLog::class);
|
|
}
|
|
|
|
// === Accessor ===
|
|
|
|
public function getRoleLabelAttribute(): string
|
|
{
|
|
return match ($this->role) {
|
|
self::ROLE_ADMIN => 'Admin Sistem',
|
|
self::ROLE_STAFF => 'Kakitangan',
|
|
self::ROLE_VIEWER => 'Pengguna',
|
|
default => 'Tidak Diketahui',
|
|
};
|
|
}
|
|
}
|