‪TYPO3CMS  10.4
BackendUserController.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
42 
48 {
53 
57  protected ‪$moduleData;
58 
63 
67  protected ‪$backendUserRepository;
68 
73 
78 
83 
84  public function ‪__construct(
90  ) {
91  $this->moduleDataStorageService = ‪$moduleDataStorageService;
92  $this->backendUserRepository = ‪$backendUserRepository;
93  $this->backendUserGroupRepository = ‪$backendUserGroupRepository;
94  $this->backendUserSessionRepository = ‪$backendUserSessionRepository;
95  $this->userInformationService = ‪$userInformationService;
96  }
97 
106  {
107  $this->moduleData = $this->moduleDataStorageService->loadModuleData();
108  // We "finally" persist the module data.
109  try {
110  parent::processRequest(‪$request, ‪$response);
111  $this->moduleDataStorageService->persistModuleData($this->moduleData);
112  } catch (StopActionException $e) {
113  $this->moduleDataStorageService->persistModuleData($this->moduleData);
114  throw $e;
115  }
116  }
117 
122  protected function ‪initializeView(‪ViewInterface ‪$view)
123  {
125  'shortcutLabel' => 'backendUsers',
126  'dateFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
127  'timeFormat' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
128  ]);
129  }
130 
137  public function ‪indexAction(Demand $demand = null)
138  {
139  if ($demand === null) {
140  $demand = $this->moduleData->getDemand();
141  } else {
142  $this->moduleData->setDemand($demand);
143  }
144  // Switch user until logout
145  $switchUser = (int)GeneralUtility::_GP('SwitchUser');
146  if ($switchUser > 0) {
147  $this->‪switchUser($switchUser);
148  }
149  $compareUserList = $this->moduleData->getCompareUserList();
150 
151  $this->view->assignMultiple([
152  'onlineBackendUsers' => $this->‪getOnlineBackendUsers(),
153  'demand' => $demand,
154  'backendUsers' => $this->backendUserRepository->findDemanded($demand),
155  'backendUserGroups' => array_merge([''], $this->backendUserGroupRepository->findAll()->toArray()),
156  'compareUserUidList' => array_combine($compareUserList, $compareUserList),
157  'currentUserUid' => $this->‪getBackendUserAuthentication()->user['uid'],
158  'compareUserList' => !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '',
159  ]);
160  }
161 
165  public function ‪onlineAction()
166  {
167  $onlineUsersAndSessions = [];
168  $onlineUsers = $this->backendUserRepository->findOnline();
169  foreach ($onlineUsers as $onlineUser) {
170  $onlineUsersAndSessions[] = [
171  'backendUser' => $onlineUser,
172  'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
173  ];
174  }
175 
176  $currentSessionId = $this->‪getBackendUserAuthentication()->‪getSessionId();
177  $sessionBackend = $this->‪getSessionBackend();
178  if ($sessionBackend instanceof HashableSessionBackendInterface) {
179  $currentSessionId = $sessionBackend->hash($currentSessionId);
180  }
181  $this->view->assignMultiple([
182  'shortcutLabel' => 'onlineUsers',
183  'onlineUsersAndSessions' => $onlineUsersAndSessions,
184  'currentSessionId' => $currentSessionId,
185  ]);
186  }
187 
191  public function ‪showAction(int $uid = 0): void
192  {
193  $data = $this->userInformationService->getUserInformation($uid);
194  $this->view->assignMultiple([
195  'shortcutLabel' => 'showUser',
196  'data' => $data
197  ]);
198  }
199 
203  public function ‪compareAction()
204  {
205  $compareUserList = $this->moduleData->getCompareUserList();
206  if (empty($compareUserList)) {
207  $this->‪redirect('index');
208  }
209 
210  $compareData = [];
211  foreach ($compareUserList as $uid) {
212  if ($compareInformation = $this->userInformationService->getUserInformation($uid)) {
213  $compareData[] = $compareInformation;
214  }
215  }
216 
217  $this->view->assignMultiple([
218  'shortcutLabel' => 'compareUsers',
219  'compareUserList' => $compareData,
220  'onlineBackendUsers' => $this->‪getOnlineBackendUsers()
221  ]);
222  }
223 
229  public function ‪initiatePasswordResetAction(int $user): void
230  {
231  $context = GeneralUtility::makeInstance(Context::class);
233  $user = $this->backendUserRepository->findByUid($user);
234  if (!$user || !$user->isPasswordResetEnabled() || !$context->getAspect('backend.user')->isAdmin()) {
235  // Add an error message
236  $this->‪addFlashMessage(
237  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.error.text', 'beuser') ?? '',
238  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.error.title', 'beuser') ?? '',
240  );
241  } else {
242  GeneralUtility::makeInstance(PasswordReset::class)->initiateReset(
243  ‪$GLOBALS['TYPO3_REQUEST'],
244  $context,
245  $user->getEmail()
246  );
247  $this->‪addFlashMessage(
248  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.success.text', 'beuser', [$user->getEmail()]) ?? '',
249  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.success.title', 'beuser') ?? '',
251  );
252  }
253  $this->‪forward('index');
254  }
255 
261  public function ‪addToCompareListAction($uid)
262  {
263  $this->moduleData->attachUidCompareUser($uid);
264  $this->moduleDataStorageService->persistModuleData($this->moduleData);
265  $this->‪forward('index');
266  }
267 
274  public function ‪removeFromCompareListAction($uid, int $redirectToCompare = 0)
275  {
276  $this->moduleData->detachUidCompareUser($uid);
277  $this->moduleDataStorageService->persistModuleData($this->moduleData);
278  if ($redirectToCompare) {
279  $this->‪redirect('compare');
280  } else {
281  $this->‪redirect('index');
282  }
283  }
284 
288  public function ‪removeAllFromCompareListAction(): void
289  {
290  foreach ($this->moduleData->getCompareUserList() as $user) {
291  $this->moduleData->detachUidCompareUser($user);
292  }
293  $this->moduleDataStorageService->persistModuleData($this->moduleData);
294  $this->‪redirect('index');
295  }
296 
304  protected function ‪terminateBackendUserSessionAction(BackendUser $backendUser, $sessionId)
305  {
306  // terminating value of persisted session ID (probably hashed value)
307  $sessionBackend = $this->‪getSessionBackend();
308  $success = $sessionBackend->remove($sessionId);
309 
310  if ($success) {
311  $this->‪addFlashMessage(‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser') ?? '');
312  }
313  $this->‪forward('online');
314  }
315 
321  protected function ‪switchUser($switchUser)
322  {
323  $targetUser = ‪BackendUtility::getRecord('be_users', $switchUser);
324  if (is_array($targetUser) && $this->‪getBackendUserAuthentication()->isAdmin()) {
325  // Set backend user listing module as starting module for switchback
326  $this->‪getBackendUserAuthentication()->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
327  $this->‪getBackendUserAuthentication()->uc['recentSwitchedToUsers'] = $this->‪generateListOfMostRecentSwitchedUsers($targetUser['uid']);
329 
330  // User switch written to log
332  255,
333  2,
334  0,
335  1,
336  'User %s switched to user %s (be_users:%s)',
337  [
338  $this->‪getBackendUserAuthentication()->user['username'],
339  $targetUser['username'],
340  $targetUser['uid'],
341  ]
342  );
343 
345  $this->‪getBackendUserAuthentication()->getSessionId(),
346  [
347  'ses_userid' => (int)$targetUser['uid'],
348  'ses_backuserid' => (int)$this->‪getBackendUserAuthentication()->user['uid']
349  ]
350  );
351 
352  $event = new ‪SwitchUserEvent(
353  $this->‪getBackendUserAuthentication()->getSessionId(),
354  $targetUser,
355  (array)$this->‪getBackendUserAuthentication()->user
356  );
357  $this->eventDispatcher->dispatch($event);
358 
359  $redirectUrl = 'index.php' . (‪$GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
360  ‪HttpUtility::redirect($redirectUrl);
361  }
362  }
363 
370  protected function ‪generateListOfMostRecentSwitchedUsers(int $targetUserUid): array
371  {
372  $latestUserUids = [];
373  $backendUser = $this->‪getBackendUserAuthentication();
374 
375  if (isset($backendUser->uc['recentSwitchedToUsers']) && is_array($backendUser->uc['recentSwitchedToUsers'])) {
376  $latestUserUids = $backendUser->uc['recentSwitchedToUsers'];
377  }
378 
379  // Remove potentially existing user in that list
380  $index = array_search($targetUserUid, $latestUserUids, true);
381  if ($index !== false) {
382  unset($latestUserUids[$index]);
383  }
384 
385  array_unshift($latestUserUids, $targetUserUid);
386  $latestUserUids = array_slice($latestUserUids, 0, static::RECENT_USERS_LIMIT);
387 
388  return $latestUserUids;
389  }
390 
395  {
396  return ‪$GLOBALS['BE_USER'];
397  }
398 
402  protected function ‪getSessionBackend()
403  {
404  $loginType = $this->‪getBackendUserAuthentication()->‪getLoginType();
405  return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType);
406  }
407 
416  protected function ‪getOnlineBackendUsers(): array
417  {
418  $onlineUsers = $this->backendUserSessionRepository->findAllActive();
419  $onlineBackendUsers = [];
420  if (is_array($onlineUsers)) {
421  foreach ($onlineUsers as $onlineUser) {
422  $onlineBackendUsers[$onlineUser['ses_userid']] = true;
423  }
424  }
425  return $onlineBackendUsers;
426  }
427 }
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:31
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\forward
‪forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
Definition: ActionController.php:815
‪TYPO3\CMS\Beuser\Controller\BackendUserController\RECENT_USERS_LIMIT
‪const RECENT_USERS_LIMIT
Definition: BackendUserController.php:52
‪TYPO3\CMS\Beuser\Controller\BackendUserController\showAction
‪showAction(int $uid=0)
Definition: BackendUserController.php:185
‪TYPO3\CMS\Beuser\Controller\BackendUserController\removeFromCompareListAction
‪removeFromCompareListAction($uid, int $redirectToCompare=0)
Definition: BackendUserController.php:268
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:1120
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Beuser\Controller\BackendUserController\initiatePasswordResetAction
‪initiatePasswordResetAction(int $user)
Definition: BackendUserController.php:223
‪TYPO3\CMS\Extbase\Mvc\ResponseInterface
Definition: ResponseInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$view
‪ViewInterface $view
Definition: ActionController.php:76
‪TYPO3\CMS\Backend\Authentication\PasswordReset
Definition: PasswordReset.php:58
‪TYPO3\CMS\Core\Session\SessionManager
Definition: SessionManager.php:39
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserGroupRepository
‪BackendUserGroupRepository $backendUserGroupRepository
Definition: BackendUserController.php:68
‪TYPO3\CMS\Beuser\Controller
Definition: BackendUserController.php:16
‪TYPO3\CMS\Core\Session\Backend\HashableSessionBackendInterface
Definition: HashableSessionBackendInterface.php:21
‪TYPO3\CMS\Beuser\Controller\BackendUserController\generateListOfMostRecentSwitchedUsers
‪int[] generateListOfMostRecentSwitchedUsers(int $targetUserUid)
Definition: BackendUserController.php:364
‪TYPO3\CMS\Beuser\Controller\BackendUserController\compareAction
‪compareAction()
Definition: BackendUserController.php:197
‪TYPO3\CMS\Beuser\Controller\BackendUserController\addToCompareListAction
‪addToCompareListAction($uid)
Definition: BackendUserController.php:255
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=AbstractMessage::OK, $storeInSession=true)
Definition: ActionController.php:747
‪TYPO3\CMS\Beuser\Controller\BackendUserController\__construct
‪__construct(ModuleDataStorageService $moduleDataStorageService, BackendUserRepository $backendUserRepository, BackendUserGroupRepository $backendUserGroupRepository, BackendUserSessionRepository $backendUserSessionRepository, UserInformationService $userInformationService)
Definition: BackendUserController.php:78
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$moduleDataStorageService
‪ModuleDataStorageService $moduleDataStorageService
Definition: BackendUserController.php:60
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Beuser\Controller\BackendUserController\getSessionBackend
‪SessionBackendInterface getSessionBackend()
Definition: BackendUserController.php:396
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$moduleData
‪TYPO3 CMS Beuser Domain Model ModuleData $moduleData
Definition: BackendUserController.php:56
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: ActionController.php:852
‪TYPO3\CMS\Beuser\Controller\BackendUserController\terminateBackendUserSessionAction
‪terminateBackendUserSessionAction(BackendUser $backendUser, $sessionId)
Definition: BackendUserController.php:298
‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository
Definition: BackendUserSessionRepository.php:30
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$response
‪TYPO3 CMS Extbase Mvc Response $response
Definition: ActionController.php:115
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserRepository
‪BackendUserRepository $backendUserRepository
Definition: BackendUserController.php:64
‪TYPO3\CMS\Beuser\Controller\BackendUserController\indexAction
‪indexAction(Demand $demand=null)
Definition: BackendUserController.php:131
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface\update
‪array update(string $sessionId, array $sessionData)
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$backendUserSessionRepository
‪BackendUserSessionRepository $backendUserSessionRepository
Definition: BackendUserController.php:72
‪TYPO3\CMS\Core\Session\Backend\SessionBackendInterface
Definition: SessionBackendInterface.php:28
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Beuser\Controller\BackendUserController\$userInformationService
‪UserInformationService $userInformationService
Definition: BackendUserController.php:76
‪TYPO3\CMS\Beuser\Controller\BackendUserController\initializeView
‪initializeView(ViewInterface $view)
Definition: BackendUserController.php:116
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository
Definition: BackendUserRepository.php:34
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Beuser\Controller\BackendUserController\getBackendUserAuthentication
‪BackendUserAuthentication getBackendUserAuthentication()
Definition: BackendUserController.php:388
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getLoginType
‪string getLoginType()
Definition: AbstractUserAuthentication.php:1492
‪TYPO3\CMS\Beuser\Controller\BackendUserController\switchUser
‪switchUser($switchUser)
Definition: BackendUserController.php:315
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:95
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Beuser\Domain\Model\Demand
Definition: Demand.php:25
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:22
‪TYPO3\CMS\Beuser\Controller\BackendUserController\getOnlineBackendUsers
‪array getOnlineBackendUsers()
Definition: BackendUserController.php:410
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\writelog
‪int writelog($type, $action, $error, $details_nr, $details, $data, $tablename='', $recuid='', $recpid='', $event_pid=-1, $NEWid='', $userId=0)
Definition: BackendUserAuthentication.php:2290
‪$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:109
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:55
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:24
‪TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository
Definition: BackendUserGroupRepository.php:26
‪TYPO3\CMS\Beuser\Controller\BackendUserController\processRequest
‪processRequest(RequestInterface $request, ResponseInterface $response)
Definition: BackendUserController.php:99
‪TYPO3\CMS\Beuser\Controller\BackendUserController
Definition: BackendUserController.php:48
‪TYPO3\CMS\Backend\Authentication\Event\SwitchUserEvent
Definition: SwitchUserEvent.php:24
‪TYPO3\CMS\Beuser\Domain\Model\BackendUser
Definition: BackendUser.php:26
‪TYPO3\CMS\Beuser\Controller\BackendUserController\removeAllFromCompareListAction
‪removeAllFromCompareListAction()
Definition: BackendUserController.php:282
‪TYPO3\CMS\Beuser\Service\UserInformationService
Definition: UserInformationService.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Beuser\Controller\BackendUserController\onlineAction
‪onlineAction()
Definition: BackendUserController.php:159
‪TYPO3\CMS\Core\Utility\HttpUtility\redirect
‪static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:106
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface\assignMultiple
‪TYPO3 CMS Extbase Mvc View ViewInterface assignMultiple(array $values)
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Beuser\Service\ModuleDataStorageService
Definition: ModuleDataStorageService.php:29
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getSessionId
‪string getSessionId()
Definition: AbstractUserAuthentication.php:1483