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

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.     private ObjectManager $objectManager;
  18.     private string $recommendedEncoderName;
  19.     private string $className;
  20.     private string $interfaceName;
  21.     private string $passwordParameter;
  22.     public function __construct(
  23.         ObjectManager $objectManager,
  24.         string $recommendedEncoderName,
  25.         string $className,
  26.         string $interfaceName,
  27.         string $passwordParameter
  28.     ) {
  29.         $this->objectManager $objectManager;
  30.         $this->recommendedEncoderName $recommendedEncoderName;
  31.         $this->className $className;
  32.         $this->interfaceName $interfaceName;
  33.         $this->passwordParameter $passwordParameter;
  34.     }
  35.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  36.     {
  37.         $user $event->getAuthenticationToken()->getUser();
  38.         if (!$user instanceof UserInterface) {
  39.             return;
  40.         }
  41.         if (!$user instanceof $this->className || !$user instanceof $this->interfaceName) {
  42.             return;
  43.         }
  44.         if ($user->getEncoderName() === $this->recommendedEncoderName) {
  45.             return;
  46.         }
  47.         $request $event->getRequest();
  48.         $plainPassword $request->request->get($this->passwordParameter);
  49.         if (null === $plainPassword || '' === $plainPassword) {
  50.             return;
  51.         }
  52.         $user->setEncoderName($this->recommendedEncoderName);
  53.         $user->setPlainPassword((string) $plainPassword);
  54.         $this->objectManager->persist($user);
  55.         $this->objectManager->flush();
  56.     }
  57. }