65678332c368f40c8929194561d4f63197c3a5a1
[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\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\HttpUtility;
21
22 /**
23 * Backend user switchback, for logoff_pre_processing hook within
24 * \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication class
25 */
26 class SwitchBackUserHook
27 {
28 /**
29 * Switch backend user session.
30 *
31 * @param array $params
32 * @param AbstractUserAuthentication $authentication
33 * @see AbstractUserAuthentication
34 */
35 public function switchBack($params, AbstractUserAuthentication $authentication)
36 {
37 if ($this->isAHandledBackendSession($authentication)) {
38 $objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
39 $backendUserSessionRepository = $objectManager->get(\TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository::class);
40 $backendUserSessionRepository->switchBackToOriginalUser($authentication);
41 HttpUtility::redirect(\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('main'));
42 }
43 }
44
45 /**
46 * Check if the given authentication object is a backend session and
47 * contains all necessary information to allow switching.
48 *
49 * @param AbstractUserAuthentication $authentication
50 * @return bool
51 */
52 protected function isAHandledBackendSession(AbstractUserAuthentication $authentication)
53 {
54 return ($authentication instanceof BackendUserAuthentication)
55 && is_array($authentication->user)
56 && (int)$authentication->user['uid'] > 0
57 && (int)$authentication->user['ses_backuserid'] > 0;
58 }
59 }