public static function createService($configFile, DibiConnection $dbConnection, vBuilder\Security\User $userService) { $userConfig = new self($dbConnection, $userService->isLoggedIn() ? $userService->getId() : null, $configFile); $userService->addObserver($userService::EVENT_ON_LOGGED_IN, function ($userService) use($userConfig) { $userConfig->setUserId($userService->getId()); }); $userService->addObserver($userService::EVENT_ON_LOGGED_OUT, function ($userService) use($userConfig) { $userConfig->setUserId(NULL); }); return $userConfig; }
public function onLoggedIn(User $service) { // Only for real users (no PSK, etc...s) try { if (!$service->isInRole('user')) { return; } // If role does not exist } catch (Nette\InvalidStateException $e) { return; } $remoteAddr = $this->httpRequest->getRemoteAddress(); $uid = $service->getId(); // Sanity check if ($uid == NULL) { return; } $this->db->query('INSERT INTO %n', $this->tableName, '([userId], [time], [ip], [time2], [ip2]) VALUES(' . '%i', $uid, ', NOW(), %s', $remoteAddr, ', NULL, NULL) ' . 'ON DUPLICATE KEY UPDATE [time2] = [time], [ip2] = [ip], [time] = NOW(), [ip] = %s', $remoteAddr); }