Skip to content
Snippets Groups Projects
Commit fca2dda3 authored by Andreas Fernandez's avatar Andreas Fernandez Committed by Anja Leichsenring
Browse files

[BUGFIX] Pass FrontendUserAuthentication to redirect handling

`$GLOBALS['TSFE']->fe_user` is not available in the RedirectService. This
patch now explicitly passes the `frontend.user` attribute of the current
request to the RedirectService.

Resolves: #88906
Releases: master
Change-Id: Iae876d6a14f29e675295503be3be80008bcccbb1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61430


Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: default avatarSusanne Moog <look@susi.dev>
Tested-by: default avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: default avatarSusanne Moog <look@susi.dev>
Reviewed-by: default avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
parent f8f09efb
Branches
Tags
No related merge requests found
......@@ -66,7 +66,7 @@ class RedirectHandler implements MiddlewareInterface, LoggerAwareInterface
// If the matched redirect is found, resolve it, and check further
if (is_array($matchedRedirect)) {
$url = $this->redirectService->getTargetUrl($matchedRedirect, $request->getQueryParams(), $request->getAttribute('site', null));
$url = $this->redirectService->getTargetUrl($matchedRedirect, $request->getQueryParams(), $request->getAttribute('frontend.user'), $request->getAttribute('site'));
if ($url instanceof UriInterface) {
$this->logger->debug('Redirecting', ['record' => $matchedRedirect, 'uri' => $url]);
$response = $this->buildRedirectResponse($url, $matchedRedirect);
......
......@@ -28,6 +28,7 @@ use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
use TYPO3\CMS\Frontend\Typolink\AbstractTypolinkBuilder;
......@@ -181,10 +182,11 @@ class RedirectService implements LoggerAwareInterface
/**
* @param array $matchedRedirect
* @param array $queryParams
* @param FrontendUserAuthentication $frontendUserAuthentication
* @param SiteInterface|null $site
* @return UriInterface|null
*/
public function getTargetUrl(array $matchedRedirect, array $queryParams, ?SiteInterface $site = null): ?UriInterface
public function getTargetUrl(array $matchedRedirect, array $queryParams, FrontendUserAuthentication $frontendUserAuthentication, ?SiteInterface $site = null): ?UriInterface
{
$this->logger->debug('Found a redirect to process', $matchedRedirect);
$linkParameterParts = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode((string)$matchedRedirect['target']);
......@@ -203,7 +205,7 @@ class RedirectService implements LoggerAwareInterface
return $url;
}
// If it's a record or page, then boot up TSFE and use typolink
return $this->getUriFromCustomLinkDetails($matchedRedirect, $site, $linkDetails, $queryParams);
return $this->getUriFromCustomLinkDetails($matchedRedirect, $frontendUserAuthentication, $site, $linkDetails, $queryParams);
}
/**
......@@ -233,17 +235,18 @@ class RedirectService implements LoggerAwareInterface
* Called when TypoScript/TSFE is available, so typolink is used to generate the URL
*
* @param array $redirectRecord
* @param FrontendUserAuthentication|null $frontendUserAuthentication
* @param SiteInterface|null $site
* @param array $linkDetails
* @param array $queryParams
* @return UriInterface|null
*/
protected function getUriFromCustomLinkDetails(array $redirectRecord, ?SiteInterface $site, array $linkDetails, array $queryParams): ?UriInterface
protected function getUriFromCustomLinkDetails(array $redirectRecord, FrontendUserAuthentication $frontendUserAuthentication, ?SiteInterface $site, array $linkDetails, array $queryParams): ?UriInterface
{
if (!isset($linkDetails['type'], $GLOBALS['TYPO3_CONF_VARS']['FE']['typolinkBuilder'][$linkDetails['type']])) {
return null;
}
$controller = $this->bootFrontendController($site, $queryParams);
$controller = $this->bootFrontendController($frontendUserAuthentication, $site, $queryParams);
/** @var AbstractTypolinkBuilder $linkBuilder */
$linkBuilder = GeneralUtility::makeInstance(
$GLOBALS['TYPO3_CONF_VARS']['FE']['typolinkBuilder'][$linkDetails['type']],
......@@ -281,11 +284,12 @@ class RedirectService implements LoggerAwareInterface
*
* So a link to a page can be generated.
*
* @param FrontendUserAuthentication|null $frontendUserAuthentication
* @param SiteInterface|null $site
* @param array $queryParams
* @return TypoScriptFrontendController
*/
protected function bootFrontendController(?SiteInterface $site, array $queryParams): TypoScriptFrontendController
protected function bootFrontendController(FrontendUserAuthentication $frontendUserAuthentication, ?SiteInterface $site, array $queryParams): TypoScriptFrontendController
{
$pageId = $site ? $site->getRootPageId() : ($GLOBALS['TSFE'] ? $GLOBALS['TSFE']->id : 0);
$controller = GeneralUtility::makeInstance(
......@@ -295,7 +299,7 @@ class RedirectService implements LoggerAwareInterface
$site->getDefaultLanguage(),
new PageArguments((int)$pageId, '0', [])
);
$controller->fe_user = $GLOBALS['TSFE']->fe_user ?? null;
$controller->fe_user = $frontendUserAuthentication;
$controller->fetch_the_id();
$controller->calculateLinkVars($queryParams);
$controller->getConfigArray();
......
......@@ -24,6 +24,7 @@ use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Redirects\Service\RedirectCacheService;
use TYPO3\CMS\Redirects\Service\RedirectService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
......@@ -408,7 +409,7 @@ class RedirectServiceTest extends UnitTestCase
{
$this->linkServiceProphecy->resolve(Argument::any())->willThrow(new InvalidPathException('', 1516531195));
$result = $this->redirectService->getTargetUrl(['target' => 'invalid'], [], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl(['target' => 'invalid'], [], new FrontendUserAuthentication(), new Site('dummy', 13, []));
self::assertNull($result);
}
......@@ -429,7 +430,7 @@ class RedirectServiceTest extends UnitTestCase
];
$this->linkServiceProphecy->resolve($redirectTargetMatch['target'])->willReturn($linkDetails);
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new FrontendUserAuthentication(), new Site('dummy', 13, []));
$uri = new Uri('https://example.com/');
self::assertEquals($uri, $result);
......@@ -453,7 +454,7 @@ class RedirectServiceTest extends UnitTestCase
];
$this->linkServiceProphecy->resolve($redirectTargetMatch['target'])->willReturn($linkDetails);
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new FrontendUserAuthentication(), new Site('dummy', 13, []));
$uri = new Uri('https://example.com/file.txt');
self::assertEquals($uri, $result);
......@@ -478,7 +479,7 @@ class RedirectServiceTest extends UnitTestCase
];
$this->linkServiceProphecy->resolve($redirectTargetMatch['target'])->willReturn($linkDetails);
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new FrontendUserAuthentication(), new Site('dummy', 13, []));
$uri = new Uri('https://example.com/folder/');
self::assertEquals($uri, $result);
......@@ -500,7 +501,7 @@ class RedirectServiceTest extends UnitTestCase
];
$this->linkServiceProphecy->resolve($redirectTargetMatch['target'])->willReturn($linkDetails);
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], new FrontendUserAuthentication(), new Site('dummy', 13, []));
$uri = new Uri('https://example.com');
self::assertEquals($uri, $result);
......@@ -522,7 +523,7 @@ class RedirectServiceTest extends UnitTestCase
];
$this->linkServiceProphecy->resolve($redirectTargetMatch['target'])->willReturn($linkDetails);
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, ['bar' => 3, 'baz' => 4], new Site('dummy', 13, []));
$result = $this->redirectService->getTargetUrl($redirectTargetMatch, ['bar' => 3, 'baz' => 4], new FrontendUserAuthentication(), new Site('dummy', 13, []));
$uri = new Uri('https://example.com/?bar=2&baz=4&foo=1');
self::assertEquals($uri, $result);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment