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.

Revisions

  1. Bastian Waidelich revised this gist Jun 1, 2015. 2 changed files with 55 additions and 7 deletions.
    56 changes: 50 additions & 6 deletions DispatchComponent.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    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;

    /**
    @@ -24,17 +25,60 @@ public function handle(ComponentContext $componentContext) {
    try {
    parent::handle($componentContext);
    } catch (AccessDeniedException $exception) {
    $uri = $this->options['redirectUriUponAccessDenied'];
    $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);

    $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);
    }

    $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;
    }
    }
    6 changes: 5 additions & 1 deletion Settings.yaml
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,8 @@ TYPO3:
    'dispatching':
    component: 'Wwwision\Test\Mvc\DispatchComponent'
    componentOptions:
    'redirectUriUponAccessDenied': 'login'
    'redirectUriUponAccessDenied':
    routeValues:
    '@package': 'Wwwision.Test'
    '@controller': 'Login'
    '@action': 'login'
  2. Bastian Waidelich revised this gist Jun 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DispatchComponent.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ public function handle(ComponentContext $componentContext) {
    parent::handle($componentContext);
    } catch (AccessDeniedException $exception) {
    $uri = $this->options['redirectUriUponAccessDenied'];
    if (strpos($this->options['uri'], '://') === FALSE) {
    if (strpos($uri, '://') === FALSE) {
    $uri = $componentContext->getHttpRequest()->getBaseUri() . $uri;
    }

  3. Bastian Waidelich created this gist Jun 1, 2015.
    40 changes: 40 additions & 0 deletions DispatchComponent.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?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($this->options['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);
    }
    }
    }
    10 changes: 10 additions & 0 deletions Settings.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    TYPO3:
    Flow:
    http:
    chain:
    'process':
    chain:
    'dispatching':
    component: 'Wwwision\Test\Mvc\DispatchComponent'
    componentOptions:
    'redirectUriUponAccessDenied': 'login'