src/EventSubscriber/CheckVerifiedUserSubscriber.php line 99

  1. <?php
  2. /**
  3.  * CheckVerifiedUserSubscriber.
  4.  *
  5.  * @author  SOLTIX <lukasz.m@soltix.pl>
  6.  * @license <https://opensource.org/licenses/MIT> MIT
  7.  */
  8. declare(strict_types=1);
  9. namespace App\EventSubscriber;
  10. use App\Entity\User;
  11. use Doctrine\ORM\EntityManager;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;
  14. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
  15. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvents;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  19. class CheckVerifiedUserSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var ManagerRegistry
  23.      */
  24.     private ManagerRegistry $managerRegistry;
  25.     /**
  26.      * @param ManagerRegistry $managerRegistry
  27.      */
  28.     public function __construct(ManagerRegistry $managerRegistry)
  29.     {
  30.         $this->managerRegistry $managerRegistry;
  31.     }//end __construct()
  32.     /**
  33.      * Returns an array of event names this subscriber wants to listen to.
  34.      *
  35.      * The array keys are event names and the value can be:
  36.      *
  37.      *  * The method name to call (priority defaults to 0)
  38.      *  * An array composed of the method name to call and the priority
  39.      *  * An array of arrays composed of the method names to call and respective
  40.      *    priorities, or 0 if unset
  41.      *
  42.      * For instance:
  43.      *
  44.      *  * ['eventName' => 'methodName']
  45.      *  * ['eventName' => ['methodName', $priority]]
  46.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  47.      *
  48.      * The code must not depend on runtime state as it will only be called at compile time.
  49.      * All logic depending on runtime state must be put into the individual methods handling the events.
  50.      *
  51.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  52.      */
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             LoginSuccessEvent::class                => 'onSuccessLogin',
  57.             TwoFactorAuthenticationEvents::COMPLETE => 'onSuccessTwoFactorAuthentication',
  58.         ];
  59.     }//end getSubscribedEvents()
  60.     /**
  61.      * @param LoginSuccessEvent $event
  62.      *
  63.      * @return void
  64.      */
  65.     public function onSuccessLogin(LoginSuccessEvent $event): void
  66.     {
  67.         $session = new Session();
  68.         $user    $event->getUser();
  69.         if ($user instanceof User && $user->isFirstLogin() === true) {
  70.             $session->set('is_change_password'true);
  71.             $user->setFirstLogin(false);
  72.             $this->managerRegistry->getManager()->flush();
  73.         } else {
  74.             $session->set('is_change_password'false);
  75.         }
  76.     }//end onSuccessLogin()
  77.     /**
  78.      * @param TwoFactorAuthenticationEvent $event
  79.      *
  80.      * @return void
  81.      */
  82.     public function onSuccessTwoFactorAuthentication(TwoFactorAuthenticationEvent $event): void
  83.     {
  84.         $token $event->getToken();
  85.         $user  $token->getUser();
  86.         $user?->setUse2FaCode(true);
  87.         $this->managerRegistry->getManager()->flush();
  88.     }//end onSuccessTwoFactorAuthentication()
  89. }//end class