c65e315a5a44979c33ff16384ffcfecdb46f032f
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Controller / BackendUserController.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Felix Kopp <felix-source@phorax.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Backend module user administration controller
29 *
30 * @author Felix Kopp <felix-source@phorax.com>
31 * @package TYPO3
32 * @subpackage beuser
33 */
34 class Tx_Beuser_Controller_BackendUserController extends Tx_Extbase_MVC_Controller_ActionController {
35
36 /**
37 * @var Tx_Beuser_Domain_Model_ModuleData
38 */
39 protected $moduleData;
40
41 /**
42 * @var Tx_Beuser_Service_ModuleDataStorageService
43 * @inject
44 */
45 protected $moduleDataStorageService;
46
47 /**
48 * @var Tx_Beuser_Domain_Repository_BackendUserRepository
49 * @inject
50 */
51 protected $backendUserRepository;
52
53 /**
54 * @var Tx_Beuser_Domain_Repository_BackendUserGroupRepository
55 * @inject
56 */
57 protected $backendUserGroupRepository;
58
59 /**
60 * @var Tx_Beuser_Domain_Repository_BackendUserSessionRepository
61 * @inject
62 */
63 protected $backendUserSessionRepository;
64
65 /**
66 * Load and persist module data
67 *
68 * @param Tx_Extbase_MVC_RequestInterface $request
69 * @param Tx_Extbase_MVC_ResponseInterface $response
70 * @throws Tx_Extbase_MVC_Exception_StopAction
71 * @return void
72 */
73 public function processRequest(Tx_Extbase_MVC_RequestInterface $request, Tx_Extbase_MVC_ResponseInterface $response) {
74 $this->moduleData = $this->moduleDataStorageService->loadModuleData();
75
76 // We "finally" persist the module data.
77 try {
78 parent::processRequest($request, $response);
79 $this->moduleDataStorageService->persistModuleData($this->moduleData);
80 } catch (Tx_Extbase_MVC_Exception_StopAction $e) {
81 $this->moduleDataStorageService->persistModuleData($this->moduleData);
82 throw $e;
83 }
84 }
85
86 /**
87 * Displays all BackendUsers
88 * - Switch session to different user
89 *
90 * @param Tx_Beuser_Domain_Model_Demand $demand
91 * @return void
92 */
93 public function indexAction(Tx_Beuser_Domain_Model_Demand $demand = NULL) {
94 if ($demand === NULL) {
95 $demand = $this->moduleData->getDemand();
96 } else {
97 $this->moduleData->setDemand($demand);
98 }
99
100 // Switch user permanently or only until logout
101 if (t3lib_div::_GP('SwitchUser')) {
102 $this->switchUser(t3lib_div::_GP('SwitchUser'), t3lib_div::_GP('switchBackUser'));
103 }
104
105 $compareUserList = $this->moduleData->getCompareUserList();
106
107 $this->view->assign('demand', $demand);
108 $this->view->assign('returnUrl', 'mod.php?M=tools_BeuserTxBeuser');
109 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
110 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
111 $this->view->assign('backendUsers', $this->backendUserRepository->findDemanded($demand));
112 $this->view->assign('backendUserGroups', array_merge(array(''), $this->backendUserGroupRepository->findAll()->toArray()));
113 $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
114 }
115
116 /**
117 * Views all currently logged in BackendUsers and their sessions
118 *
119 * @return void
120 */
121 public function onlineAction() {
122 $onlineUsersAndSessions = array();
123 $onlineUsers = $this->backendUserRepository->findOnline();
124 foreach ($onlineUsers as $onlineUser) {
125 $onlineUsersAndSessions[] = array(
126 'backendUser' => $onlineUser,
127 'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
128 );
129 }
130
131 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
132 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
133 $this->view->assign('onlineUsersAndSessions', $onlineUsersAndSessions);
134 $this->view->assign('currentSessionId', $GLOBALS['BE_USER']->user['ses_id']);
135 }
136
137 /**
138 * Compare backend users from demand
139 *
140 * @return void
141 */
142 public function compareAction() {
143 $compareUserList = $this->moduleData->getCompareUserList();
144
145 $this->view->assign('dateFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy']);
146 $this->view->assign('timeFormat', $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']);
147 $this->view->assign('compareUserList', !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '');
148 }
149
150 /**
151 * Attaches one backend user to the compare list
152 *
153 * @param integer $uid
154 * @retun void
155 */
156 public function addToCompareListAction($uid) {
157 $this->moduleData->attachUidCompareUser($uid);
158 $this->forward('index');
159 }
160
161 /**
162 * Removes given backend user to the compare list
163 *
164 * @param integer $uid
165 * @retun void
166 */
167 public function removeFromCompareListAction($uid) {
168 $this->moduleData->detachUidCompareUser($uid);
169 $this->forward('index');
170 }
171
172 /**
173 * Terminate BackendUser session and logout corresponding client
174 * Redirects to onlineAction with message
175 *
176 * @param Tx_Beuser_Domain_Model_BackendUser $backendUser
177 * @param string $sessionId
178 * @return void
179 */
180 protected function terminateBackendUserSessionAction(Tx_Beuser_Domain_Model_BackendUser $backendUser, $sessionId) {
181 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
182 'be_sessions',
183 'ses_userid = "' . $backendUser->getUid() . '"' .
184 ' AND ses_id = "' . $sessionId . '"' .
185 ' LIMIT 1'
186 );
187
188 if ($GLOBALS['TYPO3_DB']->sql_affected_rows() == 1) {
189 $message = 'Session successfully terminated.';
190 $this->flashMessageContainer->add($message, '', t3lib_FlashMessage::OK);
191 }
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 array $switchUser BE-user record that will be switched to
200 * @param boolean $switchBack
201 * @return void
202 */
203 protected function switchUser($switchUser, $switchBack = FALSE) {
204 $targetUser = t3lib_BEfunc::getRecord('be_users', $switchUser);
205 if (is_array($targetUser) && $GLOBALS['BE_USER']->isAdmin()) {
206 $updateData['ses_userid'] = $targetUser['uid'];
207
208 // User switchback or replace current session?
209 if ($switchBack) {
210 $updateData['ses_backuserid'] = intval($GLOBALS['BE_USER']->user['uid']);
211 }
212 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('be_sessions', 'ses_id=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['BE_USER']->id, 'be_sessions') . ' AND ses_name=' . $GLOBALS['TYPO3_DB']->fullQuoteStr(t3lib_beUserAuth::getCookieName(), 'be_sessions') . ' AND ses_userid=' . intval($GLOBALS['BE_USER']->user['uid']), $updateData);
213
214 $redirectUrl = $GLOBALS['BACK_PATH'] . 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
215 t3lib_utility_Http::redirect($redirectUrl);
216 }
217 }
218
219 }
220
221 ?>