Passed
Push — master ( 42f7fa...76f43a )
by
unknown
14:32
created

FrontendUserAuthenticator::transferFrontendUserSession()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 3
dl 0
loc 25
rs 9.8666
c 1
b 0
f 0
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
18
namespace TYPO3\CMS\Frontend\Middleware;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
use TYPO3\CMS\Core\Context\Context;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
27
28
/**
29
 * This middleware authenticates a Frontend User (fe_users).
30
 */
31
class FrontendUserAuthenticator implements MiddlewareInterface
32
{
33
    /**
34
     * @var Context
35
     */
36
    protected $context;
37
38
    public function __construct(Context $context)
39
    {
40
        $this->context = $context;
41
    }
42
43
    /**
44
     * Creates a frontend user authentication object, tries to authenticate a user and stores
45
     * it in the current request as attribute.
46
     *
47
     * @param ServerRequestInterface $request
48
     * @param RequestHandlerInterface $handler
49
     * @return ResponseInterface
50
     */
51
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52
    {
53
        $frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
54
55
        // List of page IDs where to look for frontend user records
56
        $pid = $request->getParsedBody()['pid'] ?? $request->getQueryParams()['pid'] ?? 0;
57
        if ($pid) {
58
            $frontendUser->checkPid_value = implode(',', GeneralUtility::intExplode(',', $pid));
59
        }
60
61
        // Authenticate now
62
        $frontendUser->start();
63
        $frontendUser->unpack_uc();
64
        // no matter if we have an active user we try to fetch matching groups which can
65
        // be set without an user (simulation for instance!)
66
        $frontendUser->fetchGroupData();
67
68
        // Register the frontend user as aspect and within the request
69
        $userAspect = $frontendUser->createUserAspect();
70
        $this->context->setAspect('frontend.user', $userAspect);
71
        $request = $request->withAttribute('frontend.user', $frontendUser);
72
73
        $response = $handler->handle($request);
74
75
        // Store session data for fe_users if it still exists
76
        if ($frontendUser instanceof FrontendUserAuthentication) {
77
            $frontendUser->storeSessionData();
78
            if ($frontendUser->sendNoCacheHeaders) {
79
                $response = $this->applyHeadersToResponse($response);
80
            }
81
        }
82
83
        return $response;
84
    }
85
86
    /**
87
     * Adding headers to the response to avoid caching on the client side.
88
     * These headers will override any previous headers of these names sent.
89
     * Get the http headers to be sent if an authenticated user is available,
90
     * in order to disallow browsers to store the response on the client side.
91
     *
92
     * @param ResponseInterface $response
93
     * @return ResponseInterface the modified response object.
94
     */
95
    protected function applyHeadersToResponse(ResponseInterface $response): ResponseInterface
96
    {
97
        $headers = [
98
            'Expires' => 0,
99
            'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
100
            'Cache-Control' => 'no-cache, must-revalidate',
101
            'Pragma' => 'no-cache'
102
        ];
103
        foreach ($headers as $headerName => $headerValue) {
104
            $response = $response->withHeader($headerName, (string)$headerValue);
105
        }
106
        return $response;
107
    }
108
}
109