‪TYPO3CMS  9.5
BackendUserController.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 
26 
32 {
34 
38  private ‪$deprecatedPublicMethods = [
39  'initializeView' => 'Using BackendUserController::initializeView() is deprecated and will not be possible anymore in TYPO3 v10.0.',
40  ];
41 
45  const ‪RECENT_USERS_LIMIT = 3;
46 
50  protected ‪$moduleData;
51 
56 
60  protected ‪$backendUserRepository;
61 
66 
71 
75  public function ‪injectModuleDataStorageService(\‪TYPO3\CMS\Beuser\Service\ModuleDataStorageService ‪$moduleDataStorageService)
76  {
77  $this->moduleDataStorageService = ‪$moduleDataStorageService;
78  }
79 
83  public function ‪injectBackendUserRepository(\‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository ‪$backendUserRepository)
84  {
85  $this->backendUserRepository = ‪$backendUserRepository;
86  }
87 
91  public function ‪injectBackendUserGroupRepository(\‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository ‪$backendUserGroupRepository)
92  {
93  $this->backendUserGroupRepository = ‪$backendUserGroupRepository;
94  }
95 
99  public function ‪injectBackendUserSessionRepository(\‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository ‪$backendUserSessionRepository)
100  {
101  $this->backendUserSessionRepository = ‪$backendUserSessionRepository;
102  }
103 
111  public function ‪processRequest(\‪TYPO3\CMS\‪Extbase\Mvc\RequestInterface ‪$request, \‪TYPO3\CMS\‪Extbase\Mvc\ResponseInterface ‪$response)
112  {
113  $this->moduleData = $this->moduleDataStorageService->loadModuleData();
114  // We "finally" persist the module data.
115  try {
116  parent::processRequest(‪$request, ‪$response);
117  $this->moduleDataStorageService->persistModuleData($this->moduleData);
118  } catch (\‪TYPO3\CMS\‪Extbase\Mvc\Exception\StopActionException $e) {
119  $this->moduleDataStorageService->persistModuleData($this->moduleData);
120  throw $e;
121  }
122  }
123 
128  protected function ‪initializeView(ViewInterface ‪$view)
129  {
131  'shortcutLabel' => 'backendUsers',
132  'dateFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
133  'timeFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
134  ]);
135  }
136 
143  public function ‪indexAction(\‪TYPO3\CMS\Beuser\Domain\Model\Demand $demand = null)
144  {
145  if ($demand === null) {
146  $demand = $this->moduleData->getDemand();
147  } else {
148  $this->moduleData->setDemand($demand);
149  }
150  // Switch user until logout
151  $switchUser = (int)GeneralUtility::_GP('SwitchUser');
152  if ($switchUser > 0) {
153  $this->‪switchUser($switchUser);
154  }
155  $compareUserList = $this->moduleData->getCompareUserList();
156 
157  // Create online user list for easy parsing
158  $onlineUsers = $this->backendUserSessionRepository->findAllActive();
159  $onlineBackendUsers = [];
160  if (is_array($onlineUsers)) {
161  foreach ($onlineUsers as $onlineUser) {
162  $onlineBackendUsers[$onlineUser['ses_userid']] = true;
163  }
164  }
165 
166  $this->view->assignMultiple([
167  'onlineBackendUsers' => $onlineBackendUsers,
168  'demand' => $demand,
169  'backendUsers' => $this->backendUserRepository->findDemanded($demand),
170  'backendUserGroups' => array_merge([''], $this->backendUserGroupRepository->findAll()->toArray()),
171  'compareUserUidList' => array_combine($compareUserList, $compareUserList),
172  'currentUserUid' => $this->‪getBackendUserAuthentication()->user['uid'],
173  'compareUserList' => !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '',
174  ]);
175  }
176 
180  public function ‪onlineAction()
181  {
182  $onlineUsersAndSessions = [];
183  $onlineUsers = $this->backendUserRepository->findOnline();
184  foreach ($onlineUsers as $onlineUser) {
185  $onlineUsersAndSessions[] = [
186  'backendUser' => $onlineUser,
187  'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
188  ];
189  }
190 
191  $currentSessionId = $this->‪getBackendUserAuthentication()->‪getSessionId();
192  $sessionBackend = $this->‪getSessionBackend();
193  if ($sessionBackend instanceof HashableSessionBackendInterface) {
194  $currentSessionId = $sessionBackend->hash($currentSessionId);
195  }
196  $this->view->assignMultiple([
197  'shortcutLabel' => 'onlineUsers',
198  'onlineUsersAndSessions' => $onlineUsersAndSessions,
199  'currentSessionId' => $currentSessionId,
200  ]);
201  }
202 
206  public function ‪compareAction()
207  {
208  $compareUserList = $this->moduleData->getCompareUserList();
209  if (empty($compareUserList)) {
210  $this->‪redirect('index');
211  }
212 
213  $this->view->assignMultiple([
214  'shortcutLabel' => 'compareUsers',
215  'compareUserList' => $this->backendUserRepository->findByUidList($compareUserList),
216  ]);
217  }
218 
224  public function ‪addToCompareListAction($uid)
225  {
226  $this->moduleData->attachUidCompareUser($uid);
227  $this->moduleDataStorageService->persistModuleData($this->moduleData);
228  $this->‪forward('index');
229  }
230 
236  public function ‪removeFromCompareListAction($uid)
237  {
238  $this->moduleData->detachUidCompareUser($uid);
239  $this->moduleDataStorageService->persistModuleData($this->moduleData);
240  $this->‪forward('index');
241  }
242 
250  protected function ‪terminateBackendUserSessionAction(\‪TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
251  {
252  // terminating value of persisted session ID (probably hashed value)
253  $sessionBackend = $this->‪getSessionBackend();
254  $success = $sessionBackend->remove($sessionId);
255 
256  if ($success) {
257  $this->‪addFlashMessage(‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser'));
258  }
259  $this->‪forward('online');
260  }
261 
267  protected function ‪switchUser($switchUser)
268  {
269  $targetUser = ‪\TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('be_users', $switchUser);
270  if (is_array($targetUser) && $this->‪getBackendUserAuthentication()->isAdmin()) {
271  // Set backend user listing module as starting module for switchback
272  $this->‪getBackendUserAuthentication()->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
273  $this->‪getBackendUserAuthentication()->uc['recentSwitchedToUsers'] = $this->‪generateListOfMostRecentSwitchedUsers($targetUser['uid']);
275 
277  $this->‪getBackendUserAuthentication()->getSessionId(),
278  [
279  'ses_userid' => (int)$targetUser['uid'],
280  'ses_backuserid' => (int)$this->‪getBackendUserAuthentication()->user['uid']
281  ]
282  );
283 
284  $this->‪emitSwitchUserSignal($targetUser);
285 
286  $redirectUrl = 'index.php' . (‪$GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
288  }
289  }
290 
297  protected function ‪generateListOfMostRecentSwitchedUsers(int $targetUserUid): array
298  {
299  $latestUserUids = [];
300  $backendUser = $this->‪getBackendUserAuthentication();
301 
302  if (isset($backendUser->uc['recentSwitchedToUsers']) && is_array($backendUser->uc['recentSwitchedToUsers'])) {
303  $latestUserUids = $backendUser->uc['recentSwitchedToUsers'];
304  }
305 
306  // Remove potentially existing user in that list
307  $index = array_search($targetUserUid, $latestUserUids, true);
308  if ($index !== false) {
309  unset($latestUserUids[$index]);
310  }
311 
312  array_unshift($latestUserUids, $targetUserUid);
313  $latestUserUids = array_slice($latestUserUids, 0, static::RECENT_USERS_LIMIT);
314 
315  return $latestUserUids;
316  }
317 
323  protected function ‪emitSwitchUserSignal(array $targetUser)
324  {
325  $this->signalSlotDispatcher->dispatch(__CLASS__, 'switchUser', [$targetUser]);
326  }
327 
332  {
333  return ‪$GLOBALS['BE_USER'];
334  }
335 
339  protected function ‪getSessionBackend()
340  {
341  $loginType = $this->‪getBackendUserAuthentication()->‪getLoginType();
342  return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType);
343  }
344 }
‪TYPO3\CMS\Beuser\Controller\BackendUserController\RECENT_USERS_LIMIT
‪const RECENT_USERS_LIMIT
Definition: BackendUserController.php:43
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserSessionRepository
‪TYPO3 CMS Beuser Domain Repository BackendUserSessionRepository $backendUserSessionRepository
Definition: BackendUserController.php:63
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Beuser\Exception
Definition: Exception.php:24
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:1172
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:29
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$view
‪ViewInterface $view
Definition: ActionController.php:44
‪TYPO3\CMS\Beuser\Controller\BackendUserController\processRequest
‪processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
Definition: BackendUserController.php:104
‪TYPO3\CMS\Beuser\Controller\BackendUserController\removeFromCompareListAction
‪removeFromCompareListAction($uid)
Definition: BackendUserController.php:229
‪TYPO3
‪TYPO3\CMS\Beuser\Controller\BackendUserController\injectModuleDataStorageService
‪injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
Definition: BackendUserController.php:68
‪TYPO3\CMS\Core\Session\SessionManager
Definition: SessionManager.php:37
‪TYPO3\CMS\Beuser\Controller
Definition: BackendUserController.php:2
‪TYPO3\CMS\Core\Session\Backend\HashableSessionBackendInterface
Definition: HashableSessionBackendInterface.php:21
‪TYPO3\CMS\Beuser\Controller\BackendUserController\generateListOfMostRecentSwitchedUsers
‪int[] generateListOfMostRecentSwitchedUsers(int $targetUserUid)
Definition: BackendUserController.php:290
‪TYPO3\CMS\Beuser\Controller\BackendUserController\compareAction
‪compareAction()
Definition: BackendUserController.php:199
‪TYPO3\CMS\Beuser\Controller\BackendUserController\addToCompareListAction
‪addToCompareListAction($uid)
Definition: BackendUserController.php:217
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserGroupRepository
‪TYPO3 CMS Beuser Domain Repository BackendUserGroupRepository $backendUserGroupRepository
Definition: BackendUserController.php:59
‪TYPO3\CMS\Beuser\Controller\BackendUserController\getSessionBackend
‪SessionBackendInterface getSessionBackend()
Definition: BackendUserController.php:332
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$moduleData
‪TYPO3 CMS Beuser Domain Model ModuleData $moduleData
Definition: BackendUserController.php:47
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$response
‪TYPO3 CMS Extbase Mvc Response $response
Definition: ActionController.php:93
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\update
‪array update(string $sessionId, array $sessionData)
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: AbstractController.php:284
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface
Definition: SessionBackendInterface.php:26
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserRepository
‪TYPO3 CMS Beuser Domain Repository BackendUserRepository $backendUserRepository
Definition: BackendUserController.php:55
‪TYPO3\CMS\Beuser\Controller\BackendUserController\initializeView
‪initializeView(ViewInterface $view)
Definition: BackendUserController.php:121
‪TYPO3\CMS\Beuser\Controller\BackendUserController\injectBackendUserSessionRepository
‪injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)
Definition: BackendUserController.php:92
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:21
‪TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait
Definition: PublicMethodDeprecationTrait.php:68
‪TYPO3\CMS\Beuser\Controller\BackendUserController\getBackendUserAuthentication
‪BackendUserAuthentication getBackendUserAuthentication()
Definition: BackendUserController.php:324
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getLoginType
‪string getLoginType()
Definition: AbstractUserAuthentication.php:1612
‪TYPO3\CMS\Beuser\Controller\BackendUserController\switchUser
‪switchUser($switchUser)
Definition: BackendUserController.php:260
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Beuser\Controller\BackendUserController\indexAction
‪indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand=null)
Definition: BackendUserController.php:136
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪TYPO3 CMS Extbase Mvc Request $request
Definition: ActionController.php:87
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:31
‪TYPO3\CMS\Beuser\Controller\BackendUserController\terminateBackendUserSessionAction
‪terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
Definition: BackendUserController.php:243
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$deprecatedPublicMethods
‪array $deprecatedPublicMethods
Definition: BackendUserController.php:36
‪TYPO3\CMS\Beuser\Controller\BackendUserController
Definition: BackendUserController.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Beuser\Controller\BackendUserController\onlineAction
‪onlineAction()
Definition: BackendUserController.php:173
‪TYPO3\CMS\Core\Utility\HttpUtility\redirect
‪static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:103
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface\assignMultiple
‪TYPO3 CMS Extbase Mvc View ViewInterface assignMultiple(array $values)
‪TYPO3\CMS\Beuser\Controller\BackendUserController\injectBackendUserRepository
‪injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
Definition: BackendUserController.php:76
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
Definition: AbstractController.php:154
‪TYPO3\CMS\Beuser\Controller\BackendUserController\injectBackendUserGroupRepository
‪injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
Definition: BackendUserController.php:84
‪TYPO3\CMS\Beuser\Controller\BackendUserController\emitSwitchUserSignal
‪emitSwitchUserSignal(array $targetUser)
Definition: BackendUserController.php:316
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getSessionId
‪string getSessionId()
Definition: AbstractUserAuthentication.php:1603
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$moduleDataStorageService
‪TYPO3 CMS Beuser Service ModuleDataStorageService $moduleDataStorageService
Definition: BackendUserController.php:51
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\forward
‪forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
Definition: AbstractController.php:247