From d552fddbac7ae10ffcbe78c92aa0dc2e68e7f2c4 Mon Sep 17 00:00:00 2001 From: Mees Hoogendoorn Date: Fri, 28 Jan 2022 16:28:30 +0100 Subject: [PATCH] Added feature with maintenance mode on store-api.pwa.cms-page-resolve route --- src/Pwa/Controller/PageController.php | 38 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Pwa/Controller/PageController.php b/src/Pwa/Controller/PageController.php index 595e781..3eb028c 100644 --- a/src/Pwa/Controller/PageController.php +++ b/src/Pwa/Controller/PageController.php @@ -14,6 +14,9 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use OpenApi\Annotations as OA; +use Shopware\Core\System\SalesChannel\SalesChannelEntity; +use Symfony\Component\HttpFoundation\IpUtils; +use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; /** * @RouteScope(scopes={"store-api"}) @@ -36,11 +39,13 @@ class PageController extends AbstractController */ private $pageLoaderContextBuilder; + /** * @var PageLoaderInterface[] */ private $pageLoaders; + public function __construct(PageLoaderContextBuilderInterface $pageLoaderContextBuilder, iterable $pageLoaders) { $this->pageLoaderContextBuilder = $pageLoaderContextBuilder; @@ -50,8 +55,7 @@ public function __construct(PageLoaderContextBuilderInterface $pageLoaderContext $this->pageLoaders[$pageLoader->getResourceType()] = $pageLoader; } } - - /** +/** * @Route("/store-api/pwa/page", name="store-api.pwa.cms-page-resolve", methods={"POST"}) * * @OA\Post( @@ -152,6 +156,11 @@ public function __construct(PageLoaderContextBuilderInterface $pageLoaderContext * description="The resource could not be resolved or no path is provided.", * ref="#/components/responses/404" * ), + * @OA\Response( + * response="503", + * description="The resource could is temporarily unavailable or under construction.", + * ref="#/components/responses/503" + * ), * ) * * Resolve a page for a given resource and resource identification or path @@ -161,8 +170,12 @@ public function __construct(PageLoaderContextBuilderInterface $pageLoaderContext * @param Request $request * @return CmsPageRouteResponse */ - public function resolve(Request $request, SalesChannelContext $context): CmsPageRouteResponse + public function resolve(Request $request, SalesChannelContext $context) { + if($this->checkMaintenanceActive($request) && !$this->checkClientIps($request)){ + throw new ServiceUnavailableHttpException(60, "This site is temporarily unavailable or under construction"); + } + /** @var PageLoaderContext $pageLoaderContext */ $pageLoaderContext = $this->pageLoaderContextBuilder->build($request, $context); @@ -204,4 +217,23 @@ private function getPageResult(PageLoaderInterface $pageLoader, PageLoaderContex return $pageResult; } + + private function checkMaintenanceActive(Request $request){ + $salesChannel = $this->getSalesChannelFromRequest($request); + return $salesChannel->isMaintenance(); + } + + private function checkClientIps(Request $request){ + $salesChannel = $this->getSalesChannelFromRequest($request); + return IpUtils::checkIp( + (string) $request->getClientIp(), + $salesChannel->getMaintenanceIpWhitelist() + ); + } + + private function getSalesChannelFromRequest(Request $request) :SalesChannelEntity + { + $salesChannelContext = $request->attributes->get("sw-sales-channel-context"); + return $salesChannelContext->getSalesChannel(); + } } \ No newline at end of file