<?php
namespace App\Security\EventListener;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\UserResolveEvent;
final class UserResolveListener
{
private ?UserProviderInterface $userProvider;
private ?UserPasswordEncoderInterface $userPasswordEncoder;
public function __construct(UserProviderInterface $userProvider, UserPasswordEncoderInterface $encoder)
{
$this->userProvider = $userProvider;
$this->userPasswordEncoder = $encoder;
}
/**
* @param UserResolveEvent $event
*/
public function onUserResolve(UserResolveEvent $event): void
{
$user = $this->userProvider->loadUserByUsername($event->getUsername());
if (null === $user) {
return;
}
if (!$this->userPasswordEncoder->isPasswordValid($user, $event->getPassword())) {
return;
}
$event->setUser($user);
}
}