Skip to content

Commit

Permalink
[BUGFIX] Pass FrontendUserAuthentication to redirect handling
Browse files Browse the repository at this point in the history
`$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: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
  • Loading branch information
andreaskienast authored and maddy2101 committed Aug 4, 2019
1 parent f8f09ef commit fca2dda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

// 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);
Expand Down
16 changes: 10 additions & 6 deletions typo3/sysext/redirects/Classes/Service/RedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -181,10 +182,11 @@ protected function resolveLinkDetailsFromLinkTarget(string $redirectTarget): arr
/**
* @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']);
Expand All @@ -203,7 +205,7 @@ public function getTargetUrl(array $matchedRedirect, array $queryParams, ?SiteIn
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);
}

/**
Expand Down Expand Up @@ -233,17 +235,18 @@ protected function addQueryParams(array $queryParams, Uri $url): Uri
* 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']],
Expand Down Expand Up @@ -281,11 +284,12 @@ protected function getUriFromCustomLinkDetails(array $redirectRecord, ?SiteInter
*
* 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(
Expand All @@ -295,7 +299,7 @@ protected function bootFrontendController(?SiteInterface $site, array $queryPara
$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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -408,7 +409,7 @@ public function getTargetUrlReturnsNullIfUrlCouldNotBeResolved()
{
$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);
}
Expand All @@ -429,7 +430,7 @@ public function getTargetUrlReturnsUrlForTypeUrl()
];
$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);
Expand All @@ -453,7 +454,7 @@ public function getTargetUrlReturnsUrlForTypeFile()
];
$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);
Expand All @@ -478,7 +479,7 @@ public function getTargetUrlReturnsUrlForTypeFolder()
];
$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);
Expand All @@ -500,7 +501,7 @@ public function getTargetUrlRespectsForceHttps()
];
$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);
Expand All @@ -522,7 +523,7 @@ public function getTargetUrlAddsExistingQueryParams()
];
$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);
Expand Down

0 comments on commit fca2dda

Please sign in to comment.