Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Add a ChainUserChecker to allow calling multiple user checkers for a firewall #46064

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
Sep 13, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -44,6 +45,7 @@
use Symfony\Component\Security\Core\Authorization\Strategy\PriorityStrategy;
use Symfony\Component\Security\Core\Authorization\Strategy\UnanimousStrategy;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\User\ChainUserChecker;
use Symfony\Component\Security\Core\User\ChainUserProvider;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand Down Expand Up @@ -385,6 +387,10 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
]))
;

// Register Firewall-specific chained user checker
$container->register('security.user_checker.chain.'.$id, ChainUserChecker::class)
->addArgument(new TaggedIteratorArgument('security.user_checker.'.$id));

// Register listeners
$listeners = [];
$listenerKeys = [];
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Deprecate the `Security` class, use `Symfony\Bundle\SecurityBundle\Security\Security` instead
* Change the signature of `TokenStorageInterface::setToken()` to `setToken(?TokenInterface $token)`
* Deprecate calling `TokenStorage::setToken()` without arguments
* Add a `ChainUserChecker` to allow calling multiple user checkers for a firewall

6.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Core\Tests\User;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\ChainUserChecker;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

final class ChainUserCheckerTest extends TestCase
{
public function testForwardsPreAuthToAllUserCheckers()
{
$user = $this->createMock(UserInterface::class);

$checker1 = $this->createMock(UserCheckerInterface::class);
$checker1->expects($this->once())
->method('checkPreAuth')
->with($user);

$checker2 = $this->createMock(UserCheckerInterface::class);
$checker2->expects($this->once())
->method('checkPreAuth')
->with($user);

$checker3 = $this->createMock(UserCheckerInterface::class);
$checker3->expects($this->once())
->method('checkPreAuth')
->with($user);

(new ChainUserChecker([$checker1, $checker2, $checker3]))->checkPreAuth($user);
}

public function testForwardsPostAuthToAllUserCheckers()
{
$user = $this->createMock(UserInterface::class);

$checker1 = $this->createMock(UserCheckerInterface::class);
$checker1->expects($this->once())
->method('checkPostAuth')
->with($user);

$checker2 = $this->createMock(UserCheckerInterface::class);
$checker2->expects($this->once())
->method('checkPostAuth')
->with($user);

$checker3 = $this->createMock(UserCheckerInterface::class);
$checker3->expects($this->once())
->method('checkPostAuth')
->with($user);

(new ChainUserChecker([$checker1, $checker2, $checker3]))->checkPostAuth($user);
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Security/Core/User/ChainUserChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Core\User;

final class ChainUserChecker implements UserCheckerInterface
{
/**
* @param iterable<UserCheckerInterface> $checkers
*/
public function __construct(private readonly iterable $checkers)
{
}

public function checkPreAuth(UserInterface $user): void
{
foreach ($this->checkers as $checker) {
$checker->checkPreAuth($user);
}
}

public function checkPostAuth(UserInterface $user): void
{
foreach ($this->checkers as $checker) {
$checker->checkPostAuth($user);
}
}
}