Skip to content

Commit

Permalink
[!!!][TASK] Rework frontend indexing helpers
Browse files Browse the repository at this point in the history
This change cleans up a few parts within the FrontendHelper
logic when indexing a page in the frontend:

- The PageIndexerInitialization and PageIndexerFinisher middleware
  are now merged.

- The PageIndexerRequestHandler contains no state

- The PageIndexerRequest is added to the PSR-7 Request as attribute
  "solr.pageIndexingInstructions" so it
  can be retrieved in various places.

- The AbstractFrontendHelper is removed, as the FrontendHelper
  middleware is thinned out. Activation and Deactivation rather
  works as initialization and flushing

This allows us to use DI in various places we hadn't had a chance
before.

In addition, the AuthorizationService getGroupsFE is removed
as it has been already replaced by a PSR-14 event.

AuthorizationService is now registered at any time
and now listens properly to the request object.

Relates: #3376
  • Loading branch information
bmack authored Jun 5, 2023
1 parent d061e00 commit 1a926ce
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 452 deletions.
34 changes: 11 additions & 23 deletions Classes/EventListener/PageIndexer/FrontendGroupsModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
use ApacheSolrForTypo3\Solr\Access\Rootline;
use ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper\AuthorizationService;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequestHandler;
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -42,23 +39,19 @@ class FrontendGroupsModifier
*/
public function __invoke(ModifyResolvedFrontendGroupsEvent $event): void
{
if (!$event->getRequest()->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER)) {
$pageIndexerRequest = $event->getRequest()->getAttribute('solr.pageIndexingInstructions');
if (!$pageIndexerRequest instanceof PageIndexerRequest) {
return;
}

$jsonEncodedParameters = $event->getRequest()->getHeader(PageIndexerRequest::SOLR_INDEX_HEADER)[0];
/** @var PageIndexerRequestHandler $pageIndexerRequestHandler */
$pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class, $jsonEncodedParameters);

