示例#1
0
 /**
  * @param MvcEvent $event
  * @throws \Zend\Http\Exception\InvalidArgumentException
  *
  * @return void
  */
 public function onDispatchError(MvcEvent $event)
 {
     parent::onDispatchError($event);
     $response = $event->getResponse();
     // Common view variables
     $variables = ['error' => $event->getParam('error'), 'identity' => $event->getParam('identity')];
     switch ($event->getError()) {
         case GuardInterface::GUARD_UNAUTHORIZED:
             $variables['controller'] = $event->getParam('controller');
             $variables['action'] = $event->getParam('action');
             break;
         case Application::ERROR_EXCEPTION:
             if (!$event->getParam('exception') instanceof UnauthorizedException) {
                 return;
             }
             $variables['reason'] = $event->getParam('exception')->getMessage();
             $variables['error'] = 'error-unauthorized';
             break;
         default:
             /*
              * do nothing if there is no error in the event or the error
              * does not match one of our predefined errors (we don't want
              * our 403 template to handle other types of errors)
              */
             return;
     }
     $model = new ViewModel($variables);
     $response = $response ?: new HttpResponse();
     $model->setTemplate($this->getOptions()->getTemplate());
     $event->getViewModel()->addChild($model);
     $response->setStatusCode(403);
     $event->setResponse($response);
 }
示例#2
0
 /**
  * @param MvcEvent $event
  * @throws \Zend\Http\Exception\InvalidArgumentException
  *
  * @return void
  */
 public function onDispatchError(MvcEvent $event)
 {
     parent::onDispatchError($event);
     if ($event->getError() !== GuardInterface::GUARD_UNAUTHORIZED) {
         return;
     }
     $response = $event->getResponse();
     $router = $event->getRouter();
     $options = $this->getOptions();
     $resource = $event->getParam('resource');
     if ($this->identityProvider->getIdentity($resource) && !$options->getRedirectWhenConnected()) {
         return;
     }
     $url = $router->assemble($options->getRouteParams(), ['name' => $options->getRouteName()] + $options->getRouteOptions());
     $response = $response ?: new Response();
     $response->getHeaders()->addHeaderLine('Location', $url);
     $response->setStatusCode(302);
     $event->setResponse($response);
 }