‪TYPO3CMS  10.4
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 
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' => $backendUser->user['ses_backuserid'],
56  ]);
57  return $view->render();
58  }
59 
65  public function ‪getDropDown()
66  {
67  $backendUser = $this->‪getBackendUser();
68 
70  $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
71  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
72 
73  $mostRecentUsers = [];
75  && $backendUser->isAdmin()
76  && (int)$backendUser->user['ses_backuserid'] === 0
77  && isset($backendUser->uc['recentSwitchedToUsers'])
78  && is_array($backendUser->uc['recentSwitchedToUsers'])
79  ) {
80  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
81  $result = $queryBuilder
82  ->select('uid', 'username', 'realName')
83  ->from('be_users')
84  ->where(
85  $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], Connection::PARAM_INT_ARRAY))
86  )->execute();
87 
88  // Flip the array to have a "sorted" list of items
89  $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
90 
91  while ($row = $result->fetch()) {
92  $row['switchUserLink'] = (string)$uriBuilder->buildUriFromRoute(
93  'system_BeuserTxBeuser',
94  [
95  'SwitchUser' => $row['uid']
96  ]
97  );
98 
99  $mostRecentUsers[$row['uid']] = $row;
100  }
101 
102  // Remove any item that is not an array (means, the stored uid is not available anymore)
103  $mostRecentUsers = array_filter($mostRecentUsers, function ($record) {
104  return is_array($record);
105  });
106 
107  $availableUsers = array_keys($mostRecentUsers);
108  if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
109  $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
110  $backendUser->writeUC();
111  }
112  }
113 
114  $view = $this->‪getFluidTemplateObject('UserToolbarItemDropDown.html');
115  $view->assignMultiple([
116  'modules' => $backendModuleRepository->findByModuleName('user')->getChildren(),
117  'logoutUrl' => (string)$uriBuilder->buildUriFromRoute('logout'),
118  'switchUserMode' => $this->getBackendUser()->user['ses_backuserid'],
119  'recentUsers' => $mostRecentUsers,
120  ]);
121  return $view->render();
122  }
123 
129  public function ‪getAdditionalAttributes()
130  {
131  $result = [
132  'class' => 'toolbar-item-user'
133  ];
134  if ($this->‪getBackendUser()->user['ses_backuserid']) {
135  $result['class'] .= ' su-user';
136  }
137  return $result;
138  }
139 
145  public function ‪hasDropDown()
146  {
147  return true;
148  }
149 
155  public function ‪getIndex()
156  {
157  return 80;
158  }
159 
165  protected function ‪getBackendUser()
166  {
167  return ‪$GLOBALS['BE_USER'];
168  }
169 
177  protected function ‪getFluidTemplateObject(string $filename): ‪StandaloneView
178  {
179  $view = GeneralUtility::makeInstance(StandaloneView::class);
180  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
181  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
182  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
183 
184  $view->setTemplate($filename);
185 
186  $view->getRequest()->setControllerExtensionName('Backend');
187  return $view;
188  }
189 }
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:25
‪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:145
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\checkAccess
‪bool checkAccess()
Definition: UserToolbarItem.php:39
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:16
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename)
Definition: UserToolbarItem.php:177
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: UserToolbarItem.php:165
‪TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository
Definition: BackendModuleRepository.php:34
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪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:129
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\isLoaded
‪static bool isLoaded($key)
Definition: ExtensionManagementUtility.php:114
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getIndex
‪int getIndex()
Definition: UserToolbarItem.php:155