if (!$pageIndexerRequestHandler->getRequest()->isAuthenticated()) {
if (!$pageIndexerRequest->isAuthenticated()) {
/** @var SolrLogManager $logger */
$logger = GeneralUtility::makeInstance(SolrLogManager::class, self::class);
$logger->log(
SolrLogManager::ERROR,
'Invalid Index Queue Frontend Request detected!',
[
'page indexer request' => (array)$pageIndexerRequestHandler->getRequest(),
'index queue header' => $jsonEncodedParameters,
'page indexer request' => (array)$pageIndexerRequest,
'index queue header' => $event->getRequest()->getHeader(PageIndexerRequest::SOLR_INDEX_HEADER)[0],
]
);
throw new PropagateResponseException(
Expand All @@ -75,7 +68,7 @@ public function __invoke(ModifyResolvedFrontendGroupsEvent $event): void
);
}

$groups = $this->resolveFrontendUserGroups($event->getRequest());
$groups = $this->resolveFrontendUserGroups($pageIndexerRequest);
$groupData = [];
foreach ($groups as $groupUid) {
if (in_array($groupUid, [-2, -1])) {
Expand All @@ -94,9 +87,9 @@ public function __invoke(ModifyResolvedFrontendGroupsEvent $event): void
/**
* Resolves a logged in fe_groups to retrieve access restricted content.
*/
protected function resolveFrontendUserGroups(ServerRequestInterface $request): array
protected function resolveFrontendUserGroups(PageIndexerRequest $pageIndexerRequest): array
{
$accessRootline = $this->getAccessRootline($request);
$accessRootline = $this->getAccessRootline($pageIndexerRequest);
$stringAccessRootline = (string)$accessRootline;
if (empty($stringAccessRootline)) {
return [];
Expand All @@ -107,16 +100,11 @@ protected function resolveFrontendUserGroups(ServerRequestInterface $request): a
/**
* Gets the access rootline as defined by the request.
*/
protected function getAccessRootline(RequestInterface $request): Rootline
protected function getAccessRootline(PageIndexerRequest $pageIndexerRequest): Rootline
{
$stringAccessRootline = '';

$jsonEncodedParameters = $request->getHeader(PageIndexerRequest::SOLR_INDEX_HEADER)[0];
/** @var PageIndexerRequestHandler $pageIndexerRequestHandler */
$pageIndexerRequestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class, $jsonEncodedParameters);

if ($pageIndexerRequestHandler->getRequest()->getParameter('accessRootline')) {
$stringAccessRootline = $pageIndexerRequestHandler->getRequest()->getParameter('accessRootline');
if ($pageIndexerRequest->getParameter('accessRootline')) {
$stringAccessRootline = $pageIndexerRequest->getParameter('accessRootline');
}
return GeneralUtility::makeInstance(Rootline::class, $stringAccessRootline);
}
Expand Down
90 changes: 0 additions & 90 deletions Classes/IndexQueue/FrontendHelper/AbstractFrontendHelper.php

This file was deleted.

52 changes: 9 additions & 43 deletions Classes/IndexQueue/FrontendHelper/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;

use ApacheSolrForTypo3\Solr\Access\Rootline;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequestHandler;
use TYPO3\CMS\Core\Authentication\AbstractAuthenticationService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Authentication service to authorize the Index Queue page indexer to access
Expand All @@ -40,10 +37,13 @@ class AuthorizationService extends AbstractAuthenticationService
/**
* Gets a fake frontend user record to allow access to protected pages.
*
* @return array An array representing a frontend user.
* @return ?array An array representing a frontend user if a authenticated solr request is available.
*/
public function getUser(): array
public function getUser(): ?array
{
if (!$this->authInfo['request']->getAttribute('solr.pageIndexingInstructions')) {
return null;
}
return [
'uid' => 0,
'username' => self::SOLR_INDEXER_USERNAME,
Expand All @@ -62,10 +62,13 @@ public function getUser(): array
*
* @param array $user Array of user data
* @return int Returns 200 to grant access for the page indexer.
*@see \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication::checkAuthentication()
* @see \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication::checkAuthentication()
*/
public function authUser(array $user): int
{
if (!$this->authInfo['request']->getAttribute('solr.pageIndexingInstructions')) {
return 100;
}
// shouldn't happen, but in case we get a regular user we just
// pass it on to another (regular) auth service
$authenticationLevel = 100;
Expand All @@ -76,41 +79,4 @@ public function authUser(array $user): int

return $authenticationLevel;
}

/**
* Creates user group records so that the page indexer is granted access to
* protected pages.
*
* @param array $user Data of user.
* @param array $knownGroups Group data array of already known groups. This is handy if you want select other related groups. Keys in this array are unique IDs of those groups.
* @return array Groups array, keys = uid which must be unique
*/
public function getGroups(
array $user,
/** @noinspection PhpUnusedParameterInspection */
array $knownGroups = []
): array {
$groupData = [];

/** @var PageIndexerRequestHandler $requestHandler */
$requestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class);
$accessRootline = $requestHandler->getRequest()->getParameter('accessRootline');

if ($user['username'] == self::SOLR_INDEXER_USERNAME && !empty($accessRootline)) {
$accessRootline = GeneralUtility::makeInstance(Rootline::class, $accessRootline);
$groups = $accessRootline->getGroups();

foreach ($groups as $groupId) {
// faking a user group record
$groupData[] = [
'uid' => $groupId,
'pid' => 0,
'title' => '__SolrIndexerGroup__',
'TSconfig' => '',
];
}
}

return $groupData;
}
}
73 changes: 0 additions & 73 deletions Classes/IndexQueue/FrontendHelper/Dispatcher.php

This file was deleted.

21 changes: 1 addition & 20 deletions Classes/IndexQueue/FrontendHelper/FrontendHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;

use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerResponse;

/**
Expand All @@ -37,23 +36,5 @@ public function activate(): void;
* Deactivates a frontend helper by unregistering from hooks and releasing
* resources.
*/
public function deactivate(): void;

/**
* Starts the execution of a frontend helper.
*
* @param PageIndexerRequest $request Page indexer request
* @param PageIndexerResponse $response Page indexer response
*/
public function processRequest(
PageIndexerRequest $request,
PageIndexerResponse $response
): void;

/**
* Returns the collected data.
*
* @return array Collected data.
*/
public function getData(): array;
public function deactivate(PageIndexerResponse $response): void;
}
2 changes: 1 addition & 1 deletion Classes/IndexQueue/FrontendHelper/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function resolveAction(string $action): ?FrontendHelper
/**
* Gets an array with references to activated frontend helpers.
*
* @return array Array of references to activated frontend helpers.
* @return FrontendHelper[] Array of references to activated frontend helpers.
*/
public function getActivatedFrontendHelpers(): array
{
Expand Down
Loading

0 comments on commit 1a926ce

Please sign in to comment.