set('user', $user); $this->regenerate(); } public function getUser(): ?array { return $this->get('user'); } public function isLoggedIn(): bool { return $this->has('user'); } public function logout(): void { $this->remove('user'); $this->destroy(); } public function getUserId(): ?int { $user = $this->getUser(); return $user['id'] ?? null; } public function getUserRole(): ?string { $user = $this->getUser(); return $user['role'] ?? null; } public function isAdmin(): bool { return $this->getUserRole() === 'admin'; } public function isAuditor(): bool { return $this->getUserRole() === 'auditor'; } public function isEmployee(): bool { return $this->getUserRole() === 'employee'; } public function generateCsrfToken(): string { $token = bin2hex(random_bytes(32)); $this->set('csrf_token', $token); $this->set('csrf_token_time', time()); return $token; } public function validateCsrfToken(string $token): bool { $storedToken = $this->get('csrf_token'); $tokenTime = $this->get('csrf_token_time', 0); if (!$storedToken || !$tokenTime) { return false; } // Check if token is expired (1 hour) if (time() - $tokenTime > CSRF_TOKEN_LIFETIME) { $this->remove('csrf_token'); $this->remove('csrf_token_time'); return false; } return hash_equals($storedToken, $token); } public function getCsrfToken(): string { $token = $this->get('csrf_token'); if (!$token) { $token = $this->generateCsrfToken(); } return $token; } public function setLocale(string $locale): void { $this->set('locale', $locale); } public function getLocale(): string { return $this->get('locale', 'de'); } public function setLastActivity(): void { $this->set('last_activity', time()); } public function isExpired(int $timeout = 3600): bool { $lastActivity = $this->get('last_activity', 0); return (time() - $lastActivity) > $timeout; } public function setLoginAttempts(int $attempts): void { $this->set('login_attempts', $attempts); $this->set('login_attempts_time', time()); } public function getLoginAttempts(): int { $attempts = $this->get('login_attempts', 0); $attemptsTime = $this->get('login_attempts_time', 0); // Reset attempts if lockout time has passed if (time() - $attemptsTime > LOGIN_LOCKOUT_TIME) { $this->setLoginAttempts(0); return 0; } return $attempts; } public function isLockedOut(): bool { return $this->getLoginAttempts() >= LOGIN_MAX_ATTEMPTS; } }