src/Security/EventListener/UserResolveListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Security\EventListener;
  3. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  4. use Symfony\Component\Security\Core\User\UserProviderInterface;
  5. use Trikoder\Bundle\OAuth2Bundle\Event\UserResolveEvent;
  6. final class UserResolveListener
  7. {
  8.     private ?UserProviderInterface $userProvider;
  9.     private ?UserPasswordEncoderInterface $userPasswordEncoder;
  10.     public function __construct(UserProviderInterface $userProviderUserPasswordEncoderInterface $encoder)
  11.     {
  12.         $this->userProvider $userProvider;
  13.         $this->userPasswordEncoder $encoder;
  14.     }
  15.     /**
  16.      * @param UserResolveEvent $event
  17.      */
  18.     public function onUserResolve(UserResolveEvent $event): void
  19.     {
  20.         $user $this->userProvider->loadUserByUsername($event->getUsername());
  21.         if (null === $user) {
  22.             return;
  23.         }
  24.         if (!$this->userPasswordEncoder->isPasswordValid($user$event->getPassword())) {
  25.             return;
  26.         }
  27.         $event->setUser($user);
  28.     }
  29. }