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

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\EntityWriteResult;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. /**
  13.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  14.  */
  15. class CustomerTokenSubscriber implements EventSubscriberInterface
  16. {
  17.     private SalesChannelContextPersister $contextPersister;
  18.     private RequestStack $requestStack;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         SalesChannelContextPersister $contextPersister,
  24.         RequestStack $requestStack
  25.     ) {
  26.         $this->contextPersister $contextPersister;
  27.         $this->requestStack $requestStack;
  28.     }
  29.     /**
  30.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  31.      */
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  36.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  37.         ];
  38.     }
  39.     public function onCustomerWritten(EntityWrittenEvent $event): void
  40.     {
  41.         foreach ($event->getWriteResults() as $writeResult) {
  42.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_UPDATE) {
  43.                 continue;
  44.             }
  45.             $payload $writeResult->getPayload();
  46.             if (!$this->customerCredentialsChanged($payload)) {
  47.                 continue;
  48.             }
  49.             $customerId $payload['id'];
  50.             $newToken $this->invalidateUsingSession($customerId);
  51.             if ($newToken) {
  52.                 $this->contextPersister->revokeAllCustomerTokens($customerId$newToken);
  53.             } else {
  54.                 $this->contextPersister->revokeAllCustomerTokens($customerId);
  55.             }
  56.         }
  57.     }
  58.     public function onCustomerDeleted(EntityDeletedEvent $event): void
  59.     {
  60.         foreach ($event->getIds() as $customerId) {
  61.             $this->contextPersister->revokeAllCustomerTokens($customerId);
  62.         }
  63.     }
  64.     /**
  65.      * @param array<string, mixed> $payload
  66.      */
  67.     private function customerCredentialsChanged(array $payload): bool
  68.     {
  69.         return isset($payload['password']);
  70.     }
  71.     private function invalidateUsingSession(string $customerId): ?string
  72.     {
  73.         $master $this->requestStack->getMainRequest();
  74.         if (!$master) {
  75.             return null;
  76.         }
  77.         // Is not a storefront request
  78.         if (!$master->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  79.             return null;
  80.         }
  81.         /** @var SalesChannelContext $context */
  82.         $context $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  83.         // Not loggedin skip
  84.         if ($context->getCustomer() === null) {
  85.             return null;
  86.         }
  87.         // The written customer is not the same as logged-in. We don't modify the user session
  88.         if ($context->getCustomer()->getId() !== $customerId) {
  89.             return null;
  90.         }
  91.         $token $context->getToken();
  92.         $newToken $this->contextPersister->replace($token$context);
  93.         $context->assign([
  94.             'token' => $newToken,
  95.         ]);
  96.         if (!$master->hasSession()) {
  97.             return null;
  98.         }
  99.         $session $master->getSession();
  100.         $session->migrate();
  101.         $session->set('sessionId'$session->getId());
  102.         $session->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  103.         $master->headers->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  104.         return $newToken;
  105.     }
  106. }