TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
BackendUserController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Beuser\Controller;
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 
23 
28 {
32  protected $moduleData;
33 
38 
43 
48 
53 
57  public function injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
58  {
59  $this->moduleDataStorageService = $moduleDataStorageService;
60  }
61 
65  public function injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
66  {
67  $this->backendUserRepository = $backendUserRepository;
68  }
69 
73  public function injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
74  {
75  $this->backendUserGroupRepository = $backendUserGroupRepository;
76  }
77 
81  public function injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)
82  {
83  $this->backendUserSessionRepository = $backendUserSessionRepository;
84  }
85 
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 
113  public function initializeAction()
114  {
115  // @TODO: Extbase backend modules relies on frontend TypoScript for view, persistence
116  // and settings. Thus, we need a TypoScript root template, that then loads the
117  // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
118  // circumvented until there is a better solution in extbase.
119  // For now we throw an exception if no settings are detected.
120  if (empty($this->settings)) {
121  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);
122  }
123  }
124 
132  public function indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand = null)
133  {
134  if ($demand === null) {
135  $demand = $this->moduleData->getDemand();
136  } else {
137  $this->moduleData->setDemand($demand);
138  }
139  // Switch user until logout
140  $switchUser = (int)GeneralUtility::_GP('SwitchUser');
141  if ($switchUser > 0) {
142  $this->switchUser($switchUser);
143  }
144  $compareUserList = $this->moduleData->getCompareUserList();
145 
146  // Create online user list for easy parsing
147  $onlineUsers = $this->backendUserSessionRepository->findAllActive();
148  $onlineBackendUsers = [];
149  if (is_array($onlineUsers)) {
150  foreach ($onlineUsers as $onlineUser) {
151  $onlineBackendUsers[$onlineUser['ses_userid']] = true;
152  }
153  }
154  $this->view->assign('onlineBackendUsers', $onlineBackendUsers);
155 
156  $this->view->assign('demand', $demand);
157  $this->view->assign('returnUrl', rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser')));
158  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
159  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
160  $this->view->assign('backendUsers', $this->backendUserRepository->findDemanded($demand));
161  $this->view->assign('backendUserGroups', array_merge([''], $this->backendUserGroupRepository->findAll()->toArray()));
162  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
163  }
164 
170  public function onlineAction()
171  {
172  $onlineUsersAndSessions = [];
173  $onlineUsers = $this->backendUserRepository->findOnline();
174  foreach ($onlineUsers as $onlineUser) {
175  $onlineUsersAndSessions[] = [
176  'backendUser' => $onlineUser,
177  'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
178  ];
179  }
180  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
181  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
182  $this->view->assign('onlineUsersAndSessions', $onlineUsersAndSessions);
183  $this->view->assign('currentSessionId', $this->getBackendUserAuthentication()->user['ses_id']);
184  }
185 
191  public function compareAction()
192  {
193  $compareUserList = $this->moduleData->getCompareUserList();
194  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
195  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
196  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
197  }
198 
205  public function addToCompareListAction($uid)
206  {
207  $this->moduleData->attachUidCompareUser($uid);
208  $this->moduleDataStorageService->persistModuleData($this->moduleData);
209  $this->forward('index');
210  }
211 
218  public function removeFromCompareListAction($uid)
219  {
220  $this->moduleData->detachUidCompareUser($uid);
221  $this->moduleDataStorageService->persistModuleData($this->moduleData);
222  $this->forward('index');
223  }
224 
233  protected function terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
234  {
235  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
236  ->getQueryBuilderForTable('be_sessions');
237 
238  $affectedRows = $queryBuilder
239  ->delete('be_sessions')
240  ->where(
241  $queryBuilder->expr()->eq(
242  'ses_userid',
243  $queryBuilder->createNamedParameter($backendUser->getUid(), \PDO::PARAM_INT)
244  ),
245  $queryBuilder->expr()->eq(
246  'ses_id',
247  $queryBuilder->createNamedParameter($sessionId, \PDO::PARAM_STR)
248  )
249  )
250  ->execute();
251 
252  if ($affectedRows === 1) {
253  $this->addFlashMessage(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser'));
254  }
255  $this->forward('online');
256  }
257 
264  protected function switchUser($switchUser)
265  {
266  $targetUser = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('be_users', $switchUser);
267  if (is_array($targetUser) && $this->getBackendUserAuthentication()->isAdmin()) {
268  // Set backend user listing module as starting module for switchback
269  $this->getBackendUserAuthentication()->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
270  $this->getBackendUserAuthentication()->writeUC();
271 
272  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
273  ->getQueryBuilderForTable('be_sessions');
274 
275  $queryBuilder
276  ->update('be_sessions')
277  ->where(
278  $queryBuilder->expr()->eq(
279  'ses_id',
280  $queryBuilder->createNamedParameter(
281  $this->getBackendUserAuthentication()->id,
282  \PDO::PARAM_STR
283  )
284  ),
285  $queryBuilder->expr()->eq(
286  'ses_name',
287  $queryBuilder->createNamedParameter(
288  \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::getCookieName(),
289  \PDO::PARAM_STR
290  )
291  ),
292  $queryBuilder->expr()->eq(
293  'ses_userid',
294  $queryBuilder->createNamedParameter(
295  $this->getBackendUserAuthentication()->user['uid'],
296  \PDO::PARAM_INT
297  )
298  )
299  )
300  ->set('ses_userid', (int)$targetUser['uid'])
301  ->set('ses_backuserid', (int)$this->getBackendUserAuthentication()->user['uid'])
302  ->execute();
303 
304  $redirectUrl = 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
306  }
307  }
308 
312  protected function getBackendUserAuthentication()
313  {
314  return $GLOBALS['BE_USER'];
315  }
316 
320  protected function getLanguageService()
321  {
322  return $GLOBALS['LANG'];
323  }
324 }
indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand=null)
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request,\TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:76
forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
static getRecord($table, $uid, $fields= '*', $where= '', $useDeleteClause=true)
injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
static translate($key, $extensionName, $arguments=null)
terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
addFlashMessage($messageBody, $messageTitle= '', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)