Skip to content

Commit

Permalink
Merge pull request #14 from b13/v11Compat
Browse files Browse the repository at this point in the history
[!!!][TASK] Compatibility with TYPO3 v11
  • Loading branch information
bmack authored Oct 13, 2022
2 parents 946d279 + 8cac9fc commit fa142df
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 114 deletions.
8 changes: 1 addition & 7 deletions Classes/Authentication/PreviewUserAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
*/
class PreviewUserAuthentication extends BackendUserAuthentication
{
/**
* @var SiteLanguage
*/
protected $siteLanguage;
protected SiteLanguage $siteLanguage;

public function __construct(SiteLanguage $siteLanguage)
{
Expand Down Expand Up @@ -66,9 +63,6 @@ public function calcPerms($row): int
*/
public function checkLanguageAccess($langValue): bool
{
if ($this->siteLanguage === null) {
return false;
}
return (int)$langValue === $this->siteLanguage->getLanguageId();
}
}
40 changes: 7 additions & 33 deletions Classes/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,21 @@
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* Class PreviewController
*/
class PreviewController
{
/**
* @var ModuleTemplate
*/
protected $moduleTemplate;

/**
* @var StandaloneView
*/
protected $view;

/**
* @var ServerRequestInterface
*/
protected $request;

/**
* @var IconFactory
*/
protected $iconFactory;

/**
* @var SiteFinder
*/
protected $siteFinder;
protected ModuleTemplate $moduleTemplate;
protected StandaloneView $view;
protected SiteFinder $siteFinder;

public function __construct(ModuleTemplate $moduleTemplate = null, IconFactory $iconFactory = null, SiteFinder $siteFinder = null)
public function __construct(ModuleTemplate $moduleTemplate, SiteFinder $siteFinder)
{
$this->moduleTemplate = $moduleTemplate ?? GeneralUtility::makeInstance(ModuleTemplate::class);
$this->iconFactory = $iconFactory ?? GeneralUtility::makeInstance(IconFactory::class);
$this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
$this->moduleTemplate = $moduleTemplate;
$this->siteFinder = $siteFinder;
$this->initializeView('index');
}

Expand All @@ -82,7 +56,7 @@ public function indexAction(ServerRequestInterface $request): ResponseInterface
}

/**
* @return Site[]
* @return SiteWrapper[]
*/
protected function getAllSites(): array
{
Expand Down
38 changes: 19 additions & 19 deletions Classes/Http/Middleware/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
Expand All @@ -30,14 +31,11 @@
*/
class Preview implements MiddlewareInterface
{
/**
* @var Context
*/
protected $context;
protected Context $context;

public function __construct(Context $context = null)
public function __construct(Context $context)
{
$this->context = $context ?? GeneralUtility::makeInstance(Context::class);
$this->context = $context;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand Down Expand Up @@ -68,33 +66,35 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if (!$this->verifyHash($hash, $language, $site)) {
return $handler->handle($request);
}
$this->initializePreviewUser($language);
$response = $handler->handle($request);

// If the GET parameter PreviewUriBuilder::PARAMETER_NAME is set, then a cookie is set for the next request
if ($request->getQueryParams()[PreviewUriBuilder::PARAMETER_NAME] ?? false) {
$this->setCookie($hash, $request->getAttribute('normalizedParams'));
/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
$cookie = new Cookie(
PreviewUriBuilder::PARAMETER_NAME,
$hash,
0,
$normalizedParams->getSitePath(),
'',
true,
true
);
return $response->withAddedHeader('Set-Cookie', $cookie->__toString());
}

$this->initializePreviewUser($language);

return $handler->handle($request);
return $response;
}

/**
* Looks for the PreviewUriBuilder::PARAMETER_NAME in the QueryParams and Cookies
*
* @param ServerRequestInterface $request
* @return string
*/
protected function findHashInRequest(ServerRequestInterface $request): string
{
return $request->getQueryParams()[PreviewUriBuilder::PARAMETER_NAME] ?? $request->getCookieParams()[PreviewUriBuilder::PARAMETER_NAME] ?? '';
}

protected function setCookie(string $inputCode, NormalizedParams $normalizedParams): void
{
setcookie(PreviewUriBuilder::PARAMETER_NAME, $inputCode, 0, $normalizedParams->getSitePath(), '', true, true);
}

/**
* Creates a preview user and sets the current page ID (for accessing the page)
*/
Expand Down
17 changes: 3 additions & 14 deletions Classes/Preview/PreviewUriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@ class PreviewUriBuilder
{
public const PARAMETER_NAME = 'tx_authorized_preview';

/**
* @var SitePreview
*/
protected $sitePreview;

/**
* @var string
*/
protected $uri = '';

/**
* @var string
*/
protected $hash = '';
protected SitePreview $sitePreview;
protected string $uri = '';
protected string $hash = '';

public function __construct(SitePreview $sitePreview)
{
Expand Down
41 changes: 11 additions & 30 deletions Classes/Preview/SitePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,17 @@

class SitePreview
{
/**
* @var bool
*/
protected $valid = false;

/**
* @var Site
*/
protected $site = null;

/**
* The language ID for the Preview URL
*
* @var int
*/
protected $languageId = -1;

/**
* Time until the preview URl expires
*
* @var int
*/
protected $lifeTime = 604800;

/**
* The final preview URL
*
* @var string
*/
protected $previewUrl = '';
protected bool $valid = false;
protected ?Site $site = null;

/** The language ID for the Preview URL */
protected int $languageId = -1;

/** Time until the preview URl expires */
protected int $lifeTime = 604800;

/** The final preview URL */
protected string $previewUrl = '';

public function __construct(int $languageId, string $identifier, array $lifetime = [])
{
Expand Down
7 changes: 2 additions & 5 deletions Classes/SiteWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
*/
class SiteWrapper
{
/**
* @var Site
*/
protected $site = null;
protected Site $site;

/**
* @var SiteLanguage[]
*/
protected $disabledLanguages = [];
protected array $disabledLanguages = [];

public function __construct(Site $site)
{
Expand Down
11 changes: 11 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

B13\AuthorizedPreview\:
resource: '../Classes/*'

B13\AuthorizedPreview\Controller\PreviewController:
tags: ['backend.controller']
7 changes: 3 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ through generated preview URLs without the need for a backend login.

## Requirements

* TYPO3 9 LTS as well as TYPO3 10.3 or higher
* Site Configuration(s)
* TYPO3 v10 LTS or TYPO3 v11 LTS

## Installation and Setup
Install the extension via your preferred way. The extension will add one database table to the database.
No further setup is required. The extension works out of the box.

## What is does
The extension adds a backend module called "Preview". The module lists all disabled languages
(show in frontend = 0) for each Site. For each disabled language a lifetime can be configured and a
preview URL can be generated that can then be send to colleagues (e.g. for proof reading).
("Show in frontend" = false) for each Site. For each disabled language a lifetime can be configured and a
preview URL can be generated, that can then be sent to colleagues (e.g. for proof reading).

Within their lifetime the preview URLs enable access to a disabled language without a backend login.
For any other website visitor the disabled languages is still not accessible.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "GPL-2.0+",
"description": "Generate URLs to preview hidden languages without a backend login",
"require": {
"typo3/cms-core": "^9.5 || ^10.3 || ^11.0"
"typo3/cms-core": "^10.4 || ^11.5"
},
"extra": {
"typo3/cms": {
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'version' => '1.3.0',
'constraints' => [
'depends' => [
'typo3' => '9.5.0-11.5.99'
'typo3' => '10.4.0-11.5.99'
],
'conflicts' => [],
'suggests' => [],
Expand Down

0 comments on commit fa142df

Please sign in to comment.