[CLEANUP] Deprecate DataHandler property stripslashes_values
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Controller / BackendUserController.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
19
20 /**
21 * Backend module user administration controller
22 *
23 * @author Felix Kopp <felix-source@phorax.com>
24 */
25 class BackendUserController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
26
27 /**
28 * @var \TYPO3\CMS\Beuser\Domain\Model\ModuleData
29 */
30 protected $moduleData;
31
32 /**
33 * @var \TYPO3\CMS\Beuser\Service\ModuleDataStorageService
34 * @inject
35 */
36 protected $moduleDataStorageService;
37
38 /**
39 * @var \TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository
40 * @inject
41 */
42 protected $backendUserRepository;
43
44 /**
45 * @var \TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository
46 * @inject
47 */
48 protected $backendUserGroupRepository;
49
50 /**
51 * @var \TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository
52 * @inject
53 */
54 protected $backendUserSessionRepository;
55
56 /**
57 * Load and persist module data
58 *
59 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request
60 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response
61 * @return void
62 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
63 */
64 public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
65 $this->moduleData = $this->moduleDataStorageService->loadModuleData();
66 // We "finally" persist the module data.
67 try {
68 parent::processRequest($request, $response);
69 $this->moduleDataStorageService->persistModuleData($this->moduleData);
70 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
71 $this->moduleDataStorageService->persistModuleData($this->moduleData);
72 throw $e;
73 }
74 }
75
76 /**
77 * Initialize actions
78 *
79 * @return void
80 * @throws \RuntimeException
81 */
82 public function initializeAction() {
83 // @TODO: Extbase backend modules relies on frontend TypoScript for view, persistence
84 // and settings. Thus, we need a TypoScript root template, that then loads the
85 // ext_typoscript_setup.txt file of this module. This is nasty, but can not be
86 // circumvented until there is a better solution in extbase.
87 // For now we throw an exception if no settings are detected.
88 if (empty($this->settings)) {
89 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);
90 }
91 }
92
93 /**
94 * Displays all BackendUsers
95 * - Switch session to different user
96 *
97 * @param \TYPO3\CMS\Beuser\Domain\Model\Demand $demand
98 * @return void
99 */
100 public function indexAction(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand = NULL) {
101 if ($demand === NULL) {
102 $demand = $this->moduleData->getDemand();
103 } else {
104 $this->moduleData->setDemand($demand);
105 }
106 // Switch user until logout
107 $switchUser = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SwitchUser');
108 if ($switchUser > 0) {
109 $this->switchUser($switchUser);
110 }
111 $compareUserList = $this->moduleData->getCompareUserList();
112 $this->view->assign('demand', $demand);
113 $this->view->assign('returnUrl', rawurlencode(BackendUtility::getModuleUrl('system_BeuserTxBeuser')));
114 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
115 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
116 $this->view->assign('backendUsers', $this->backendUserRepository->findDemanded($demand));
117 $this->view->assign('backendUserGroups', array_merge(array(''), $this->backendUserGroupRepository->findAll()->toArray()));
118 $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
119 }
120
121 /**
122 * Views all currently logged in BackendUsers and their sessions
123 *
124 * @return void
125 */
126 public function onlineAction() {
127 $onlineUsersAndSessions = array();
128 $onlineUsers = $this->backendUserRepository->findOnline();
129 foreach ($onlineUsers as $onlineUser) {
130 $onlineUsersAndSessions[] = array(
131 'backendUser' => $onlineUser,
132 'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
133 );
134 }
135 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
136 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
137 $this->view->assign('onlineUsersAndSessions', $onlineUsersAndSessions);
138 $this->view->assign('currentSessionId', $GLOBALS['BE_USER']->user['ses_id']);
139 }
140
141 /**
142 * Compare backend users from demand
143 *
144 * @return void
145 */
146 public function compareAction() {
147 $compareUserList = $this->moduleData->getCompareUserList();
148 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
149 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
150 $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
151 }
152
153 /**
154 * Attaches one backend user to the compare list
155 *
156 * @param int $uid
157 * @return void
158 */
159 public function addToCompareListAction($uid) {
160 $this->moduleData->attachUidCompareUser($uid);
161 $this->moduleDataStorageService->persistModuleData($this->moduleData);
162 $this->forward('index');
163 }
164
165 /**
166 * Removes given backend user to the compare list
167 *
168 * @param int $uid
169 * @return void
170 */
171 public function removeFromCompareListAction($uid) {
172 $this->moduleData->detachUidCompareUser($uid);
173 $this->moduleDataStorageService->persistModuleData($this->moduleData);
174 $this->forward('index');
175 }
176
177 /**
178 * Terminate BackendUser session and logout corresponding client
179 * Redirects to onlineAction with message
180 *
181 * @param \TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser
182 * @param string $sessionId
183 * @return void
184 */
185 protected function terminateBackendUserSessionAction(\TYPO3\CMS\Beuser\Domain\Model\BackendUser $backendUser, $sessionId) {
186 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
187 'be_sessions',
188 'ses_userid = "' . (int)$backendUser->getUid() . '" AND ses_id = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($sessionId, 'be_sessions') . ' LIMIT 1'
189 );
190 if ($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
191 $this->addFlashMessage(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser'));
192 }
193 $this->forward('online');
194 }
195
196 /**
197 * Switches to a given user (SU-mode) and then redirects to the start page of the backend to refresh the navigation etc.
198 *
199 * @param string $switchUser BE-user record that will be switched to
200 * @return void
201 */
202 protected function switchUser($switchUser) {
203 $targetUser = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('be_users', $switchUser);
204 if (is_array($targetUser) && $GLOBALS['BE_USER']->isAdmin()) {
205 $updateData['ses_userid'] = (int)$targetUser['uid'];
206 $updateData['ses_backuserid'] = (int)$GLOBALS['BE_USER']->user['uid'];
207
208 // Set backend user listing module as starting module for switchback
209 $GLOBALS['BE_USER']->uc['startModuleOnFirstLogin'] = 'system_BeuserTxBeuser';
210 $GLOBALS['BE_USER']->writeUC();
211
212 $whereClause = 'ses_id=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['BE_USER']->id, 'be_sessions');
213 $whereClause .= ' AND ses_name=' . $GLOBALS['TYPO3_DB']->fullQuoteStr(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::getCookieName(), 'be_sessions');
214 $whereClause .= ' AND ses_userid=' . (int)$GLOBALS['BE_USER']->user['uid'];
215
216 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
217 'be_sessions',
218 $whereClause,
219 $updateData
220 );
221
222 $redirectUrl = $GLOBALS['BACK_PATH'] . 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
223 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
224 }
225 }
226
227 }