[TASK] Decouple adminPanel from frontend
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / BackendUserAuthenticator.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Middleware;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
24 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
25 use TYPO3\CMS\Core\Core\Bootstrap;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * This middleware authenticates a Backend User (be_user) (pre)-viewing a frontend page.
30 *
31 * This middleware also ensures that $GLOBALS['LANG'] is available, however it is possible that
32 * a different middleware later-on might unset the BE_USER as he/she is not allowed to preview a certain
33 * page due to rights management. As this can only happen once the page ID is resolved, this will happen
34 * after the routing middleware.
35 *
36 * Currently, this middleware depends on the availability of $GLOBALS['TSFE'], however, this is solely
37 * due to backwards-compatibility and will be disabled in the future.
38 */
39 class BackendUserAuthenticator implements MiddlewareInterface
40 {
41 /**
42 * Creates a frontend user authentication object, tries to authenticate a user
43 * and stores the object in $GLOBALS['TSFE']->fe_user.
44 *
45 * @param ServerRequestInterface $request
46 * @param RequestHandlerInterface $handler
47 * @return ResponseInterface
48 */
49 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
50 {
51 // PRE BE_USER HOOK
52 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preBeUser'] ?? [] as $_funcRef) {
53 $_params = [];
54 GeneralUtility::callUserFunction($_funcRef, $_params, $GLOBALS['TSFE']);
55 }
56
57 // Initializing a possible logged-in Backend User
58 // If the backend cookie is set,
59 // we proceed and check if a backend user is logged in.
60 $GLOBALS['TSFE']->beUserLogin = false;
61 $backendUserObject = null;
62 if (isset($request->getCookieParams()[BackendUserAuthentication::getCookieName()])) {
63 $backendUserObject = $this->initializeBackendUser();
64 // If the user is active now, let the controller know
65 if ($backendUserObject instanceof FrontendBackendUserAuthentication && !empty($backendUserObject->user['uid'])) {
66 $GLOBALS['TSFE']->beUserLogin = true;
67 }
68 }
69
70 $GLOBALS['BE_USER'] = $backendUserObject;
71
72 // POST BE_USER HOOK
73 $_params = [
74 'BE_USER' => &$GLOBALS['BE_USER']
75 ];
76 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['postBeUser'] ?? [] as $_funcRef) {
77 GeneralUtility::callUserFunction($_funcRef, $_params, $GLOBALS['TSFE']);
78 }
79
80 // Load specific dependencies which are necessary for a valid Backend User
81 // like $GLOBALS['LANG'] for labels in the language of the BE User, the router, and ext_tables.php for all modules
82 // So things like Frontend Editing and Admin Panel can use this for generating links to the TYPO3 Backend.
83 if ($GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
84 Bootstrap::initializeLanguageObject();
85 Bootstrap::initializeBackendRouter();
86 Bootstrap::loadExtTables();
87 }
88
89 return $handler->handle($request);
90 }
91
92 /**
93 * Creates the backend user object and returns it.
94 *
95 * @return FrontendBackendUserAuthentication|null the backend user object or null if there was no valid user found
96 */
97 public function initializeBackendUser()
98 {
99 // New backend user object
100 $backendUserObject = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class);
101 $backendUserObject->start();
102 $backendUserObject->unpack_uc();
103 if (!empty($backendUserObject->user['uid'])) {
104 $backendUserObject->fetchGroupData();
105 }
106 // Unset the user initialization if any setting / restriction applies
107 if (!$backendUserObject->checkBackendAccessSettingsFromInitPhp() || empty($backendUserObject->user['uid'])) {
108 $backendUserObject = null;
109 }
110 return $backendUserObject;
111 }
112 }