Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature with maintenance mode on store-api.pwa.cms-page-resolve route #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/Pwa/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand All @@ -36,11 +39,13 @@ class PageController extends AbstractController
*/
private $pageLoaderContextBuilder;


/**
* @var PageLoaderInterface[]
*/
private $pageLoaders;


public function __construct(PageLoaderContextBuilderInterface $pageLoaderContextBuilder, iterable $pageLoaders)
{
$this->pageLoaderContextBuilder = $pageLoaderContextBuilder;
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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();
}
}