From 7d5b706dffa153f6f904d65e120d5e2534f90252 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Fri, 24 Nov 2017 23:35:08 +0100
Subject: [PATCH] [BUGFIX] Only update fe_users.is_online if user is logged in
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The field is_online is updated in TSFE right after a user has authenticated,
but if the user has no usergroup, he/she is not logged in.

The update should be moved in the TSFE->fe_user and the check
should only be called when a logged-in user is found.

Resolves: #83086
Releases: master
Change-Id: I14b31662d99772b29e0e37153d44925b4309d69c
Reviewed-on: https://review.typo3.org/54756
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Łukasz Uznański <l.uznanski@macopedia.pl>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
---
 .../FrontendUserAuthentication.php            | 21 +++++++++++++++++++
 .../TypoScriptFrontendController.php          | 20 ++++++------------
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/typo3/sysext/frontend/Classes/Authentication/FrontendUserAuthentication.php b/typo3/sysext/frontend/Classes/Authentication/FrontendUserAuthentication.php
index a98fdb3e6ac2..044b1771f0f7 100644
--- a/typo3/sysext/frontend/Classes/Authentication/FrontendUserAuthentication.php
+++ b/typo3/sysext/frontend/Classes/Authentication/FrontendUserAuthentication.php
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Frontend\Authentication;
  */
 
 use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Session\Backend\Exception\SessionNotFoundException;
 use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -600,4 +601,24 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
         $this->user = null;
         $this->loginHidden = true;
     }
+
+    /**
+     * Update the field "is_online" every 60 seconds of a logged-in user
+     *
+     * @internal
+     */
+    public function updateOnlineTimestamp()
+    {
+        if (!is_array($this->user) || !$this->user['uid']
+            || $this->user['is_online'] >= $GLOBALS['EXEC_TIME'] - 60) {
+            return;
+        }
+        $dbConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_users');
+        $dbConnection->update(
+            'fe_users',
+            ['is_online' => $GLOBALS['EXEC_TIME']],
+            ['uid' => (int)$this->user['uid']]
+        );
+        $this->user['is_online'] = $GLOBALS['EXEC_TIME'];
+    }
 }
diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 732992aee7c2..c76840693de3 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -911,20 +911,6 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['initFEuser'] ?? [] as $_funcRef) {
             GeneralUtility::callUserFunction($_funcRef, $_params, $this);
         }
-        // For every 60 seconds the is_online timestamp is updated.
-        if (is_array($this->fe_user->user) && $this->fe_user->user['uid'] && $this->fe_user->user['is_online'] < $GLOBALS['EXEC_TIME'] - 60) {
-            $dbConnection = GeneralUtility::makeInstance(ConnectionPool::class)
-                ->getConnectionForTable('fe_users');
-            $dbConnection->update(
-                'fe_users',
-                [
-                    'is_online' => $GLOBALS['EXEC_TIME']
-                ],
-                [
-                    'uid' => (int)$this->fe_user->user['uid']
-                ]
-            );
-        }
     }
 
     /**
@@ -963,6 +949,12 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         if (!empty($gr_array) && !$this->loginAllowedInBranch_mode) {
             $this->gr_list .= ',' . implode(',', $gr_array);
         }
+
+        // For every 60 seconds the is_online timestamp for a logged-in user is updated
+        if ($this->loginUser) {
+            $this->fe_user->updateOnlineTimestamp();
+        }
+
         $this->logger->debug('Valid usergroups for TSFE: ' . $this->gr_list);
     }
 
-- 
GitLab