vendor/symfony/security-http/EventListener/UserCheckerListener.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  13. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  17. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  18. /**
  19. * @author Wouter de Jong <wouter@wouterj.nl>
  20. *
  21. * @final
  22. */
  23. class UserCheckerListener implements EventSubscriberInterface
  24. {
  25. private $userChecker;
  26. public function __construct(UserCheckerInterface $userChecker)
  27. {
  28. $this->userChecker = $userChecker;
  29. }
  30. public function preCheckCredentials(CheckPassportEvent $event): void
  31. {
  32. $passport = $event->getPassport();
  33. if (!$passport instanceof UserPassportInterface || $passport->hasBadge(PreAuthenticatedUserBadge::class)) {
  34. return;
  35. }
  36. $this->userChecker->checkPreAuth($passport->getUser());
  37. }
  38. public function postCheckCredentials(AuthenticationSuccessEvent $event): void
  39. {
  40. $user = $event->getAuthenticationToken()->getUser();
  41. if (!$user instanceof UserInterface) {
  42. return;
  43. }
  44. $this->userChecker->checkPostAuth($user);
  45. }
  46. public static function getSubscribedEvents(): array
  47. {
  48. return [
  49. CheckPassportEvent::class => ['preCheckCredentials', 256],
  50. AuthenticationSuccessEvent::class => ['postCheckCredentials', 256],
  51. ];
  52. }
  53. }