src/EventSubscriber/CheckVerifiedUserSubscriber.php line 99
<?php/*** CheckVerifiedUserSubscriber.** @author SOLTIX <lukasz.m@soltix.pl>* @license <https://opensource.org/licenses/MIT> MIT*/declare(strict_types=1);namespace App\EventSubscriber;use App\Entity\User;use Doctrine\ORM\EntityManager;use Doctrine\Persistence\ManagerRegistry;use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvents;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Session\Session;use Symfony\Component\Security\Http\Event\LoginSuccessEvent;class CheckVerifiedUserSubscriber implements EventSubscriberInterface{/*** @var ManagerRegistry*/private ManagerRegistry $managerRegistry;/*** @param ManagerRegistry $managerRegistry*/public function __construct(ManagerRegistry $managerRegistry){$this->managerRegistry = $managerRegistry;}//end __construct()/*** Returns an array of event names this subscriber wants to listen to.** The array keys are event names and the value can be:** * The method name to call (priority defaults to 0)* * An array composed of the method name to call and the priority* * An array of arrays composed of the method names to call and respective* priorities, or 0 if unset** For instance:** * ['eventName' => 'methodName']* * ['eventName' => ['methodName', $priority]]* * ['eventName' => [['methodName1', $priority], ['methodName2']]]** The code must not depend on runtime state as it will only be called at compile time.* All logic depending on runtime state must be put into the individual methods handling the events.** @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>*/public static function getSubscribedEvents(){return [LoginSuccessEvent::class => 'onSuccessLogin',TwoFactorAuthenticationEvents::COMPLETE => 'onSuccessTwoFactorAuthentication',];}//end getSubscribedEvents()/*** @param LoginSuccessEvent $event** @return void*/public function onSuccessLogin(LoginSuccessEvent $event): void{$session = new Session();$user = $event->getUser();if ($user instanceof User && $user->isFirstLogin() === true) {$session->set('is_change_password', true);$user->setFirstLogin(false);$this->managerRegistry->getManager()->flush();} else {$session->set('is_change_password', false);}}//end onSuccessLogin()/*** @param TwoFactorAuthenticationEvent $event** @return void*/public function onSuccessTwoFactorAuthentication(TwoFactorAuthenticationEvent $event): void{$token = $event->getToken();$user = $token->getUser();$user?->setUse2FaCode(true);$this->managerRegistry->getManager()->flush();}//end onSuccessTwoFactorAuthentication()}//end class