Commit 31b32c28 authored by Benni Mack's avatar Benni Mack
Browse files

[BUGFIX] Register workspace preview middleware earlier

The current workspace preview middleware runs at a point
after the Page ID (and PageArguments) has been resolved by
the URL already.

This means a workspace preview with a preview user (and not
a backend user) does not work, as the user is registered too
late in the Middleware workflow.

In order to restore the functionality, the preview user is now
created BEFORE the PageResolver middleware, and does
everything (incl. sending the preview cookie, which is now
attached to the response, and not done during processing of the request).

However, as the webmount of the current page ID needs to be registered,
a second middleware is added which runs before TSFE->determineId()
but after the PageResolver and PageArgumentValidator middlewares.

Resolves: #91662
Releases: master, 10.4
Change-Id: Ic0108d2cd468f3ecf84e5a0e06c0fd5329046606
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64900


Tested-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: David Steeb's avatarDavid Steeb <david.steeb@b13.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
Reviewed-by: David Steeb's avatarDavid Steeb <david.steeb@b13.com>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 270b5f4a
......@@ -32,7 +32,7 @@ use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Routing\RouteResultInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Workspaces\Authentication\PreviewUserAuthentication;
......@@ -73,41 +73,48 @@ class WorkspacePreview implements MiddlewareInterface
{
$addInformationAboutDisabledCache = false;
$keyword = $this->getPreviewInputCode($request);
if ($keyword) {
switch ($keyword) {
case 'IGNORE':
break;
case 'LOGOUT':
// "log out", and unset the cookie
$this->setCookie('', $request->getAttribute('normalizedParams'));
$message = $this->getLogoutTemplateMessage($request->getQueryParams()['returnUrl'] ?? '');
return new HtmlResponse($message);
default:
$pageArguments = $request->getAttribute('routing', null);
// A keyword was found in a query parameter or in a cookie
// If the keyword is valid, activate a BE User and override any existing BE Users
$configuration = $this->getPreviewConfigurationFromRequest($request, $keyword);
if (is_array($configuration) && $configuration['fullWorkspace'] > 0 && $pageArguments instanceof PageArguments) {
$previewUser = $this->initializePreviewUser(
(int)$configuration['fullWorkspace'],
$pageArguments->getPageId()
);
if ($previewUser) {
$GLOBALS['BE_USER'] = $previewUser;
// Register the preview user as aspect
$this->setBackendUserAspect(GeneralUtility::makeInstance(Context::class), $previewUser);
}
}
$setCookieOnCurrentRequest = false;
/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
$context = GeneralUtility::makeInstance(Context::class);
// First, if a Log out is happening, a custom HTML output page is shown and the request exits with removing
// the cookie for the backend preview.
if ($keyword === 'LOGOUT') {
// "log out", and unset the cookie
$message = $this->getLogoutTemplateMessage($request->getQueryParams()['returnUrl'] ?? '');
$response = new HtmlResponse($message);
return $this->addCookie('', $normalizedParams, $response);
}
// If the keyword is ignore, then the preview is not managed as "Preview User" but handled
// via the regular backend user or even no user if the GET parameter ADMCMD_noBeUser is set
if (!empty($keyword) && $keyword !== 'IGNORE') {
$routeResult = $request->getAttribute('routing', null);
// A keyword was found in a query parameter or in a cookie
// If the keyword is valid, activate a BE User and override any existing BE Users
// (in case workspace ID was given and a corresponding site to be used was found)
$previewWorkspaceId = $this->getWorkspaceIdFromRequest($request, $keyword);
if ($previewWorkspaceId > 0 && $routeResult instanceof RouteResultInterface) {
$previewUser = $this->initializePreviewUser($previewWorkspaceId);
if ($previewUser instanceof PreviewUserAuthentication) {
$GLOBALS['BE_USER'] = $previewUser;
// Register the preview user as aspect
$this->setBackendUserAspect($context, $previewUser);
// If the GET parameter is set, and we have a valid Preview User, the cookie needs to be
// set and the GET parameter should be removed.
$setCookieOnCurrentRequest = $request->getQueryParams()[$this->previewKey] ?? false;
}
}
}
// If "ADMCMD_noBeUser" is set, then ensure that there is no workspace preview and no BE User logged in.
// This option is solely used to ensure that a be user can preview the live version of a page in the
// workspace preview module.
if ($request->getQueryParams()['ADMCMD_noBeUser']) {
if ($request->getQueryParams()['ADMCMD_noBeUser'] ?? null) {
$GLOBALS['BE_USER'] = null;
// Register the backend user as aspect
$this->setBackendUserAspect(GeneralUtility::makeInstance(Context::class), null);
$this->setBackendUserAspect($context, null);
// Caching is disabled, because otherwise generated URLs could include the ADMCMD_noBeUser parameter
$request = $request->withAttribute('noCache', true);
$addInformationAboutDisabledCache = true;
......@@ -115,13 +122,12 @@ class WorkspacePreview implements MiddlewareInterface
$response = $handler->handle($request);
// Caching is disabled, because otherwise generated URLs could include the ADMCMD_noBeUser parameter
if ($addInformationAboutDisabledCache) {
if ($GLOBALS['TSFE'] instanceof TypoScriptFrontendController && $addInformationAboutDisabledCache) {
$GLOBALS['TSFE']->set_no_cache('GET Parameter ADMCMD_noBeUser was given', true);
}
// Add an info box to the frontend content
if ($GLOBALS['TSFE']->doWorkspacePreview()) {
if ($GLOBALS['TSFE'] instanceof TypoScriptFrontendController && $context->getPropertyFromAspect('workspace', 'isOffline', false)) {
$previewInfo = $this->renderPreviewInfo($GLOBALS['TSFE'], $request->getAttribute('normalizedParams'));
$body = $response->getBody();
$body->rewind();
......@@ -132,6 +138,10 @@ class WorkspacePreview implements MiddlewareInterface
$response = $response->withBody($body);
}
// If the GET parameter ADMCMD_prev is set, then a cookie is set for the next request to keep the preview user
if ($setCookieOnCurrentRequest) {
$response = $this->addCookie($keyword, $normalizedParams, $response);
}
return $response;
}
......@@ -176,10 +186,10 @@ class WorkspacePreview implements MiddlewareInterface
*
* @param ServerRequestInterface $request
* @param string $inputCode
* @return array|null Preview configuration array from sys_preview record.
* @return int|null Workspace ID stored in the preview configuration array of a sys_preview record.
* @throws \Exception
*/
protected function getPreviewConfigurationFromRequest(ServerRequestInterface $request, string $inputCode): ?array
protected function getWorkspaceIdFromRequest(ServerRequestInterface $request, string $inputCode): ?int
{
$previewData = $this->getPreviewData($inputCode);
if (!is_array($previewData)) {
......@@ -194,39 +204,33 @@ class WorkspacePreview implements MiddlewareInterface
if (!$previewConfig['fullWorkspace']) {
throw new \Exception('Preview configuration did not include a workspace preview', 1294585190);
}
// If the GET parameter ADMCMD_prev is set, then a cookie is set for the next request
if ($request->getQueryParams()[$this->previewKey] ?? false) {
$this->setCookie($inputCode, $request->getAttribute('normalizedParams'));
}
return $previewConfig;
return (int)$previewConfig['fullWorkspace'];
}
/**
* Creates a preview user and sets the workspace ID and the current page ID (for accessing the page)
* Creates a preview user and sets the workspace ID
*
* @param int $workspaceUid the workspace ID to set
* @param mixed $requestedPageId pageID to the current page
* @return PreviewUserAuthentication|bool if the set up of the workspace was successful, the user is returned.
* @return PreviewUserAuthentication|null if the set up of the workspace was successful, the user is returned.
*/
protected function initializePreviewUser(int $workspaceUid, $requestedPageId)
protected function initializePreviewUser(int $workspaceUid): ?PreviewUserAuthentication
{
if ($workspaceUid > 0) {
$previewUser = GeneralUtility::makeInstance(PreviewUserAuthentication::class);
$previewUser->setWebmounts([$requestedPageId]);
if ($previewUser->setTemporaryWorkspace($workspaceUid)) {
return $previewUser;
}
$previewUser = GeneralUtility::makeInstance(PreviewUserAuthentication::class);
if ($previewUser->setTemporaryWorkspace($workspaceUid)) {
return $previewUser;
}
return false;
return null;
}
/**
* Sets a cookie for logging in a preview user
* Adds a cookie for logging in a preview user into the HTTP response
*
* @param string $inputCode
* @param string $keyword
* @param NormalizedParams $normalizedParams
* @param ResponseInterface $response
* @return ResponseInterface
*/
protected function setCookie(string $inputCode, NormalizedParams $normalizedParams)
protected function addCookie(string $keyword, NormalizedParams $normalizedParams, ResponseInterface $response): ResponseInterface
{
$cookieSameSite = $this->sanitizeSameSiteCookieValue(
strtolower($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieSameSite'] ?? Cookie::SAMESITE_STRICT)
......@@ -236,7 +240,7 @@ class WorkspacePreview implements MiddlewareInterface
$cookie = new Cookie(
$this->previewKey,
$inputCode,
$keyword,
0,
$normalizedParams->getSitePath(),
null,
......@@ -245,7 +249,7 @@ class WorkspacePreview implements MiddlewareInterface
false,
$cookieSameSite
);
header('Set-Cookie: ' . $cookie->__toString(), false);
return $response->withAddedHeader('Set-Cookie', $cookie->__toString());
}
/**
......@@ -394,11 +398,11 @@ class WorkspacePreview implements MiddlewareInterface
*/
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'] ?: GeneralUtility::makeInstance(LanguageService::class);
return $GLOBALS['LANG'] ?: LanguageService::create('default');
}
/**
* Register the backend user as aspect
* Register or override the backend user as aspect, as well as the workspace information the user object is holding
*
* @param Context $context
* @param BackendUserAuthentication $user
......
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Workspaces\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Workspaces\Authentication\PreviewUserAuthentication;
/**
* Middleware to set the current page ID (for accessing the page) for the PreviewBackendUser,
* so the preview user is allowed to actually access the page (even if it is hidden).
*
* @internal
*/
class WorkspacePreviewPermissions implements MiddlewareInterface
{
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws \Exception
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$pageArguments = $request->getAttribute('routing', null);
if ($pageArguments instanceof PageArguments && $GLOBALS['BE_USER'] instanceof PreviewUserAuthentication) {
$GLOBALS['BE_USER']->setWebmounts([$pageArguments->getPageId()]);
}
return $handler->handle($request);
}
}
......@@ -7,13 +7,25 @@ return [
'typo3/cms-workspaces/preview' => [
'target' => \TYPO3\CMS\Workspaces\Middleware\WorkspacePreview::class,
'after' => [
// PageArguments are needed to store information about the preview
'typo3/cms-frontend/page-argument-validator',
// A preview user will override an existing logged-in backend user
'typo3/cms-frontend/backend-user-authentication',
],
'before' => [
// TSFE is needed to store information about the preview
// Page Router should have not been called yet, in order to set up the Context's Workspace Aspect
'typo3/cms-frontend/page-resolver',
]
],
'typo3/cms-workspaces/preview-permissions' => [
'target' => \TYPO3\CMS\Workspaces\Middleware\WorkspacePreviewPermissions::class,
'after' => [
// The cookie/GET parameter information should have been evaluated
'typo3/cms-workspaces/preview',
// PageArguments are needed to find out the current Page ID
'typo3/cms-frontend/page-resolver',
'typo3/cms-frontend/page-argument-validator',
],
'before' => [
// TSFE should not yet have been called - determineId() is what relies on the information of this middleware
'typo3/cms-frontend/tsfe',
]
],
......
Supports Markdown
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