src/Core/Security/Subscriber/RememberMeLoginSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Core\Security\Subscriber;
  3. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
  6. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  7. use Symfony\Component\Security\Http\SecurityEvents;
  8. /**
  9.  * Class CancelSubscriptionSubscriber
  10.  * @package Harmonizely\Service\Subscription\Subscriber
  11.  */
  12. class RememberMeLoginSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ISecurityHelper
  16.      */
  17.     private ISecurityHelper $securityHelper;
  18.     /**
  19.      * ReceiveSubscriptionSubscriber constructor.
  20.      * @param ISecurityHelper $securityHelper
  21.      */
  22.     public function __construct(ISecurityHelper $securityHelper)
  23.     {
  24.         $this->securityHelper $securityHelper;
  25.     }
  26.     /**
  27.      * @return string[]
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  33.         ];
  34.     }
  35.     /**
  36.      * @param InteractiveLoginEvent $event
  37.      * @return void
  38.      */
  39.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  40.     {
  41.         $token $event->getAuthenticationToken();
  42.         if ($token instanceof RememberMeToken) {
  43.             $user $this->securityHelper->getUser();
  44.             $this->securityHelper->setLoginType($tokenISecurityHelper::LOGIN_TYPE_REMEMBER_ME);
  45.             if ($user) {
  46.                 $codeFromCookie $event->getRequest()->cookies->get(ISecurityHelper::LAST_TWO_FACTOR_CODE);
  47.                 $codeFromDb $user->getLastTwoFactorCode();
  48.                 if ($codeFromCookie && $codeFromDb) {
  49.                     if ($codeFromCookie === $codeFromDb) {
  50.                         $this->securityHelper->setSecondFactorPassed($tokentrue);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }