Skip to content

[BUGFIX] use event listener instead of hook to simulate frontend user… #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions Classes/Hooks/TypoScriptFrontendController.php

This file was deleted.

59 changes: 59 additions & 0 deletions Classes/Middleware/SimulateFrontendUserGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace DirectMailTeam\DirectMail\Middleware;

/*
* 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!
*/

use DirectMailTeam\DirectMail\Utility\DmRegistryUtility;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class SimulateFrontendUserGroup implements MiddlewareInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

public function __construct(
protected readonly Context $context,
) {}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$directMailFeGroup = (int)($request->getQueryParams()['dmail_fe_group'] ?? 0);
$accessToken = (string)($request->getQueryParams()['access_token'] ?? '');

if ($directMailFeGroup > 0 && GeneralUtility::makeInstance(DmRegistryUtility::class)->validateAndRemoveAccessToken($accessToken)) {
$userAspect = $this->context->getAspect('frontend.user');

// overwrite 'frontend.user' aspect if required
if (!in_array($directMailFeGroup, $userAspect->getGroupIds(), true)) {
$frontendUser = $request->getAttribute('frontend.user');
$this->context->setAspect(
'frontend.user',
new UserAspect($frontendUser, [$directMailFeGroup]),
);
}
}

return $handler->handle($request);
}
}
9 changes: 9 additions & 0 deletions Configuration/RequestMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@
'friends-of-typo3/jumpurl',
],
],
'direct-mail/simulate-frontend-user-group' => [
'target' => \DirectMailTeam\DirectMail\Middleware\SimulateFrontendUserGroup::class,
'before' => [
'typo3/cms-frontend/tsfe',
],
'after' => [
'typo3/cms-frontend/authentication',
],
],
],
];
3 changes: 0 additions & 3 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

// https://docs.typo3.org/m/typo3/reference-coreapi/12.4/en-us/ExtensionArchitecture/BestPractises/ConfigurationFiles.html
(function () {
// Register hook for simulating a user group
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_checkEnableFields']['direct_mail'] = 'DirectMailTeam\\DirectMail\\Hooks\\TypoScriptFrontendController->simulateUsergroup';

// Get extension configuration so we can use it here:
$extConf = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)->get('direct_mail');

Expand Down