‪TYPO3CMS  11.5
UserToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 use TYPO3\CMS\Core\Page\PageRenderer;
26 
33 {
39  public function ‪checkAccess()
40  {
41  return true;
42  }
43 
49  public function ‪getItem()
50  {
51  $backendUser = $this->‪getBackendUser();
52  $view = $this->‪getFluidTemplateObject('UserToolbarItem.html');
53  $view->assignMultiple([
54  'currentUser' => $backendUser->user,
55  'switchUserMode' => (int)$backendUser->getOriginalUserIdWhenInSwitchUserMode(),
56  ]);
57  return $view->render();
58  }
59 
65  public function ‪getDropDown()
66  {
67  $backendUser = $this->‪getBackendUser();
68 
69  $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
70  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
71 
72  $mostRecentUsers = [];
73  if ($backendUser->isAdmin()
74  && $backendUser->getOriginalUserIdWhenInSwitchUserMode() === null
75  && isset($backendUser->uc['recentSwitchedToUsers'])
76  && is_array($backendUser->uc['recentSwitchedToUsers'])
77  ) {
78  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
79  $result = $queryBuilder
80  ->select('uid', 'username', 'realName')
81  ->from('be_users')
82  ->where(
83  $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], Connection::PARAM_INT_ARRAY))
84  )->executeQuery();
85 
86  // Flip the array to have a "sorted" list of items
87  $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
88 
89  while ($row = $result->fetchAssociative()) {
90  $mostRecentUsers[$row['uid']] = $row;
91  }
92 
93  // Remove any item that is not an array (means, the stored uid is not available anymore)
94  $mostRecentUsers = array_filter($mostRecentUsers, static function ($record) {
95  return is_array($record);
96  });
97 
98  $availableUsers = array_keys($mostRecentUsers);
99  if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
100  $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
101  $backendUser->writeUC();
102  }
103  }
104 
105  GeneralUtility::makeInstance(PageRenderer::class)
106  ->loadRequireJsModule('TYPO3/CMS/Backend/SwitchUser');
107 
108  $modules = null;
109  if ($userModule = $backendModuleRepository->findByModuleName('user')) {
110  $modules = $userModule->getChildren();
111  }
112  $view = $this->‪getFluidTemplateObject('UserToolbarItemDropDown.html');
113  $view->assignMultiple([
114  'modules' => $modules,
115  'logoutUrl' => (string)$uriBuilder->buildUriFromRoute('logout'),
116  'switchUserMode' => $this->getBackendUser()->getOriginalUserIdWhenInSwitchUserMode() !== null,
117  'recentUsers' => $mostRecentUsers,
118  ]);
119  return $view->render();
120  }
121 
127  public function ‪getAdditionalAttributes()
128  {
129  $result = [
130  'class' => 'toolbar-item-user',
131  ];
132  if ($this->‪getBackendUser()->getOriginalUserIdWhenInSwitchUserMode()) {
133  $result['class'] .= ' su-user';
134  }
135  return $result;
136  }
137 
143  public function ‪hasDropDown()
144  {
145  return true;
146  }
147 
153  public function ‪getIndex()
154  {
155  return 80;
156  }
157 
163  protected function ‪getBackendUser()
164  {
165  return ‪$GLOBALS['BE_USER'];
166  }
167 
175  protected function ‪getFluidTemplateObject(string $filename): ‪StandaloneView
176  {
177  $view = GeneralUtility::makeInstance(StandaloneView::class);
178  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
179  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
180  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
181 
182  $view->setTemplate($filename);
183 
184  $view->getRequest()->setControllerExtensionName('Backend');
185  return $view;
186  }
187 }
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getItem
‪string getItem()
Definition: UserToolbarItem.php:49
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem
Definition: UserToolbarItem.php:33
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\hasDropDown
‪bool hasDropDown()
Definition: UserToolbarItem.php:143
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\checkAccess
‪bool checkAccess()
Definition: UserToolbarItem.php:39
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:16
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename)
Definition: UserToolbarItem.php:175
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: UserToolbarItem.php:163
‪TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository
Definition: BackendModuleRepository.php:34
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getDropDown
‪string getDropDown()
Definition: UserToolbarItem.php:65
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getAdditionalAttributes
‪array getAdditionalAttributes()
Definition: UserToolbarItem.php:127
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getIndex
‪int getIndex()
Definition: UserToolbarItem.php:153