vendor/shopware/core/Checkout/Customer/Subscriber/CustomerTokenSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class CustomerTokenSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SalesChannelContextPersister
  15.      */
  16.     private $contextPersister;
  17.     /**
  18.      * @var RequestStack
  19.      */
  20.     private $requestStack;
  21.     public function __construct(
  22.         SalesChannelContextPersister $contextPersister,
  23.         RequestStack $requestStack
  24.     ) {
  25.         $this->contextPersister $contextPersister;
  26.         $this->requestStack $requestStack;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  32.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  33.         ];
  34.     }
  35.     public function onCustomerWritten(EntityWrittenEvent $event): void
  36.     {
  37.         $master $this->requestStack->getMasterRequest();
  38.         if (!$master) {
  39.             return;
  40.         }
  41.         if (!$master->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  42.             return;
  43.         }
  44.         /** @var SalesChannelContext $context */
  45.         $context $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  46.         $token $context->getToken();
  47.         $payloads $event->getPayloads();
  48.         foreach ($payloads as $payload) {
  49.             if ($this->customerCredentialsChanged($payload)) {
  50.                 $this->contextPersister->revokeAllCustomerTokens($payload['id'], $token);
  51.             }
  52.         }
  53.     }
  54.     public function onCustomerDeleted(EntityDeletedEvent $event): void
  55.     {
  56.         $master $this->requestStack->getMasterRequest();
  57.         if (!$master) {
  58.             return;
  59.         }
  60.         $customerIds $event->getIds();
  61.         foreach ($customerIds as $customerId) {
  62.             $this->contextPersister->revokeAllCustomerTokens($customerId);
  63.         }
  64.     }
  65.     private function customerCredentialsChanged(array $payload): bool
  66.     {
  67.         return isset($payload['password']);
  68.     }
  69. }