Skip to content

Commit

Permalink
[SecurityBundle] fix(security): user login programmatically with dedi…
Browse files Browse the repository at this point in the history
…cated user checker on a firewall
  • Loading branch information
94noni committed Apr 2, 2024
1 parent 91d708f commit 29a65f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
'security.authenticator.managers_locator' => service('security.authenticator.managers_locator')->ignoreOnInvalid(),
'request_stack' => service('request_stack'),
'security.firewall.map' => service('security.firewall.map'),
'security.user_checker' => service('security.user_checker'),
'event_dispatcher' => service('event_dispatcher'),
'security.firewall.event_dispatcher_locator' => service('security.firewall.event_dispatcher_locator'),
'security.csrf.token_manager' => service('security.csrf.token_manager')->ignoreOnInvalid(),
]),
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Event\ProgrammaticLoginEvent;
use Symfony\Component\Security\Core\Exception\LogicException;
use Symfony\Component\Security\Core\Exception\LogoutException;
use Symfony\Component\Security\Core\Security as LegacySecurity;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function login(UserInterface $user, ?string $authenticatorName = null, ?s

$authenticator = $this->getAuthenticator($authenticatorName, $firewallName);

$this->container->get('security.user_checker')->checkPreAuth($user);
$this->container->get('event_dispatcher')->dispatch(new ProgrammaticLoginEvent($user));

return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user, $authenticator, $request, $badges);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Event;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* This event is dispatch when login programmatically.
*
* @internal
*
* @author Antoine Makdessi <[email protected]>
*/
class ProgrammaticLoginEvent extends Event
{
private UserInterface $user;

public function __construct(UserInterface $user)
{
$this->user = $user;
}

public function getUser(): UserInterface
{
return $this->user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Core\Event\ProgrammaticLoginEvent;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
Expand Down Expand Up @@ -52,11 +53,17 @@ public function postCheckCredentials(AuthenticationSuccessEvent $event): void
$this->userChecker->checkPostAuth($user);
}

public function onProgrammaticLoginEvent(ProgrammaticLoginEvent $event): void
{
$this->userChecker->checkPreAuth($event->getUser());
}

public static function getSubscribedEvents(): array
{
return [
CheckPassportEvent::class => ['preCheckCredentials', 256],
AuthenticationSuccessEvent::class => ['postCheckCredentials', 256],
ProgrammaticLoginEvent::class => ['onProgrammaticLoginEvent', 256],
];
}
}

0 comments on commit 29a65f9

Please sign in to comment.