4b36257c9594fca8d80bc95e8c1ba328816b8f99
[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\Toolbar\ToolbarItemInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
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
69 $mostRecentUsers = [];
70 if (ExtensionManagementUtility::isLoaded('beuser')
71 && $backendUser->isAdmin()
72 && (int)$backendUser->user['ses_backuserid'] === 0
73 && isset($backendUser->uc['recentSwitchedToUsers'])
74 && is_array($backendUser->uc['recentSwitchedToUsers'])
75 ) {
76 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
77 $result = $queryBuilder
78 ->select('uid', 'username', 'realName')
79 ->from('be_users')
80 ->where(
81 $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($backendUser->uc['recentSwitchedToUsers'], Connection::PARAM_INT_ARRAY))
82 )->execute();
83
84 // Flip the array to have a "sorted" list of items
85 $mostRecentUsers = array_flip($backendUser->uc['recentSwitchedToUsers']);
86
87 while ($row = $result->fetch()) {
88 $row['switchUserLink'] = BackendUtility::getModuleUrl(
89 'system_BeuserTxBeuser',
90 [
91 'SwitchUser' => $row['uid']
92 ]
93 );
94
95 $mostRecentUsers[$row['uid']] = $row;
96 }
97
98 // Remove any item that is not an array (means, the stored uid is not available anymore)
99 $mostRecentUsers = array_filter($mostRecentUsers, function ($record) {
100 return is_array($record);
101 });
102
103 $availableUsers = array_keys($mostRecentUsers);
104 if (!empty(array_diff($backendUser->uc['recentSwitchedToUsers'], $availableUsers))) {
105 $backendUser->uc['recentSwitchedToUsers'] = $availableUsers;
106 $backendUser->writeUC();
107 }
108 }
109
110 $view = $this->getFluidTemplateObject('UserToolbarItemDropDown.html');
111 $view->assignMultiple([
112 'modules' => $backendModuleRepository->findByModuleName('user')->getChildren(),
113 'logoutUrl' => BackendUtility::getModuleUrl('logout'),
114 'switchUserMode' => $this->getBackendUser()->user['ses_backuserid'],
115 'recentUsers' => $mostRecentUsers,
116 ]);
117 return $view->render();
118 }
119
120 /**
121 * Returns an additional class if user is in "switch user" mode
122 *
123 * @return array
124 */
125 public function getAdditionalAttributes()
126 {
127 $result = [
128 'class' => 'toolbar-item-user'
129 ];
130 if ($this->getBackendUser()->user['ses_backuserid']) {
131 $result['class'] .= ' su-user';
132 }
133 return $result;
134 }
135
136 /**
137 * This item has a drop down
138 *
139 * @return bool
140 */
141 public function hasDropDown()
142 {
143 return true;
144 }
145
146 /**
147 * Position relative to others
148 *
149 * @return int
150 */
151 public function getIndex()
152 {
153 return 80;
154 }
155
156 /**
157 * Returns the current BE user.
158 *
159 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
160 */
161 protected function getBackendUser()
162 {
163 return $GLOBALS['BE_USER'];
164 }
165
166 /**
167 * Returns a new standalone view, shorthand function
168 *
169 * @param string $filename Which templateFile should be used.
170 *
171 * @return StandaloneView
172 */
173 protected function getFluidTemplateObject(string $filename):StandaloneView
174 {
175 $view = GeneralUtility::makeInstance(StandaloneView::class);
176 $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
177 $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
178 $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
179
180 $view->setTemplate($filename);
181
182 $view->getRequest()->setControllerExtensionName('Backend');
183 return $view;
184 }
185 }