Added a AccessDeniedHttpException to wrap the AccessDeniedException.

See #1631
This commit is contained in:
Christophe Coevoet 2011-07-11 13:12:24 +02:00
parent 1bdc2b206e
commit dbe1854e1f
2 changed files with 39 additions and 5 deletions

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Exception;
/**
* NotFoundHttpException.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Christophe Coevoet <stof@notk.org>
*/
class AccessDeniedHttpException extends HttpException
{
/**
* Constructor.
*
* @param string $message The internal exception message
* @param Exception $previous The previous exception
* @param integer $code The internal exception code
*/
public function __construct($message = null, \Exception $previous = null, $code = 0)
{
parent::__construct(403, $message, $previous, array(), $code);
}
}

View File

@ -26,6 +26,7 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
@ -113,16 +114,16 @@ class ExceptionListener
if (!$response instanceof Response) { if (!$response instanceof Response) {
return; return;
} }
} else { } elseif (null !== $this->errorPage) {
if (null === $this->errorPage) {
return;
}
$subRequest = $this->httpUtils->createRequest($request, $this->errorPage); $subRequest = $this->httpUtils->createRequest($request, $this->errorPage);
$subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception); $subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);
$response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); $response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
$response->setStatusCode(403); $response->setStatusCode(403);
} else {
$event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
return;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
if (null !== $this->logger) { if (null !== $this->logger) {