10000 [SecurityBundle] Fix UserCheckerListener registration with custom user checker by wouterj · Pull Request #37366 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[SecurityBundle] Fix UserCheckerListener registration with custom user checker #37366

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
Jun 20, 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 8000
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 @@ -478,6 +478,12 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
->replaceArgument(0, new Reference($managerId))
;

// user checker listener
$container
->setDefinition('security.listener.user_checker.'.$id, new ChildDefinition('security.listener.user_checker'))
->replaceArgument(0, new Reference('security.user_checker.'.$id))
->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);

$listeners[] = new Reference('security.firewall.authenticator.'.$id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@
<argument type="service" id="security.encoder_factory" />
</service>

<service id="security.listener.user_checker" class="Symfony\Component\Security\Http\EventListener\UserCheckerListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="Symfony\Component\Security\Core\User\UserCheckerInterface" />
<service id="security.listener.user_checker" class="Symfony\Component\Security\Http\EventListener\UserCheckerListener" abstract="true">
<argument type="abstract">user checker</argument>
</service>

<service id="security.listener.session"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface as GuardAuthenticatorInterface;
Expand Down Expand Up @@ -509,7 +511,7 @@ public function provideEntryPointRequiredData()
];
}

public function testAlwaysAuthenticateBeforeGrantingCannotBeTrueWithAuthenticationManager()
public function testAlwaysAuthenticateBeforeGrantingCannotBeTrueWithAuthenticatorManager()
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The security option "always_authenticate_before_granting" cannot be used when "enable_authenticator_manager" is set to true. If you rely on this behavior, set it to false.');
Expand Down Expand Up @@ -559,7 +561,7 @@ public function provideConfigureCustomAuthenticatorData()
];
}

public function testCompilesWithoutSessionListenerWithStatelessFirewallWithAuthenticationManager()
public function testCompilesWithoutSessionListenerWithStatelessFirewallWithAuthenticatorManager()
{
$container = $this->getRawContainer();

Expand All @@ -580,7 +582,7 @@ public function testCompilesWithoutSessionListenerWithStatelessFirewallWithAuthe
$this->assertFalse($container->has('security.listener.session.'.$firewallId));
}

public function testCompilesWithSessionListenerWithStatefulllFirewallWithAuthenticationManager()
public function testCompilesWithSessionListenerWithStatefulllFirewallWithAuthenticatorManager()
{
$container = $this->getRawContainer();

Expand All @@ -601,6 +603,37 @@ public function testCompilesWithSessionListenerWithStatefulllFirewallWithAuthent
$this->assertTrue($container->has('security.listener.session.'.$firewallId));
}

/**
* @dataProvider provideUserCheckerConfig
*/
public function testUserCheckerWithAuthenticatorManager(array $config, string $expectedUserCheckerClass)
{
$container = $this->getRawContainer();
$container->register(TestUserChecker::class);

$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'firewalls' => [
'main' => array_merge([
'pattern' => '/.*',
'http_basic' => true,
], $config),
],
]);

$container->compile();

$userCheckerId = (string) $container->getDefinition('security.listener.user_checker.main')->getArgument(0);
$this->assertTrue($container->has($userCheckerId));
$this->assertEquals($expectedUserCheckerClass, $container->findDefinition($userCheckerId)->getClass());
}

public function provideUserCheckerConfig()
{
yield [[], UserChecker::class];
yield [['user_checker' => TestUserChecker::class], TestUserChecker::class];
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -689,3 +722,14 @@ public function supportsRememberMe()
{
}
}

class TestUserChecker implements UserCheckerInterface
{
public function checkPreAuth(UserInterface $user)
{
}

public function checkPostAuth(UserInterface $user)
{
}
}
0