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