custom/plugins/IwvTwoFactorAuthentication/src/Subscriber/UserEntitySubscriber.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Iwv\IwvTwoFactorAuthentication\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\System\User\UserEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class UserEntitySubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var EntityRepositoryInterface */
  14.     private $userRepository;
  15.     /** @var EntityRepositoryInterface */
  16.     private $customerRepository;
  17.     public function __construct(
  18.         EntityRepositoryInterface $userRepository,
  19.         EntityRepositoryInterface $customerRepository
  20.     ) {
  21.         $this->userRepository $userRepository;
  22.         $this->customerRepository $customerRepository;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             UserEvents::USER_WRITTEN_EVENT => 'onUserWrittenEvent',
  28.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWrittenEvent',
  29.         ];
  30.     }
  31.     public function onUserWrittenEvent(EntityWrittenEvent $event): void
  32.     {
  33.         $payloads $event->getPayloads();
  34.         /** @var \Shopware\Core\System\User\UserEntity $userDB */
  35.         $updateUsers = [];
  36.         $ids array_filter(array_map(fn($payload) => ($payload['id'] ?? ''), $payloads));
  37.         if ($ids) {
  38.             foreach ($this->userRepository->search(
  39.                 (new Criteria())->addFilter(new EqualsAnyFilter('id'$ids)),
  40.                 $event->getContext()
  41.             ) as $userDB) {
  42.                 $customFields = (array) $userDB->getCustomFields();
  43.                 if (array_key_exists('iwvTwoFactor'$customFields)) {
  44.                     if (empty($customFields['iwvTwoFactor'])) {
  45.                         unset($customFields['iwvTwoFactor']);
  46.                         unset($customFields['iwvTwoFactorCookie']);
  47.                         unset($customFields['iwvTwoFactorSession']);
  48.                         $updateUsers[] = [
  49.                             'id' => $userDB->getId(),
  50.                             'customFields' => $customFields
  51.                         ];
  52.                     } elseif (!array_key_exists('iwvTwoFactorCookie'$customFields)) {
  53.                         $customFields['iwvTwoFactorCookie'] = bin2hex(random_bytes(25));
  54.                         $updateUsers[] = [
  55.                             'id' => $userDB->getId(),
  56.                             'customFields' => $customFields
  57.                         ];
  58.                     }
  59.                 }
  60.             }
  61.             if ($updateUsers) {
  62.                 $this->userRepository->update($updateUsers$event->getContext());
  63.             }
  64.         }
  65.     }
  66.     public function onCustomerWrittenEvent(EntityWrittenEvent $event): void
  67.     {
  68.         $payloads $event->getPayloads();
  69.         /** @var \Shopware\Core\Checkout\Customer\CustomerEntity $userDB */
  70.         $updateCustomers = [];
  71.         $ids array_map(fn($payload) => $payload['id'] ?? null$payloads);
  72.         $ids array_filter($ids, fn($id) => $id !== null);
  73.         if (!empty($ids)) {
  74.             $criteria = new Criteria();
  75.             $criteria->addFilter(new EqualsAnyFilter('id'$ids));
  76.             foreach ($this->customerRepository->search($criteria$event->getContext()) as $userDB) {
  77.                 $customFields = (array) $userDB->getCustomFields();
  78.                 if (array_key_exists('iwvTwoFactor'$customFields)) {
  79.                     if (empty($customFields['iwvTwoFactor'])) {
  80.                         unset($customFields['iwvTwoFactor']);
  81.                         unset($customFields['iwvTwoFactorCookie']);
  82.                         unset($customFields['iwvTwoFactorSession']);
  83.                         $updateCustomers[] = [
  84.                             'id' => $userDB->getId(),
  85.                             'customFields' => $customFields
  86.                         ];
  87.                     } elseif (!array_key_exists('iwvTwoFactorCookie'$customFields)) {
  88.                         $customFields['iwvTwoFactorCookie'] = bin2hex(random_bytes(25));
  89.                         $updateCustomers[] = [
  90.                             'id' => $userDB->getId(),
  91.                             'customFields' => $customFields
  92.                         ];
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         if ($updateCustomers) {
  98.             $this->customerRepository->update($updateCustomers$event->getContext());
  99.         }
  100.     }
  101. }