TYPO3 CMS  TYPO3_8-7
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 
24 
29 {
33  protected $moduleData;
34 
39 
44 
49 
54 
58  public function injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
59  {
60  $this->moduleDataStorageService = $moduleDataStorageService;
61  }
62 
66  public function injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
67  {
68  $this->backendUserRepository = $backendUserRepository;
69  }
70 
74  public function injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
75  {
76  $this->backendUserGroupRepository = $backendUserGroupRepository;
77  }
78 
82  public function injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)
83  {
84  $this->backendUserSessionRepository = $backendUserSessionRepository;
85  }
86 
94  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
95  {
96  $this->moduleData = $this->moduleDataStorageService->loadModuleData();
97  // We "finally" persist the module data.
98  try {
99  parent::processRequest($request, $response);
100  $this->moduleDataStorageService->persistModuleData($this->moduleData);
101  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
102  $this->moduleDataStorageService->persistModuleData($this->moduleData);
103  throw $e;
104  }
105  }
106 
112  public function initializeAction()
113  {
114  // @TODO: Extbase backend modules relies on frontend TypoScript for view, persistence
115  // and settings. Thus, we need a TypoScript root template, that then loads the
116  // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
117  // circumvented until there is a better solution in extbase.
118  // For now we throw an exception if no settings are detected.
119  if (empty($this->settings)) {
120  throw new \RuntimeException('No settings detected. This module can not work then. This usually happens if there is no frontend TypoScript template with root flag set. ' . 'Please create a frontend page with a TypoScript root template.', 1344375003);
121  }
122  }
123 
130  public function indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand = null)
131  {
132  if ($demand === null) {
133  $demand = $this->moduleData->getDemand();
134  } else {
135  $this->moduleData->setDemand($demand);
136  }
137  // Switch user until logout
138  $switchUser = (int)GeneralUtility::_GP('SwitchUser');
139  if ($switchUser > 0) {
140  $this->switchUser($switchUser);
141  }
142  $compareUserList = $this->moduleData->getCompareUserList();
143 
144  // Create online user list for easy parsing
145  $onlineUsers = $this->backendUserSessionRepository->findAllActive();
146  $onlineBackendUsers = [];
147  if (is_array($onlineUsers)) {
148  foreach ($onlineUsers as $onlineUser) {
149  $onlineBackendUsers[$onlineUser['ses_userid']] = true;
150  }
151  }
152  $this->view->assign('onlineBackendUsers', $onlineBackendUsers);
153 
154  $this->view->assign('demand', $demand);
155  $this->view->assign('returnUrl', rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser')));
156  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
157  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
158  $this->view->assign('backendUsers', $this->backendUserRepository->findDemanded($demand));
159  $this->view->assign('backendUserGroups', array_merge([''], $this->backendUserGroupRepository->findAll()->toArray()));
160  $this->view->assign('compareUserUidList', array_map(function ($item) {
161  return true;
162  }, array_flip((array)$compareUserList)));
163  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
164  }
165 
169  public function onlineAction()
170  {
171  $onlineUsersAndSessions = [];
172  $onlineUsers = $this->backendUserRepository->findOnline();
173  foreach ($onlineUsers as $onlineUser) {
174  $onlineUsersAndSessions[] = [
175  'backendUser' => $onlineUser,
176  'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
177  ];
178  }
179  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
180  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
181  $this->view->assign('onlineUsersAndSessions', $onlineUsersAndSessions);
182  $this->view->assign('currentSessionId', $this->getBackendUserAuthentication()->user['ses_id']);
183  }
184 
188  public function compareAction()
189  {
190  $compareUserList = $this->moduleData->getCompareUserList();
191  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
192  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
193  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
194  }
195 
201  public function addToCompareListAction($uid)
202  {
203  $this->moduleData->attachUidCompareUser($uid);
204  $this->moduleDataStorageService->persistModuleData($this->moduleData);
205  $this->forward('index');
206  }
207 
213  public function removeFromCompareListAction($uid)
214  {
215  $this->moduleData->detachUidCompareUser($uid);
216  $this->moduleDataStorageService->persistModuleData($this->moduleData);
217  $this->forward('index');
218  }
219 
227  protected function terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
228  {
229  $sessionBackend = $this->getSessionBackend();
230  $success = $sessionBackend->remove($sessionId);
231 
232  if ($success) {
233  $this->addFlashMessage(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser'));
234  }
235  $this->forward('online');
236  }
237 
243  protected function switchUser($switchUser)
244  {
245  $targetUser = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('be_users', $switchUser);
246  if (is_array($targetUser) && $this->getBackendUserAuthentication()->isAdmin()) {
247  // Set backend user listing module as starting module for switchback
248  $this->getBackendUserAuthentication()->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
249  $this->getBackendUserAuthentication()->writeUC();
250 
251  $sessionBackend = $this->getSessionBackend();
252  $sessionBackend->update(
253  $this->getBackendUserAuthentication()->getSessionId(),
254  [
255  'ses_userid' => (int)$targetUser['uid'],
256  'ses_backuserid' => (int)$this->getBackendUserAuthentication()->user['uid']
257  ]
258  );
259 
260  $redirectUrl = 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
262  }
263  }
264 
268  protected function getBackendUserAuthentication()
269  {
270  return $GLOBALS['BE_USER'];
271  }
272 
276  protected function getLanguageService()
277  {
278  return $GLOBALS['LANG'];
279  }
280 
284  protected function getSessionBackend()
285  {
286  $loginType = $this->getBackendUserAuthentication()->getLoginType();
287  return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType);
288  }
289 }
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand=null)
static translate($key, $extensionName=null, $arguments=null)
static makeInstance($className,... $constructorArguments)
injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)