8000 [Security] Move AbstractListener abstract methods to the new FirewallListenerInterface by chalasr · Pull Request #38751 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Move AbstractListener abstract methods to the new FirewallListenerInterface #38751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Security] Move AbstractListener abstract methods to the new Firewall…
…ListenerInterface
  • Loading branch information
chalasr committed Oct 25, 2020
commit 5dd70bd62e7e6ce50743a24d5c9db9be0ddfa1db
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Bundle\SecurityBundle\Security\LazyFirewallContext;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;

/**
* Firewall collecting called listeners.
Expand All @@ -41,7 +41,7 @@ protected function callListeners(RequestEvent $event, iterable $listeners)
\Closure::bind(function () use (&$wrappedLazyListeners, &$wrappedListeners) {
$listeners = [];
foreach ($this->listeners as $listener) {
if ($listener instanceof AbstractListener) {
if ($listener instanceof FirewallListenerInterface) {
$listener = new WrappedLazyListener($listener);
$listeners[] = $listener;
$wrappedLazyListeners[] = $listener;
Expand All @@ -58,7 +58,7 @@ protected function callListeners(RequestEvent $event, iterable $listeners)

$listener($event);
} else {
$wrappedListener = $listener instanceof AbstractListener ? new WrappedLazyListener($listener) : new WrappedListener($listener);
$wrappedListener = $listener instanceof FirewallListenerInterface ? new WrappedLazyListener($listener) : new WrappedListener($listener);
$wrappedListener($event);
$wrappedListeners[] = $wrappedListener->getInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Exception\LazyResponseException;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;

/**
* Wraps a lazy security listener.
Expand All @@ -27,7 +28,7 @@ final class WrappedLazyListener extends AbstractListener
{
use TraceableListenerTrait;

public function __construct(AbstractListener $listener)
public function __construct(FirewallListenerInterface $listener)
{
$this->listener = $listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Http\Event\LazyResponseEvent;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
use Symfony\Component\Security\Http\Firewall\LogoutListener;

/**
Expand Down Expand Up @@ -46,9 +46,9 @@ public function __invoke(RequestEvent $event)
$lazy = $request->isMethodCacheable();

foreach (parent::getListeners() as $listener) {
if (!$lazy || !$listener instanceof AbstractListener) {
if (!$lazy || !$listener instanceof FirewallListenerInterface) {
$listeners[] = $listener;
$lazy = $lazy && $listener instanceof AbstractListener;
$lazy = $lazy && $listener instanceof FirewallListenerInterface;
} elseif (false !== $supports = $listener->supports($request)) {
$listeners[] = [$listener, 'authenticate'];
$lazy = null === $supports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;

class SortFirewallListenersPassTest extends TestCase
Expand Down Expand Up @@ -59,6 +61,14 @@ public function testSortFirewallListeners()

class FirewallListenerPriorityMinus1 implements FirewallListenerInterface
{
public function supports(Request $request): ?bool
{
}

public function authenticate(RequestEvent $event)
{
}

public static function getPriority(): int
{
return -1;
Expand All @@ -67,6 +77,14 @@ public static function getPriority(): int

class FirewallListenerPriority1 implements FirewallListenerInterface
{
public function supports(Request $request): ?bool
{
}

public function authenticate(RequestEvent $event)
{
}

public static function getPriority(): int
{
return 1;
Expand All @@ -75,6 +93,14 @@ public static function getPriority(): int

class FirewallListenerPriority2 implements FirewallListenerInterface
{
public function supports(Request $request): ?bool
{
}

public function authenticate(RequestEvent $event)
{
}

public static function getPriority(): int
{
return 2;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHANGELOG
* Added a CurrentUser attribute to force the UserValueResolver to resolve an argument to the current user.
* Added `LoginThrottlingListener`.
* Added `LoginLinkAuthenticator`.
* Moved methods `supports()` and `authenticate()` from `AbstractListener` to `FirewallListenerInterface`.

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;

/**
* @author Wouter de Jong <wouter@wouterj.nl>
Expand All @@ -26,7 +26,7 @@ interface AuthenticatorManagerInterface
/**
* Called to see if authentication should be attempted on this request.
*
* @see AbstractListener::supports()
* @see FirewallListenerInterface::supports()
*/
public function supports(Request $request): ?bool;

Expand Down
13 changes: 0 additions & 13 deletions src/Symfony/Component/Security/Http/Firewall/AbstractListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Security\Http\Firewall;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
Expand All @@ -28,18 +27,6 @@ final public function __invoke(RequestEvent $event)
}
}

/**
* Tells whether the authenticate() method should be called or not depending on the incoming request.
*
* Returning null means authenticate() can be called lazily when accessing the token storage.
*/
abstract public function supports(Request $request): ?bool;

/**
* Does whatever is required to authenticate the request, typically calling $event->setResponse() internally.
*/
abstract public function authenticate(RequestEvent $event);

public static function getPriority(): int
{
return 0; // Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@

namespace Symfony\Component\Security\Http\Firewall;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
* Can be implemented by firewall listeners to define their priority in execution.
* Can be implemented by firewall listeners.
*
* @author Christian Scheb <me@christianscheb.de>
* @author Nicolas Grekas <p@tchwork.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
interface FirewallListenerInterface
{
/**
* Tells whether the authenticate() method should be called or not depending on the incoming request.
*
* Returning null means authenticate() can be called lazily when accessing the token storage.
*/
public function supports(Request $request): ?bool;

/**
* Does whatever is required to authenticate the request, typically calling $event->setResponse() internally.
*/
public function authenticate(RequestEvent $event);

/**
* Defines the priority of the listener.
* The higher the number, the earlier a listener is executed.
Expand Down
0