[BUGFIX] Delete unavailable "recently switched to" users from UC 33/54433/7
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Wed, 18 Oct 2017 06:57:41 +0000 (08:57 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 19 Oct 2017 19:11:22 +0000 (21:11 +0200)
If a user who appears in the "Recently switched to" list became
unavailable for some reason, only the placeholder avatar was rendered,
giving a slight hint of life in the past.

Such users are now deleted from UC and do not appear in the list anymore.
Furthermore, the section label was slightly modified to be more
understandable.

For performance reasons, only one query is triggered now and sorting is
done based on the array of UIDs stored in UC.

Resolves: #82796
Related: #80581
Releases: master
Change-Id: Ieff8a779639f277de1c0cb7feaa0fe2b82bd97c5
Reviewed-on: https://review.typo3.org/54433
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Backend/ToolbarItems/UserToolbarItem.php
typo3/sysext/backend/Resources/Private/Language/locallang.xlf

index 0a4c44f..4b36257 100644 (file)
@@ -17,6 +17,8 @@ namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\Connection;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Fluid\View\StandaloneView;
@@ -71,15 +73,37 @@ class UserToolbarItem implements ToolbarItemInterface
             && isset($backendUser->uc['recentSwitchedToUsers'])
             && is_array($backendUser->uc['recentSwitchedToUsers'])
         ) {
-            foreach ($backendUser->uc['recentSwitchedToUsers'] as $userUid) {
-                $backendUserRecord = BackendUtility::getRecord('be_users', $userUid);
-                $backendUserRecord['switchUserLink'] = BackendUtility::getModuleUrl(
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
+            $result = $queryBuilder
+                ->select('uid', 'username', 'realName')
+                ->from('be_users')
+                ->where(
+                    $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], Connection::PARAM_INT_ARRAY))
+                )->execute();
+
+            // Flip the array to have a "sorted" list of items
+            $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
+
+            while ($row = $result->fetch()) {
+                $row['switchUserLink'] = BackendUtility::getModuleUrl(
                     'system_BeuserTxBeuser',
-                        [
-                            'SwitchUser' => $backendUserRecord['uid']
-                        ]
+                    [
+                        'SwitchUser' => $row['uid']
+                    ]
                 );
-                $mostRecentUsers[] = $backendUserRecord;
+
+                $mostRecentUsers[$row['uid']] = $row;
+            }
+
+            // Remove any item that is not an array (means, the stored uid is not available anymore)
+            $mostRecentUsers = array_filter($mostRecentUsers, function ($record) {
+                return is_array($record);
+            });
+
+            $availableUsers = array_keys($mostRecentUsers);
+            if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
+                $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
+                $backendUser->writeUC();
             }
         }
 
index a2d1f3e..415e735 100644 (file)
@@ -20,7 +20,7 @@
 Have a nice day.</source>
                        </trans-unit>
                        <trans-unit id="usermodule.su.list">
-                               <source>Recently 'switched to' users</source>
+                               <source>Recently switched to</source>
                        </trans-unit>
                        <trans-unit id="usermodule.su.tooltip">
                                <source>Switch to user %s</source>