Passed
Push — master ( b612b9...54c255 )
by Rafael
43:43
created

FrontendUserAuthenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ApacheSolrForTypo3\Solr\Middleware;
5
6
/***************************************************************
7
 * Copyright notice
8
 *
9
 * (c) 2020 dkd Internet Services GmbH <[email protected]>
10
 * All rights reserved
11
 *
12
 * This script is part of the TYPO3 project. The TYPO3 project is
13
 * free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 3 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * The GNU General Public License can be found at
19
 * http://www.gnu.org/copyleft/gpl.html.
20
 * A copy is found in the text file GPL.txt and important notices to the license
21
 * from the author is found in LICENSE.txt distributed with these scripts.
22
 *
23
 *
24
 * This script is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU General Public License for more details.
28
 *
29
 * This copyright notice MUST APPEAR in all copies of the script!
30
 ***************************************************************/
31
32
use ApacheSolrForTypo3\Solr\Access\Rootline;
33
use ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper\AuthorizationService;
34
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
35
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequestHandler;
36
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
37
use ApacheSolrForTypo3\Solr\Util;
38
use Psr\Http\Message\ResponseInterface;
39
use Psr\Http\Message\ServerRequestInterface;
40
use Psr\Http\Server\MiddlewareInterface;
41
use Psr\Http\Server\RequestHandlerInterface;
42
use TYPO3\CMS\Core\Context\Context;
43
use TYPO3\CMS\Core\Context\UserAspect;
44
use TYPO3\CMS\Core\Http\JsonResponse;
45
use TYPO3\CMS\Core\Utility\GeneralUtility;
46
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
47
48
/**
49
 * Class FrontendUserAuthenticator is responsible to fake a frontend user and fe_groups.
50
 * It makes possible to Index access restricted pages and content elements.
51
 */
52
class FrontendUserAuthenticator implements MiddlewareInterface
53
{
54
55
    /**
56
     * @var Context
57
     */
58
    protected $context;
59
60
    /**
61
     * FrontendUserAuthorizator constructor.
62
     *
63
     * @param Context|null $context
64
     * @noinspection PhpUnused
65
     */
66
    public function __construct(?Context $context = null)
67
    {
68
        $this->context = $context ?? GeneralUtility::makeInstance(Context::class);
69
    }
70
71
    /**
72
     * @param ServerRequestInterface $request
73
     * @param RequestHandlerInterface $handler
74
     * @return ResponseInterface
75
     * @noinspection PhpUnused
76
     */
77
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
78
    {
79
        if (Util::getIsTYPO3VersionBelow10()
80
            || !$request->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER)
81
        ) {
82
            return $handler->handle($request);
83
        }
84
85
        // disable TSFE cache for TYPO3 v10
86
        $request = $request->withAttribute('noCache', true);
87
        $jsonEncodedParameters = $request->getHeader(PageIndexerRequest::SOLR_INDEX_HEADER)[0];
88
89
        /* @var PageIndexerRequestHandler $pageIndexerRequestHandler */
90
        $pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class, $jsonEncodedParameters);
91
        if (!$pageIndexerRequestHandler->getRequest()->isAuthenticated()) {
92
            /* @var SolrLogManager $logger */
93
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, self::class);
94
            $logger->log(
95
                SolrLogManager::ERROR,
96
                'Invalid Index Queue Frontend Request detected!',
97
                [
98
                    'page indexer request' => (array)$pageIndexerRequestHandler->getRequest(),
99
                    'index queue header' => $jsonEncodedParameters
100
                ]
101
            );
102
103
            return new JsonResponse(['error' => ['code' => 403, 'message' => 'Invalid Index Queue Request.']], 403);
104
105
        }
106
        $request = $this->tryToAuthenticateFrontendUser($pageIndexerRequestHandler, $request);
107
108
        return $handler->handle($request);
109
    }
110
111
    /**
112
     * Fakes a logged in user to retrieve access restricted content.
113
     *
114
     * @param PageIndexerRequestHandler $handler
115
     * @param ServerRequestInterface $request
116
     * @return ServerRequestInterface
117
     * @noinspection PhpUnused
118
     */
119
    protected function tryToAuthenticateFrontendUser(PageIndexerRequestHandler $handler, ServerRequestInterface $request): ServerRequestInterface
120
    {
121
        $accessRootline = $this->getAccessRootline($handler);
122
        $stringAccessRootline = (string)$accessRootline;
123
124
        if (empty($stringAccessRootline)) {
125
            return $request;
126
        }
127
128
        $groups = $accessRootline->getGroups();
129
130
        /* @var FrontendUserAuthentication $feUser */
131
        $feUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
132
        $feUser->user[$feUser->username_column] = AuthorizationService::SOLR_INDEXER_USERNAME;
133
        /* @noinspection PhpParamsInspection */
134
        $this->context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $feUser, $groups));
135
        $request = $request->withAttribute('frontend.user', $feUser);
136
137
        return $request;
138
    }
139
140
    /**
141
     * Gets the access rootline as defined by the request.
142
     *
143
     * @param PageIndexerRequestHandler $handler
144
     * @return Rootline The access rootline to use for indexing.
145
     */
146
    protected function getAccessRootline(PageIndexerRequestHandler $handler): Rootline
147
    {
148
        $stringAccessRootline = '';
149
150
        if ($handler->getRequest()->getParameter('accessRootline')) {
151
            $stringAccessRootline = $handler->getRequest()->getParameter('accessRootline');
152
        }
153
154
        /* @noinspection PhpIncompatibleReturnTypeInspection */
155
        return GeneralUtility::makeInstance(Rootline::class, /** @scrutinizer ignore-type */ $stringAccessRootline);
156
    }
157
158
}
159