1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AOE\Crawler\Middleware; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* (c) 2020 AOE GmbH <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This file is part of the TYPO3 Crawler Extension. |
11
|
|
|
* |
12
|
|
|
* It is free software; you can redistribute it and/or modify it under |
13
|
|
|
* the terms of the GNU General Public License, either version 2 |
14
|
|
|
* of the License, or any later version. |
15
|
|
|
* |
16
|
|
|
* For the full copyright and license information, please read the |
17
|
|
|
* LICENSE.txt file that was distributed with this source code. |
18
|
|
|
* |
19
|
|
|
* The TYPO3 project - inspiring people to share! |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use AOE\Crawler\Converter\JsonCompatibilityConverter; |
23
|
|
|
use AOE\Crawler\Domain\Repository\QueueRepository; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
27
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
28
|
|
|
use TYPO3\CMS\Core\Context\Context; |
29
|
|
|
use TYPO3\CMS\Core\Context\UserAspect; |
30
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
31
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
32
|
|
|
use TYPO3\CMS\Frontend\Controller\ErrorController; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Evaluates HTTP headers and checks if Crawler should register itself. |
36
|
|
|
*/ |
37
|
|
|
class FrontendUserAuthenticator implements MiddlewareInterface |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $headerName = 'X-T3CRAWLER'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Context |
46
|
|
|
*/ |
47
|
|
|
protected $context; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var QueueRepository |
51
|
|
|
*/ |
52
|
|
|
protected $queueRepository; |
53
|
|
|
|
54
|
|
|
public function __construct(?Context $context = null) |
55
|
|
|
{ |
56
|
|
|
$this->context = $context ?? GeneralUtility::makeInstance(Context::class); |
57
|
|
|
$this->queueRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(QueueRepository::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException |
62
|
|
|
* @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException |
63
|
|
|
*/ |
64
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
/** @var JsonCompatibilityConverter $jsonCompatibilityConverter */ |
68
|
|
|
$jsonCompatibilityConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class); |
69
|
|
|
|
70
|
|
|
$crawlerInformation = $request->getHeaderLine($this->headerName) ?? null; |
71
|
|
|
if (empty($crawlerInformation)) { |
72
|
|
|
return $handler->handle($request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Authenticate crawler request: |
76
|
|
|
//@todo: ask service to exclude current call for special reasons: for example no relevance because the language version is not affected |
77
|
|
|
[$queueId, $hash] = explode(':', $crawlerInformation); |
78
|
|
|
$queueRec = $this->queueRepository->findByQueueId($queueId); |
79
|
|
|
|
80
|
|
|
// If a crawler record was found and hash was matching, set it up |
81
|
|
|
if (! $this->isRequestHashMatchingQueueRecord($queueRec, $hash)) { |
82
|
|
|
return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction($request, 'No crawler entry found'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$queueParameters = $jsonCompatibilityConverter->convert($queueRec['parameters']); |
86
|
|
|
$request = $request->withAttribute('tx_crawler', $queueParameters); |
87
|
|
|
|
88
|
|
|
// Now ensure to set the proper user groups |
89
|
|
|
$grList = $queueParameters['feUserGroupList']; |
90
|
|
|
if ($grList) { |
91
|
|
|
$frontendUser = $GLOBALS['TSFE']->fe_user; |
92
|
|
|
$frontendUser->user[$frontendUser->usergroup_column] = $grList; |
93
|
|
|
// we have to set the fe user group to the user aspect since indexed_search only reads the user aspect |
94
|
|
|
// to get the groups. otherwise groups are ignored during indexing. |
95
|
|
|
// we need to add the groups 0, and -2 too, like the getGroupIds getter does. |
96
|
|
|
$this->context->setAspect( |
97
|
|
|
'frontend.user', |
98
|
|
|
GeneralUtility::makeInstance( |
99
|
|
|
UserAspect::class, |
100
|
|
|
$frontendUser, |
101
|
|
|
explode(',', '0,-2,' . $grList) |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $handler->handle($request); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
protected function isRequestHashMatchingQueueRecord(?array $queueRec, string $hash): bool |
110
|
|
|
{ |
111
|
3 |
|
return is_array($queueRec) && hash_equals($hash, md5($queueRec['qid'] . '|' . $queueRec['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|