Skip to content

Instantly share code, notes, and snippets.

@vixe76
Forked from bwaidelich/DispatchComponent.php
Created March 23, 2017 14:58
Show Gist options
  • Select an option

  • Save vixe76/dd6bf0fbfd590fcaee8f9e289bf79e7c to your computer and use it in GitHub Desktop.

Select an option

Save vixe76/dd6bf0fbfd590fcaee8f9e289bf79e7c to your computer and use it in GitHub Desktop.
A custom implementation of the default TYPO3.Flow DispatchComponent that allows for redirecting to a configurable URI upon access denied exceptions
<?php
namespace Wwwision\Test\Mvc;
/* *
* This script belongs to the TYPO3 Flow package "Wwwision.Test". *
* *
* */
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Http\Component\ComponentChain;
use TYPO3\Flow\Http\Component\ComponentContext;
use TYPO3\Flow\Security\Exception\AccessDeniedException;
/**
* A custom implementation of the default DispatchComponent that allows for redirecting to a configurable URI upon access denied exceptions
*/
class DispatchComponent extends \TYPO3\Flow\Mvc\DispatchComponent {
/**
* @param ComponentContext $componentContext
* @return void
*/
public function handle(ComponentContext $componentContext) {
try {
parent::handle($componentContext);
} catch (AccessDeniedException $exception) {
$uri = $this->options['redirectUriUponAccessDenied'];
if (strpos($uri, '://') === FALSE) {
$uri = $componentContext->getHttpRequest()->getBaseUri() . $uri;
}
$response = $componentContext->getHttpResponse();
$response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="0;url=%s"/></head></html>', htmlentities($uri, ENT_QUOTES, 'utf-8')));
$response->setStatus(303);
$response->setHeader('Location', $uri);
$componentContext->setParameter(ComponentChain::class, 'cancel', TRUE);
}
}
}
TYPO3:
Flow:
http:
chain:
'process':
chain:
'dispatching':
component: 'Wwwision\Test\Mvc\DispatchComponent'
componentOptions:
'redirectUriUponAccessDenied': 'login'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment