[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / UserToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
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
17 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
18 use TYPO3\CMS\Backend\Routing\UriBuilder;
19 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
20 use TYPO3\CMS\Core\Database\Connection;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Fluid\View\StandaloneView;
25
26 /**
27 * User toolbar item
28 */
29 class UserToolbarItem implements ToolbarItemInterface
30 {
31 /**
32 * Item is always enabled
33 *
34 * @return bool TRUE
35 */
36 public function checkAccess()
37 {
38 return true;
39 }
40
41 /**
42 * Render username and an icon
43 *
44 * @return string HTML
45 */
46 public function getItem()
47 {
48 $backendUser = $this->getBackendUser();
49 $view = $this->getFluidTemplateObject('UserToolbarItem.html');
50 $view->assignMultiple([
51 'currentUser' => $backendUser->user,
52 'switchUserMode' => $backendUser->user['ses_backuserid'],
53 ]);
54 return $view->render();
55 }
56
57 /**
58 * Render drop down
59 *
60 * @return string HTML
61 */
62 public function getDropDown()
63 {
64 $backendUser = $this->getBackendUser();
65
66 /** @var BackendModuleRepository $backendModuleRepository */
67 $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
68 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
69
70 $mostRecentUsers = [];
71 if (ExtensionManagementUtility::isLoaded('beuser')
72 && $backendUser->isAdmin()
73 && (int)$backendUser->user['ses_backuserid'] === 0
74 && isset($backendUser->uc['recentSwitchedToUsers'])
75 && is_array($backendUser->uc['recentSwitchedToUsers'])
76 ) {
77 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
78 $result = $queryBuilder
79 ->select('uid', 'username', 'realName')
80 ->from('be_users')
81 ->where(
82 $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], Connection::PARAM_INT_ARRAY))
83 )->execute();
84
85 // Flip the array to have a "sorted" list of items
86 $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
87
88 while ($row = $result->fetch()) {
89 $row['switchUserLink'] = (string)$uriBuilder->buildUriFromRoute(
90 'system_BeuserTxBeuser',
91 [
92 'SwitchUser' => $row['uid']
93 ]
94 );
95
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, function ($record) {
101 return is_array($record);
102 });
103
104 $availableUsers = array_keys($mostRecentUsers);
105 if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
106 $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
107 $backendUser->writeUC();
108 }
109 }
110
111 $view = $this->getFluidTemplateObject('UserToolbarItemDropDown.html');
112 $view->assignMultiple([
113 'modules' => $backendModuleRepository->findByModuleName('user')->getChildren(),
114 'logoutUrl' => (string)$uriBuilder->buildUriFromRoute('logout'),
115 'switchUserMode' => $this->getBackendUser()->user['ses_backuserid'],
116 'recentUsers' => $mostRecentUsers,
117 ]);
118 return $view->render();
119 }
120
121 /**
122 * Returns an additional class if user is in "switch user" mode
123 *
124 * @return array
125 */
126 public function getAdditionalAttributes()
127 {
128 $result = [
129 'class' => 'toolbar-item-user'
130 ];
131 if ($this->getBackendUser()->user['ses_backuserid']) {
132 $result['class'] .= ' su-user';
133 }
134 return $result;
135 }
136
137 /**
138 * This item has a drop down
139 *
140 * @return bool
141 */
142 public function hasDropDown()
143 {
144 return true;
145 }
146
147 /**
148 * Position relative to others
149 *
150 * @return int
151 */
152 public function getIndex()
153 {
154 return 80;
155 }
156
157 /**
158 * Returns the current BE user.
159 *
160 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
161 */
162 protected function getBackendUser()
163 {
164 return $GLOBALS['BE_USER'];
165 }
166
167 /**
168 * Returns a new standalone view, shorthand function
169 *
170 * @param string $filename Which templateFile should be used.
171 *
172 * @return StandaloneView
173 */
174 protected function getFluidTemplateObject(string $filename):StandaloneView
175 {
176 $view = GeneralUtility::makeInstance(StandaloneView::class);
177 $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
178 $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
179 $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
180
181 $view->setTemplate($filename);
182
183 $view->getRequest()->setControllerExtensionName('Backend');
184 return $view;
185 }
186 }