src/EventListener/UserResolveListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  4. use Symfony\Component\Security\Core\User\UserProviderInterface;
  5. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent;
  8. final class UserResolveListener
  9. {
  10.     /**
  11.      * @var UserProviderInterface
  12.      */
  13.     private $userProvider;
  14.     /**
  15.      * @var UserPasswordHasherInterface
  16.      */
  17.     private $userPasswordHasher;
  18.     public function __construct(UserProviderInterface $userProviderUserPasswordHasherInterface $userPasswordHasher)
  19.     {
  20.         $this->userProvider $userProvider;
  21.         $this->userPasswordHasher $userPasswordHasher;
  22.     }
  23.     public function onUserResolve(UserResolveEvent $event): void
  24.     {
  25.         try {
  26.             $user $this->userProvider->loadUserByIdentifier($event->getUsername());
  27.         } catch (AuthenticationException $e) {
  28.             return;
  29.         }
  30.         if (null === $user || !($user instanceof PasswordAuthenticatedUserInterface)) {
  31.             return;
  32.         }
  33.         if (!$this->userPasswordHasher->isPasswordValid($user$event->getPassword())) {
  34.             return;
  35.         }
  36.         $event->setUser($user);
  37.     }
  38. }