*/ use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'username', 'email', 'password', 'role', 'gate_id', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } protected static function booted(): void { static::deleting(function (User $user) { if ($user->employee) { if ($user->employee->activities()->exists() || $user->employee->p2hRecords()->exists()) { throw new \Exception("Tidak dapat menghapus user: Karyawan '{$user->name}' memiliki data audit riwayat operasional kendaraan atau inspeksi P2H."); } $user->employee->delete(); } if ($user->securityGuard) { if ($user->securityGuard->activities()->exists()) { throw new \Exception("Tidak dapat menghapus user: Petugas Satpam '{$user->name}' memiliki riwayat data validasi pos gerbang."); } $user->securityGuard->delete(); } }); } public function gate(): BelongsTo { return $this->belongsTo(Gate::class); } public function securityGuard(): HasOne { return $this->hasOne(SecurityGuard::class); } public function employee(): HasOne { return $this->hasOne(Employee::class); } public function isEmployee(): bool { return $this->role === 'employee'; } public function isGateOfficer(): bool { return $this->role === 'gate_officer'; } public function isHseAdmin(): bool { return $this->role === 'hse_admin'; } public function isQmaAdmin(): bool { return $this->role === 'qma_admin'; } public function isPgAdmin(): bool { return $this->role === 'pg_admin'; } public function isSuperAdmin(): bool { return in_array($this->role, ['super_admin', 'superadmin']); } public function isAdmin(): bool { return in_array($this->role, ['admin', 'super_admin', 'superadmin']); } }