custom/plugins/BartCTwoFactorAuth/src/Subscriber/UserEdit.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BartCTwoFactorAuth\Subscriber;
  3. use PragmaRX\Google2FA\Google2FA;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\User\UserEntity;
  10. use Shopware\Core\System\User\UserEvents;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class UserEdit implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $tfaRepository;
  15.     public function __construct(EntityRepositoryInterface $tfaRepository)
  16.     {
  17.         $this->tfaRepository $tfaRepository;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  22.         return [
  23.             UserEvents::USER_LOADED_EVENT => 'onUserLoaded',
  24.         ];
  25.     }
  26.     public function onUserLoaded(EntityLoadedEvent $event)
  27.     {
  28.         $entities $event->getEntities();
  29.         if (count($entities) > 0) {
  30.             if (!($entities[0] instanceof UserEntity)) {
  31.                 return;
  32.             }
  33.             $user $entities[0];
  34.             $criteria = new Criteria();
  35.             $criteria->addFilter(new EqualsFilter('userId'$user->getId()));
  36.             $criteria->addAssociation('tfaExtension');
  37.             $tfaExtension $this->tfaRepository->search($criteria$event->getContext())->first();
  38.             if ($tfaExtension === null) {
  39.                 $this->tfaRepository->create([
  40.                     [
  41.                         'userId' => $user->getId(),
  42.                         'tfaStatus' => false,
  43.                     ]
  44.                 ], $event->getContext());
  45.             }
  46.         }
  47.     }
  48. }