custom/plugins/PickwareWms/src/WmsAppVersionValidation/WmsAppVersionValidationSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareWms\WmsAppVersionValidation;
  11. use Pickware\HttpUtils\JsonApi\JsonApiError;
  12. use Pickware\ShopwareExtensionsBundle\Context\ContextExtension;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  15. use Shopware\Core\PlatformRequest;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. class WmsAppVersionValidationSubscriber implements EventSubscriberInterface
  20. {
  21.     private string $requiredMinimumAppVersion;
  22.     public function __construct(string $requiredMinimumAppVersion)
  23.     {
  24.         $this->requiredMinimumAppVersion $requiredMinimumAppVersion;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::CONTROLLER => [
  30.                 'validateAppVersion',
  31.                 KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_CONTEXT_RESOLVE_POST,
  32.             ],
  33.         ];
  34.     }
  35.     public function validateAppVersion(ControllerEvent $event): void
  36.     {
  37.         $userAgentString $event->getRequest()->headers->get('user-agent');
  38.         if ($userAgentString === null) {
  39.             return;
  40.         }
  41.         // Expected string format: "WMS/1.8.4 (com.pickware.wms; build:202305151616; iOS 16.5.0) Alamofire/5.6.3"
  42.         // Since all requests are caught, we need to make sure that the request was sent by the WMS app
  43.         $pattern '|^WMS/(\\d+\\.\\d+\\.\\d+)|';
  44.         if (!preg_match($pattern$userAgentString$matches)) {
  45.             return;
  46.         }
  47.         // We allow the app to authenticate first because otherwise the app might obfuscate the provided error.
  48.         /** @var Context $context */
  49.         $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  50.         if (!$context || !ContextExtension::hasUser($context)) {
  51.             return;
  52.         }
  53.         $receivedAppVersion $matches[1];
  54.         if (version_compare($receivedAppVersion$this->requiredMinimumAppVersion'<')) {
  55.             $apiError = new JsonApiError([
  56.                 'status' => 400,
  57.                 'code' => 'PICKWARE_WMS__USER_AGENT_VALIDATION__OUTDATED_APP_VERSION',
  58.                 'title' => 'Your app version is outdated',
  59.                 'detail' => 'This shop requires a newer version of the app. Please update your app through the App Store.',
  60.             ]);
  61.             $event->setController(fn () => $apiError->toJsonApiErrorResponse());
  62.         }
  63.     }
  64. }