TYPO3  7.6
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 
22 
27 {
31  protected $moduleData;
32 
37 
42 
47 
52 
56  public function injectModuleDataStorageService(\TYPO3\CMS\Beuser\Service\ModuleDataStorageService $moduleDataStorageService)
57  {
58  $this->moduleDataStorageService = $moduleDataStorageService;
59  }
60 
64  public function injectBackendUserRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository $backendUserRepository)
65  {
66  $this->backendUserRepository = $backendUserRepository;
67  }
68 
72  public function injectBackendUserGroupRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository $backendUserGroupRepository)
73  {
74  $this->backendUserGroupRepository = $backendUserGroupRepository;
75  }
76 
80  public function injectBackendUserSessionRepository(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository $backendUserSessionRepository)
81  {
82  $this->backendUserSessionRepository = $backendUserSessionRepository;
83  }
84 
93  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
94  {
95  $this->moduleData = $this->moduleDataStorageService->loadModuleData();
96  // We "finally" persist the module data.
97  try {
98  parent::processRequest($request, $response);
99  $this->moduleDataStorageService->persistModuleData($this->moduleData);
100  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
101  $this->moduleDataStorageService->persistModuleData($this->moduleData);
102  throw $e;
103  }
104  }
105 
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 
131  public function indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand = null)
132  {
133  if ($demand === null) {
134  $demand = $this->moduleData->getDemand();
135  } else {
136  $this->moduleData->setDemand($demand);
137  }
138  // Switch user until logout
139  $switchUser = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SwitchUser');
140  if ($switchUser > 0) {
141  $this->switchUser($switchUser);
142  }
143  $compareUserList = $this->moduleData->getCompareUserList();
144 
145  // Create online user list for easy parsing
146  $onlineUsers = $this->backendUserSessionRepository->findAllActive();
147  $onlineBackendUsers = array();
148  if (is_array($onlineUsers)) {
149  foreach ($onlineUsers as $onlineUser) {
150  $onlineBackendUsers[$onlineUser['ses_userid']] = true;
151  }
152  }
153  $this->view->assign('onlineBackendUsers', $onlineBackendUsers);
154 
155  $this->view->assign('demand', $demand);
156  $this->view->assign('returnUrl', rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser')));
157  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
158  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
159  $this->view->assign('backendUsers', $this->backendUserRepository->findDemanded($demand));
160  $this->view->assign('backendUserGroups', array_merge(array(''), $this->backendUserGroupRepository->findAll()->toArray()));
161  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
162  }
163 
169  public function onlineAction()
170  {
171  $onlineUsersAndSessions = array();
172  $onlineUsers = $this->backendUserRepository->findOnline();
173  foreach ($onlineUsers as $onlineUser) {
174  $onlineUsersAndSessions[] = array(
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 
190  public function compareAction()
191  {
192  $compareUserList = $this->moduleData->getCompareUserList();
193  $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
194  $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
195  $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
196  }
197 
204  public function addToCompareListAction($uid)
205  {
206  $this->moduleData->attachUidCompareUser($uid);
207  $this->moduleDataStorageService->persistModuleData($this->moduleData);
208  $this->forward('index');
209  }
210 
217  public function removeFromCompareListAction($uid)
218  {
219  $this->moduleData->detachUidCompareUser($uid);
220  $this->moduleDataStorageService->persistModuleData($this->moduleData);
221  $this->forward('index');
222  }
223 
232  protected function terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId)
233  {
234  $this->getDatabaseConnection()->exec_DELETEquery(
235  'be_sessions',
236  'ses_userid = "' . (int)$backendUser->getUid() . '" AND ses_id = ' . $this->getDatabaseConnection()->fullQuoteStr($sessionId, 'be_sessions') . ' LIMIT 1'
237  );
238  if ($this->getDatabaseConnection()->sql_affected_rows() == 1) {
239  $this->addFlashMessage(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser'));
240  }
241  $this->forward('online');
242  }
243 
250  protected function switchUser($switchUser)
251  {
252  $targetUser = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('be_users', $switchUser);
253  if (is_array($targetUser) && $this->getBackendUserAuthentication()->isAdmin()) {
254  $updateData['ses_userid'] = (int)$targetUser['uid'];
255  $updateData['ses_backuserid'] = (int)$this->getBackendUserAuthentication()->user['uid'];
256 
257  // Set backend user listing module as starting module for switchback
258  $this->getBackendUserAuthentication()->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
259  $this->getBackendUserAuthentication()->writeUC();
260 
261  $whereClause = 'ses_id=' . $this->getDatabaseConnection()->fullQuoteStr($this->getBackendUserAuthentication()->id, 'be_sessions');
262  $whereClause .= ' AND ses_name=' . $this->getDatabaseConnection()->fullQuoteStr(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::getCookieName(), 'be_sessions');
263  $whereClause .= ' AND ses_userid=' . (int)$this->getBackendUserAuthentication()->user['uid'];
264 
265  $this->getDatabaseConnection()->exec_UPDATEquery(
266  'be_sessions',
267  $whereClause,
268  $updateData
269  );
270 
271  $redirectUrl = 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
272  \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
273  }
274  }
275 
279  protected function getDatabaseConnection()
280  {
281  return $GLOBALS['TYPO3_DB'];
282  }
283 
287  protected function getBackendUserAuthentication()
288  {
289  return $GLOBALS['BE_USER'];
290  }
291 
295  protected function getLanguageService()
296  {
297  return $GLOBALS['LANG'];
298  }
299 }