Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author Rob Allen <[email protected]>
  5.  */
  6.  
  7. namespace BjyAuthorize\View;
  8.  
  9. use BjyAuthorize\Exception\UnAuthorizedException;
  10. use Zend\EventManager\EventManagerInterface;
  11. use Zend\EventManager\ListenerAggregateInterface;
  12. use Zend\Http\Response;
  13. use Zend\Mvc\Application;
  14. use Zend\Mvc\MvcEvent;
  15. use BjyAuthorize\Guard\Route;
  16. use BjyAuthorize\Guard\Controller;
  17.  
  18. /**
  19.  * Dispatch error handler, catches exceptions related with authorization and
  20.  * redirects the user agent to a configured location
  21.  *
  22.  * @author Ben Youngblood <[email protected]>
  23.  * @author Marco Pivetta  <[email protected]>
  24.  */
  25. class RedirectionStrategy implements ListenerAggregateInterface
  26. {
  27.     /**
  28.      * @var string route to be used to handle redirects
  29.      */
  30.     protected $redirectRoute = 'zfcuser/login';
  31.  
  32.     /**
  33.      * @var string URI to be used to handle redirects
  34.      */
  35.     protected $redirectUri;
  36.  
  37.     /**
  38.      * @var \Zend\Stdlib\CallbackHandler[]
  39.      */
  40.     protected $listeners = array();
  41.  
  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     public function attach(EventManagerInterface $events)
  46.     {
  47.         $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000);
  48.     }
  49.  
  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     public function detach(EventManagerInterface $events)
  54.     {
  55.         foreach ($this->listeners as $index => $listener) {
  56.             if ($events->detach($listener)) {
  57.                 unset($this->listeners[$index]);
  58.             }
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Handles redirects in case of dispatch errors caused by unauthorized access
  64.      *
  65.      * @param \Zend\Mvc\MvcEvent $event
  66.      */
  67.     public function onDispatchError(MvcEvent $event)
  68.     {
  69.         // Do nothing if the result is a response object
  70.         $result     = $event->getResult();
  71.         $routeMatch = $event->getRouteMatch();
  72.         $response   = $event->getResponse();
  73.         $router     = $event->getRouter();
  74.         $error      = $event->getError();
  75.         $url        = $this->redirectUri;
  76.  
  77.         if (
  78.             $result instanceof Response
  79.             || ! $routeMatch
  80.             || ($response && ! $response instanceof Response)
  81.             || ! (
  82.                 Route::ERROR === $error
  83.                 || Controller::ERROR === $error
  84.                 || (
  85.                     Application::ERROR_EXCEPTION === $error
  86.                     && ($event->getParam('exception') instanceof UnAuthorizedException)
  87.                 )
  88.             )
  89.         ) {
  90.             return;
  91.         }
  92.  
  93.         if (null === $url) {
  94.             $url = $router->assemble(array(), array('name' => $this->redirectRoute));
  95.         }
  96.  
  97.         $response = $response ?: new Response();
  98.  
  99.         $response->getHeaders()->addHeaderLine('Location', $url);
  100.         $response->setStatusCode(302);
  101.  
  102.         $event->setResponse($response);
  103.     }
  104.  
  105.     /**
  106.      * @param string $redirectRoute
  107.      */
  108.     public function setRedirectRoute($redirectRoute)
  109.     {
  110.         $this->redirectRoute = (string) $redirectRoute;
  111.     }
  112.  
  113.     /**
  114.      * @param string|null $redirectUri
  115.      */
  116.     public function setRedirectUri($redirectUri)
  117.     {
  118.         $this->redirectUri = $redirectUri ? (string) $redirectUri : null;
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement