2f00c3957e94dad6cc596dc224d2295ebef476e0
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Hook / SwitchBackUserHook.php
1 <?php
2 namespace TYPO3\CMS\Beuser\Hook;
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\Core\Authentication\AbstractUserAuthentication;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\HttpUtility;
20
21 /**
22 * Backend user switchback, for logoff_pre_processing hook within
23 * \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication class
24 */
25 class SwitchBackUserHook {
26
27 /**
28 * Switch backend user session.
29 *
30 * @param array $params
31 * @param AbstractUserAuthentication $authentication
32 * @see AbstractUserAuthentication
33 * @return void
34 */
35 public function switchBack($params, AbstractUserAuthentication $authentication) {
36 if ($this->isAHandledBackendSession($authentication)) {
37 $objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
38 $backendUserSessionRepository = $objectManager->get(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository::class);
39 $backendUserSessionRepository->switchBackToOriginalUser($authentication);
40 HttpUtility::redirect($GLOBALS['BACK_PATH'] . 'backend.php');
41 }
42 }
43
44 /**
45 * Check if the given authentication object is a backend session and
46 * contains all necessary information to allow switching.
47 *
48 * @param AbstractUserAuthentication $authentication
49 * @return bool
50 */
51 protected function isAHandledBackendSession(AbstractUserAuthentication $authentication) {
52 if (
53 $authentication->session_table !== 'be_sessions'
54 || !is_array($authentication->user)
55 || !$authentication->user['uid']
56 || !$authentication->user['ses_backuserid']
57 ) {
58 return FALSE;
59 } else {
60 return TRUE;
61 }
62 }
63
64 }