7a38a23b3266572dd3a0da374c1b5a810ba69fdd
[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\Backend\Avatar\Avatar;
18 use TYPO3\CMS\Backend\Domain\Model\Module\BackendModule;
19 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
20 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Imaging\IconFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * User toolbar item
28 */
29 class UserToolbarItem implements ToolbarItemInterface
30 {
31 /**
32 * @var IconFactory
33 */
34 protected $iconFactory;
35
36 /**
37 * Constructor
38 */
39 public function __construct()
40 {
41 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
42 }
43
44 /**
45 * Item is always enabled
46 *
47 * @return bool TRUE
48 */
49 public function checkAccess()
50 {
51 return true;
52 }
53
54 /**
55 * Render username
56 *
57 * @return string HTML
58 */
59 public function getItem()
60 {
61 $backendUser = $this->getBackendUser();
62 $languageService = $this->getLanguageService();
63
64 /** @var Avatar $avatar */
65 $avatar = GeneralUtility::makeInstance(Avatar::class);
66 $icon = $avatar->render();
67
68 $realName = $backendUser->user['realName'];
69 $username = $backendUser->user['username'];
70 $label = $realName ?: $username;
71 $title = $username;
72
73 // Switch user mode
74 if ($backendUser->user['ses_backuserid']) {
75 $title = $languageService->getLL('switchtouser') . ': ' . $username;
76 $label = $languageService->getLL('switchtousershort') . ' ' . ($realName ? $realName . ' (' . $username . ')' : $username);
77 }
78
79 $html = [];
80 $html[] = '<span class="toolbar-item-avatar">' . $icon . '</span>';
81 $html[] = '<span class="toolbar-item-name" title="' . htmlspecialchars($title) . '">';
82 $html[] = htmlspecialchars($label);
83 $html[] = '</span>';
84
85 return implode(LF, $html);
86 }
87
88 /**
89 * Render drop down
90 *
91 * @return string HTML
92 */
93 public function getDropDown()
94 {
95 $backendUser = $this->getBackendUser();
96 $languageService = $this->getLanguageService();
97
98 $dropdown = [];
99 $dropdown[] = '<div class="dropdown-table">';
100
101 /** @var BackendModuleRepository $backendModuleRepository */
102 $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
103 /** @var \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule $userModuleMenu */
104 $userModuleMenu = $backendModuleRepository->findByModuleName('user');
105 if ($userModuleMenu != false && $userModuleMenu->getChildren()->count() > 0) {
106 foreach ($userModuleMenu->getChildren() as $module) {
107 /** @var BackendModule $module */
108 $dropdown[] = '<div'
109 . ' class="dropdown-table-row"'
110 . ' id="' . htmlspecialchars($module->getName()) . '"'
111 . ' data-modulename="' . htmlspecialchars($module->getName()) . '"'
112 . ' data-navigationcomponentid="' . htmlspecialchars($module->getNavigationComponentId()) . '"'
113 . ' data-navigationframescript="' . htmlspecialchars($module->getNavigationFrameScript()) . '"'
114 . ' data-navigationframescriptparameters="' . htmlspecialchars($module->getNavigationFrameScriptParameters()) . '"'
115 . '>';
116 $dropdown[] = '<div class="dropdown-table-column dropdown-table-icon">' . $module->getIcon() . '</div>';
117 $dropdown[] = '<div class="dropdown-table-column dropdown-table-title">';
118 $dropdown[] = '<a title="' . htmlspecialchars($module->getDescription()) . '" href="' . htmlspecialchars($module->getLink()) . '" class="modlink">';
119 $dropdown[] = htmlspecialchars($module->getTitle());
120 $dropdown[] = '</a>';
121 $dropdown[] = '</div>';
122 $dropdown[] = '</div>';
123 }
124 }
125 $dropdown[] = '</div>';
126
127 $dropdown[] = '<hr>';
128 // Logout button
129 $buttonLabel = 'LLL:EXT:lang/locallang_core.xlf:' . ($backendUser->user['ses_backuserid'] ? 'buttons.exit' : 'buttons.logout');
130 $dropdown[] = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('logout')) . '" class="btn btn-danger pull-right" target="_top">';
131 $dropdown[] = $this->iconFactory->getIcon('actions-logout', Icon::SIZE_SMALL)->render('inline') . ' ';
132 $dropdown[] = htmlspecialchars($languageService->sL($buttonLabel));
133 $dropdown[] = '</a>';
134
135 return implode(LF, $dropdown);
136 }
137
138 /**
139 * Returns an additional class if user is in "switch user" mode
140 *
141 * @return array
142 */
143 public function getAdditionalAttributes()
144 {
145 $result = [];
146 $result['class'] = 'toolbar-item-user';
147 if ($this->getBackendUser()->user['ses_backuserid']) {
148 $result['class'] .= ' su-user';
149 }
150 return $result;
151 }
152
153 /**
154 * This item has a drop down
155 *
156 * @return bool
157 */
158 public function hasDropDown()
159 {
160 return true;
161 }
162
163 /**
164 * Position relative to others
165 *
166 * @return int
167 */
168 public function getIndex()
169 {
170 return 80;
171 }
172
173 /**
174 * Returns the current BE user.
175 *
176 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
177 */
178 protected function getBackendUser()
179 {
180 return $GLOBALS['BE_USER'];
181 }
182
183 /**
184 * Returns LanguageService
185 *
186 * @return \TYPO3\CMS\Lang\LanguageService
187 */
188 protected function getLanguageService()
189 {
190 return $GLOBALS['LANG'];
191 }
192 }