‪TYPO3CMS  11.5
BackendUserAuthenticator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
31 
41 {
43 
44  public function ‪__construct(
47  ) {
48  parent::__construct(‪$context);
49  $this->languageServiceFactory = ‪$languageServiceFactory;
50  }
51 
59  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
60  {
61  // Initializing a possible logged-in Backend User
62  // If the backend cookie is set,
63  // we proceed and check if a backend user is logged in.
64  $backendUserObject = null;
65  if (isset($request->getCookieParams()[‪BackendUserAuthentication::getCookieName()])) {
66  $backendUserObject = $this->‪initializeBackendUser($request);
67  }
68  ‪$GLOBALS['BE_USER'] = $backendUserObject;
69  // Load specific dependencies which are necessary for a valid Backend User
70  // like $GLOBALS['LANG'] for labels in the language of the BE User, the router, and ext_tables.php for all modules
71  // So things like Frontend Editing and Admin Panel can use this for generating links to the TYPO3 Backend.
72  if (‪$GLOBALS['BE_USER'] instanceof ‪FrontendBackendUserAuthentication) {
73  ‪$GLOBALS['LANG'] = $this->languageServiceFactory->createFromUserPreferences(‪$GLOBALS['BE_USER']);
75  $this->‪setBackendUserAspect(‪$GLOBALS['BE_USER']);
76  }
77 
78  $response = $handler->handle($request);
79 
80  // If, when building the response, the user is still available, then ensure that the headers are sent properly
81  if ($this->context->getAspect('backend.user')->isLoggedIn()) {
82  return $this->‪applyHeadersToResponse($response);
83  }
84  return $response;
85  }
86 
94  protected function ‪initializeBackendUser(ServerRequestInterface $request)
95  {
96  // New backend user object
97  $backendUserObject = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class);
98  try {
99  $backendUserObject->start($request);
100  } catch (‪MfaRequiredException $e) {
101  // Do nothing, as the user is not fully authenticated - has not
102  // passed required multi-factor authentication - via the backend.
103  return null;
104  }
105  $backendUserObject->unpack_uc();
106  if (!empty($backendUserObject->user['uid'])) {
107  $this->‪setBackendUserAspect($backendUserObject, (int)$backendUserObject->user['workspace_id']);
108  $backendUserObject->fetchGroupData();
109  }
110  // Unset the user initialization if any setting / restriction applies
111  if (!$this->‪isAuthenticated($backendUserObject, $request->getAttribute('normalizedParams'))) {
112  $backendUserObject = null;
113  $this->‪setBackendUserAspect(null);
114  }
115  return $backendUserObject;
116  }
117 
126  {
127  // Check IP
128  $ipMask = trim(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['IPmaskList'] ?? '');
129  if ($ipMask && !GeneralUtility::cmpIP($normalizedParams->‪getRemoteAddress(), $ipMask)) {
130  return false;
131  }
132  // Check SSL (https)
133  if ((bool)‪$GLOBALS['TYPO3_CONF_VARS']['BE']['lockSSL'] && !$normalizedParams->‪isHttps()) {
134  return false;
135  }
136  return $user->‪backendCheckLogin();
137  }
138 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator
Definition: BackendUserAuthenticator.php:41
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication
Definition: FrontendBackendUserAuthentication.php:31
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator\setBackendUserAspect
‪setBackendUserAspect(?BackendUserAuthentication $user, int $alternativeWorkspaceId=null)
Definition: BackendUserAuthenticator.php:94
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\initializeBackendUser
‪FrontendBackendUserAuthentication null initializeBackendUser(ServerRequestInterface $request)
Definition: BackendUserAuthenticator.php:94
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\$languageServiceFactory
‪LanguageServiceFactory $languageServiceFactory
Definition: BackendUserAuthenticator.php:42
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getCookieName
‪static string getCookieName()
Definition: BackendUserAuthentication.php:2088
‪TYPO3\CMS\Core\Http\NormalizedParams\getRemoteAddress
‪string getRemoteAddress()
Definition: NormalizedParams.php:417
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication\backendCheckLogin
‪bool backendCheckLogin($proceedIfNoUserIsLoggedIn=null)
Definition: FrontendBackendUserAuthentication.php:70
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator\$context
‪Context $context
Definition: BackendUserAuthenticator.php:52
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator
Definition: BackendUserAuthenticator.php:49
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\isAuthenticated
‪bool isAuthenticated(FrontendBackendUserAuthentication $user, NormalizedParams $normalizedParams)
Definition: BackendUserAuthenticator.php:125
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\__construct
‪__construct(Context $context, LanguageServiceFactory $languageServiceFactory)
Definition: BackendUserAuthenticator.php:44
‪TYPO3\CMS\Core\Authentication\Mfa\MfaRequiredException
Definition: MfaRequiredException.php:29
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator\applyHeadersToResponse
‪ResponseInterface applyHeadersToResponse(ResponseInterface $response)
Definition: BackendUserAuthenticator.php:73
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\NormalizedParams\isHttps
‪bool isHttps()
Definition: NormalizedParams.php:337
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: BackendUserAuthenticator.php:59
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Core\Core\Bootstrap\loadExtTables
‪static loadExtTables(bool $allowCaching=true, FrontendInterface $coreCache=null)
Definition: Bootstrap.php:532
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35