[TASK] Use ServerRequestInterface in LogoutController
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / LogoutController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller;
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
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Routing\UriBuilder;
21 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
22 use TYPO3\CMS\Core\Http\RedirectResponse;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Script Class for logging a user out.
27 * Does not display any content, just calls the logout-function for the current user and then makes a redirect.
28 */
29 class LogoutController
30 {
31 /**
32 * Injects the request object for the current request or subrequest
33 * As this controller goes only through the main() method, it is rather simple for now
34 * This will be split up in an abstract controller once proper routing/dispatcher is in place.
35 *
36 * @param ServerRequestInterface $request the current request
37 * @return ResponseInterface the response with the content
38 */
39 public function logoutAction(ServerRequestInterface $request): ResponseInterface
40 {
41 $this->processLogout();
42
43 $redirectUrl = $request->getParsedBody()['redirect'] ?? $request->getQueryParams()['redirect'];
44 $redirectUrl = GeneralUtility::sanitizeLocalUrl($redirectUrl);
45 if (empty($redirectUrl)) {
46 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
47 $redirectUrl = (string)$uriBuilder->buildUriFromRoute('login', [], $uriBuilder::ABSOLUTE_URL);
48 }
49 return new RedirectResponse(GeneralUtility::locationHeaderUrl($redirectUrl), 303);
50 }
51
52 /**
53 * Performs the logout processing
54 *
55 * @deprecated since v9, will be removed in v10
56 */
57 public function logout()
58 {
59 trigger_error('Method logout() will be replaced by protected method processLogout() in v10. Do not call from other extension', E_USER_DEPRECATED);
60 $this->processLogout();
61 }
62
63 /**
64 * Performs the logout processing
65 */
66 protected function processLogout(): void
67 {
68 if (empty($this->getBackendUser()->user['username'])) {
69 return;
70 }
71 // Logout written to log
72 $this->getBackendUser()->writelog(255, 2, 0, 1, 'User %s logged out from TYPO3 Backend', [$this->getBackendUser()->user['username']]);
73 /** @var \TYPO3\CMS\Core\FormProtection\BackendFormProtection $backendFormProtection */
74 $backendFormProtection = FormProtectionFactory::get();
75 $backendFormProtection->removeSessionTokenFromRegistry();
76 $this->getBackendUser()->logoff();
77 }
78
79 /**
80 * Returns the current BE user.
81 *
82 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
83 */
84 protected function getBackendUser()
85 {
86 return $GLOBALS['BE_USER'];
87 }
88 }