src/Core/Security/Subscriber/SetSecondFactorPassedSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Core\Security\Subscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  5. use Harmonizely\Core\Security\Event\UserSecondFactorEvent;
  6. use Harmonizely\Model\UserInterface;
  7. use Harmonizely\Service\Base\Crypt\Contract\ICrypt;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. /**
  11.  * Class CancelSubscriptionSubscriber
  12.  * @package Harmonizely\Service\Subscription\Subscriber
  13.  */
  14. class SetSecondFactorPassedSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var RequestStack
  18.      */
  19.     private RequestStack $requestStack;
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private EntityManagerInterface $entityManager;
  24.     /**
  25.      * @var ICrypt
  26.      */
  27.     private ICrypt $crypt;
  28.     /**
  29.      * ReceiveSubscriptionSubscriber constructor.
  30.      * @param RequestStack $requestStack
  31.      * @param EntityManagerInterface $entityManager
  32.      * @param ICrypt $crypt
  33.      */
  34.     public function __construct(
  35.         RequestStack $requestStack,
  36.         EntityManagerInterface $entityManager,
  37.         ICrypt $crypt
  38.     )
  39.     {
  40.         $this->requestStack $requestStack;
  41.         $this->entityManager $entityManager;
  42.         $this->crypt $crypt;
  43.     }
  44.     /**
  45.      * @return string[]
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             UserSecondFactorEvent::NAME => 'onSecondFactorPassed',
  51.         ];
  52.     }
  53.     /**
  54.      * @param UserSecondFactorEvent $event
  55.      * @return void
  56.      */
  57.     public function onSecondFactorPassed(UserSecondFactorEvent $event): void
  58.     {
  59.         if ($event->isPassed()) {
  60.             $user $event->getToken()->getUser();
  61.             if ($user instanceof UserInterface) {
  62.                 $request $this->requestStack->getCurrentRequest();
  63.                 $code $request->get('code');
  64.                 if ($code && strlen($code) === 6) {
  65.                     $codeWithSalt $this->crypt->encrypt($code);
  66.                     $user->setLastTwoFactorCode($codeWithSalt);
  67.                     setcookie(ISecurityHelper::LAST_TWO_FACTOR_CODE$codeWithSalt, [
  68.                         'expires' => time() + 86400,
  69.                         'path' => '/',
  70.                         'secure' => true,
  71.                         'httponly' => true,
  72.                     ]);
  73.                     $this->entityManager->flush();
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }