‪TYPO3CMS  ‪main
UserToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
29 
36 {
37  private ServerRequestInterface ‪$request;
38 
39  public function ‪__construct(
40  private readonly ‪ModuleProvider $moduleProvider,
41  private readonly ‪BackendViewFactory $backendViewFactory,
42  ) {}
43 
44  public function ‪setRequest(ServerRequestInterface ‪$request): void
45  {
46  $this->request = ‪$request;
47  }
48 
52  public function ‪checkAccess(): bool
53  {
54  return true;
55  }
56 
60  public function ‪getItem(): string
61  {
62  $backendUser = $this->‪getBackendUser();
63  $view = $this->backendViewFactory->create($this->request);
64  $view->assignMultiple([
65  'currentUser' => $backendUser->user,
66  'switchUserMode' => (int)$backendUser->getOriginalUserIdWhenInSwitchUserMode(),
67  ]);
68  return $view->render('ToolbarItems/UserToolbarItem');
69  }
70 
74  public function ‪getDropDown(): string
75  {
76  $backendUser = $this->‪getBackendUser();
77 
78  $mostRecentUsers = [];
79  if ($backendUser->isAdmin()
80  && $backendUser->getOriginalUserIdWhenInSwitchUserMode() === null
81  && isset($backendUser->uc['recentSwitchedToUsers'])
82  && is_array($backendUser->uc['recentSwitchedToUsers'])
83  ) {
84  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
85  $result = $queryBuilder
86  ->select('uid', 'username', 'realName')
87  ->from('be_users')
88  ->where(
89  $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], ‪Connection::PARAM_INT_ARRAY))
90  )->executeQuery();
91 
92  // Flip the array to have a "sorted" list of items
93  $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
94 
95  while ($row = $result->fetchAssociative()) {
96  $mostRecentUsers[$row['uid']] = $row;
97  }
98 
99  // Remove any item that is not an array (means, the stored uid is not available anymore)
100  $mostRecentUsers = array_filter($mostRecentUsers, is_array(...));
101 
102  $availableUsers = array_keys($mostRecentUsers);
103  if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
104  $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
105  $backendUser->writeUC();
106  }
107  }
108 
109  $modules = null;
110  if ($userModule = $this->moduleProvider->getModuleForMenu('user', $backendUser)) {
111  $modules = $userModule->getSubModules();
112  }
113  $view = $this->backendViewFactory->create($this->request);
114  $view->assignMultiple([
115  'modules' => $modules,
116  'switchUserMode' => $this->‪getBackendUser()->getOriginalUserIdWhenInSwitchUserMode() !== null,
117  'recentUsers' => $mostRecentUsers,
118  ]);
119  return $view->render('ToolbarItems/UserToolbarItemDropDown');
120  }
121 
125  public function ‪getAdditionalAttributes(): array
126  {
127  $result = [
128  'class' => 'toolbar-item-user',
129  ];
130  if ($this->‪getBackendUser()->getOriginalUserIdWhenInSwitchUserMode()) {
131  $result['class'] .= ' su-user';
132  }
133  return $result;
134  }
135 
139  public function ‪hasDropDown(): bool
140  {
141  return true;
142  }
143 
147  public function ‪getIndex(): int
148  {
149  return 90;
150  }
151 
153  {
154  return ‪$GLOBALS['BE_USER'];
155  }
156 }
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: UserToolbarItem.php:44
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getBackendUser
‪getBackendUser()
Definition: UserToolbarItem.php:152
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem
Definition: UserToolbarItem.php:36
‪TYPO3\CMS\Backend\Module\ModuleProvider
Definition: ModuleProvider.php:29
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\$request
‪ServerRequestInterface $request
Definition: UserToolbarItem.php:37
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getDropDown
‪getDropDown()
Definition: UserToolbarItem.php:74
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getAdditionalAttributes
‪getAdditionalAttributes()
Definition: UserToolbarItem.php:125
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\__construct
‪__construct(private readonly ModuleProvider $moduleProvider, private readonly BackendViewFactory $backendViewFactory,)
Definition: UserToolbarItem.php:39
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:18
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\Toolbar\RequestAwareToolbarItemInterface
Definition: RequestAwareToolbarItemInterface.php:27
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getItem
‪getItem()
Definition: UserToolbarItem.php:60
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\getIndex
‪getIndex()
Definition: UserToolbarItem.php:147
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\checkAccess
‪checkAccess()
Definition: UserToolbarItem.php:52
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT_ARRAY
‪const PARAM_INT_ARRAY
Definition: Connection.php:72
‪TYPO3\CMS\Backend\Backend\ToolbarItems\UserToolbarItem\hasDropDown
‪hasDropDown()
Definition: UserToolbarItem.php:139