<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Twig\Environment;
class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function handle(Request $request, AccessDeniedException $accessDeniedException)
{
// XML?
if($request->isXmlHttpRequest())
{
return new JsonResponse(
array(
'success' => false,
'error' => $accessDeniedException->getMessage()
));
}
// Render view
return new Response($this->twig->render('System/Errors/accessDenied.html.twig', array(
'message' => $accessDeniedException->getMessage()
)));
}
}