Пример #1
0
 private function handleAccessDeniedException(GetResponseForExceptionEvent $event, AccessDeniedException $exception)
 {
     $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
     $token = $this->context->getToken();
     if (!$this->authenticationTrustResolver->isFullFledged($token)) {
         if (null !== $this->logger) {
             $this->logger->debug(sprintf('Access is denied (user is not fully authenticated) by "%s" at line %s; redirecting to authentication entry point', $exception->getFile(), $exception->getLine()));
         }
         try {
             $insufficientAuthenticationException = new InsufficientAuthenticationException('Full authentication is required to access this resource.', 0, $exception);
             $insufficientAuthenticationException->setToken($token);
             $event->setResponse($this->startAuthentication($event->getRequest(), $insufficientAuthenticationException));
         } catch (\Exception $e) {
             $event->setException($e);
         }
         return;
     }
     if (null !== $this->logger) {
         $this->logger->debug(sprintf('Access is denied (and user is neither anonymous, nor remember-me) by "%s" at line %s', $exception->getFile(), $exception->getLine()));
     }
     try {
         if (null !== $this->accessDeniedHandler) {
             $response = $this->accessDeniedHandler->handle($event->getRequest(), $exception);
             if ($response instanceof Response) {
                 $event->setResponse($response);
             }
         } elseif (null !== $this->errorPage) {
             $subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage);
             $subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception);
             $event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true));
         }
     } catch (\Exception $e) {
         if (null !== $this->logger) {
             $this->logger->error(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()));
         }
         $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
     }
 }