First commit
This commit is contained in:
94
app/Models/User.php
Normal file
94
app/Models/User.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?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',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user