vendor/sylius/sylius/src/Sylius/Bundle/UserBundle/EventListener/UpdateUserEncoderListener.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\UserBundle\EventListener;
  12. use Doctrine\Persistence\ObjectManager;
  13. use Sylius\Component\User\Model\UserInterface;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. final class UpdateUserEncoderListener
  16. {
  17.     public function __construct(
  18.         private ObjectManager $objectManager,
  19.         private string $recommendedEncoderName,
  20.         private string $className,
  21.         private string $interfaceName,
  22.         private string $passwordParameter,
  23.     ) {
  24.     }
  25.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  26.     {
  27.         $user $event->getAuthenticationToken()->getUser();
  28.         if (!$user instanceof UserInterface) {
  29.             return;
  30.         }
  31.         if (!$user instanceof $this->className || !$user instanceof $this->interfaceName) {
  32.             return;
  33.         }
  34.         if ($user->getEncoderName() === $this->recommendedEncoderName) {
  35.             return;
  36.         }
  37.         $request $event->getRequest();
  38.         $plainPassword $request->request->get($this->passwordParameter);
  39.         if (null === $plainPassword || '' === $plainPassword) {
  40.             return;
  41.         }
  42.         $user->setEncoderName($this->recommendedEncoderName);
  43.         $user->setPlainPassword((string) $plainPassword);
  44.         $this->objectManager->persist($user);
  45.         $this->objectManager->flush();
  46.     }
  47. }