Skip to content

Commit 977e148

Browse files
committed
Allow to customize deeper the AccessDeniedListener
1 parent d2298a2 commit 977e148

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

EventListener/AccessDeniedListener.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
namespace FOS\RestBundle\EventListener;
1313

1414
use FOS\RestBundle\FOSRestBundle;
15-
use Psr\Log\LoggerInterface;
1615
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
17-
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
1816
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1917
use Symfony\Component\HttpKernel\Exception\HttpException;
2018
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
2119
use Symfony\Component\HttpKernel\KernelEvents;
2220
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2321
use Symfony\Component\Security\Core\Exception\AuthenticationException;
22+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2423

2524
/**
2625
* This listener handles ensures that for specific formats AccessDeniedExceptions
@@ -30,25 +29,22 @@
3029
*
3130
* @internal
3231
*/
33-
class AccessDeniedListener extends ExceptionListener
32+
class AccessDeniedListener implements EventSubscriberInterface
3433
{
3534
private $formats;
3635
private $challenge;
3736

3837
/**
3938
* Constructor.
4039
*
41-
* @param array $formats An array with keys corresponding to request formats or content types
42-
* that must be processed by this listener
43-
* @param string $challenge
44-
* @param string $controller
45-
* @param LoggerInterface $logger
40+
* @param array $formats An array with keys corresponding to request formats or content types
41+
* that must be processed by this listener
42+
* @param string $challenge
4643
*/
47-
public function __construct($formats, $challenge, $controller, LoggerInterface $logger = null)
44+
public function __construct($formats, $challenge)
4845
{
4946
$this->formats = $formats;
5047
$this->challenge = $challenge;
51-
parent::__construct($controller, $logger);
5248
}
5349

5450
public function onKernelException(GetResponseForExceptionEvent $event)
@@ -76,15 +72,13 @@ public function onKernelException(GetResponseForExceptionEvent $event)
7672
if ($exception instanceof AccessDeniedException) {
7773
$exception = new AccessDeniedHttpException('You do not have the necessary permissions', $exception);
7874
$event->setException($exception);
79-
parent::onKernelException($event);
8075
} elseif ($exception instanceof AuthenticationException) {
8176
if ($this->challenge) {
8277
$exception = new UnauthorizedHttpException($this->challenge, 'You are not authenticated', $exception);
8378
} else {
8479
$exception = new HttpException(401, 'You are not authenticated', $exception);
8580
}
8681
$event->setException($exception);
87-
parent::onKernelException($event);
8882
}
8983

9084
$handling = false;

Resources/config/access_denied_listener.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
<tag name="monolog.logger" channel="request" />
1212
<argument type="collection" /> <!-- formats -->
1313
<argument /> <!-- unauthorized challenge -->
14-
<argument>%twig.exception_listener.controller%</argument>
15-
<argument type="service" id="logger" on-invalid="null" />
1614
</service>
1715

1816
</services>

Resources/doc/3-listener-support.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ You need to enable this listener as follows, as it is disabled by default:
193193
# all requests using the 'json' format will return a 403 on an access denied violation
194194
json: true
195195
196-
It is also recommended to enable the exception controller described in the next chapter.
196+
Note: The access_denied_listener doesn't return a response itself and must be coupled with an exception listener returning a response (see the :doc:`FOSRestBundle exception controller <4-exception-controller-support>`. or the `twig exception controller`_).
197197

198198
Zone Listener
199199
=============
@@ -238,3 +238,4 @@ That was it!
238238
.. _`ParamConverters`: http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/converters.html
239239
.. _`mime type listener`: http://symfony.com/doc/current/cookbook/request/mime_type.html
240240
.. _`Test Cases for HTTP Test Cases for the HTTP WWW-Authenticate header field`: http://greenbytes.de/tech/tc/httpauth/
241+
.. _`twig exception controller`: https://symfony.com/doc/current/cookbook/controller/error_pages.html

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This document will be updated to list important BC breaks and behavioral changes
2727
* ``UNLOCK``
2828
* the ``RequestBodyParamConverter`` now has a priority of ``-50``
2929
* the ``RequestBodyParamConverter`` doesn't throw an exception anymore when a parameter is optional
30+
* removed the ability of the ``AccessDeniedListener`` to render a response. Use the FOSRestBundle or the twig exception controller in complement.
3031

3132
### upgrading from 1.5.*
3233

0 commit comments

Comments
 (0)