Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Last active March 23, 2017 14:58
Show Gist options
  • Save bwaidelich/ac87852b17c8b075254f to your computer and use it in GitHub Desktop.
Save bwaidelich/ac87852b17c8b075254f 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\Mvc\Routing\UriBuilder;
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) {
$this->initiateRedirect($componentContext);
}
}
/**
* Triggers the redirect, if configured
*
* @param ComponentContext $componentContext
* @return void
*/
protected function initiateRedirect(ComponentContext $componentContext) {
if (isset($this->options['redirectUriUponAccessDenied']['routeValues'])) {
$routeValues = $this->options['redirectUriUponAccessDenied']['routeValues'];
$actionRequest = $componentContext->getParameter('TYPO3\Flow\Mvc\DispatchComponent', 'actionRequest');
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($actionRequest);
$actionName = $this->extractRouteValue($routeValues, '@action');
$controllerName = $this->extractRouteValue($routeValues, '@controller');
$packageKey = $this->extractRouteValue($routeValues, '@package');
$subPackageKey = $this->extractRouteValue($routeValues, '@subpackage');
$uri = $uriBuilder->setCreateAbsoluteUri(TRUE)->uriFor($actionName, $routeValues, $controllerName, $packageKey, $subPackageKey);
} elseif (isset($this->options['redirectUriUponAccessDenied']['uri'])) {
$uri = $this->options['redirectUriUponAccessDenied']['uri'];
if (strpos($uri, '://') === FALSE) {
$uri = $componentContext->getHttpRequest()->getBaseUri() . $uri;
}
} else {
// no redirect URI configured
return;
}
$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);
}
/**
* Returns the entry $key from the array $routeValues removing the original array item.
* If $key does not exist, NULL is returned.
*
* @param array $routeValues
* @param string $key
* @return mixed the specified route value or NULL if it is not set
*/
protected function extractRouteValue(array &$routeValues, $key) {
if (!isset($routeValues[$key])) {
return NULL;
}
$routeValue = $routeValues[$key];
unset($routeValues[$key]);
return $routeValue;
}
}
TYPO3:
Flow:
http:
chain:
'process':
chain:
'dispatching':
component: 'Wwwision\Test\Mvc\DispatchComponent'
componentOptions:
'redirectUriUponAccessDenied':
routeValues:
'@package': 'Wwwision.Test'
'@controller': 'Login'
'@action': 'login'
@sebastiansommer
Copy link

Awesome! Thanks for that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment