/**
  * Handles an access denied failure redirecting to home page
  *
  * @param Request               $request
  * @param AccessDeniedException $accessDeniedException
  *
  * @return Response may return null
  */
 public function handle(Request $request, AccessDeniedException $accessDeniedException)
 {
     $this->logger->error('User tried to access: ' . $request->getUri());
     if ($request->isXmlHttpRequest()) {
         return new JsonResponse(['message' => $accessDeniedException->getMessage(), 'trace' => $accessDeniedException->getTraceAsString(), 'exception' => get_class($accessDeniedException)], Response::HTTP_SERVICE_UNAVAILABLE);
     } else {
         $url = $request->getBasePath() !== "" ? $request->getBasePath() : "/";
         $response = new RedirectResponse($url);
         $response->setStatusCode(Response::HTTP_FORBIDDEN);
         $response->prepare($request);
         return $response->send();
     }
 }
示例#2
0
/* Ensure requested file is actually in the content folder.
 * You'd think I'd do this sooner, but we don't know the actual
 * filename before this point.
 */
$filename = realpath($filename);
if (strpos($filename, realpath(__DIR__ . '/../content')) !== 0) {
    // Attempt to read file outside of content folder
    $filename = __DIR__ . '/../content/404.html';
}
/* Render the file:
 * Dispatch a FileCopyEvent to call transformers on a file, then build a response
 */
$event = new FileCopyEvent($filename, $filename);
$extension = $event->getExtension();
$event = $container->get('event_dispatcher')->dispatch(FilesystemEvents::COPY, $event);
$response = new Response($event->getContent(), 200);
$headers = $event->data->get('headers')->getOrElse(array());
// Special cases
if (isset($headers['location'])) {
    $response = new RedirectResponse($headers['location']);
    unset($headers['location']);
}
if (isset($headers['status'])) {
    $response->setStatusCode($headers['status']);
    unset($headers['status']);
}
// Everything else
foreach ($headers as $key => $value) {
    $response->headers->set($key, $value);
}
$response->send();