Skip to content

Commit

Permalink
Disallow access to a page if its root page is not available for the c…
Browse files Browse the repository at this point in the history
…urrent country
  • Loading branch information
aschempp committed Sep 6, 2024
1 parent 7977f75 commit 69f8c13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ need a commercial license of this product depending on your use case!
set to the visitors country.


### Note on page visibility

If the visibility of a root page is configured, it also affects all its subpages. This means
if a root page is not available for a country, none of the pages in this tree will be available.

Enabling this on the fallback root page can lead to unwanted consequences, because the user will
not be redirected to **any** page if none of the preferred languages match the browser!


## Installation

Choose the installation method that matches your workflow!
Expand Down
27 changes: 21 additions & 6 deletions src/Routing/CountryRestrictionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,40 @@ public function __construct(private readonly CountryProvider $countryProvider)

public function filter(RouteCollection $collection, Request $request): RouteCollection
{
$country = $this->countryProvider->getCountryCode($request);

foreach ($collection as $name => $route) {
$pageModel = $route->getDefault('pageModel');

if (!$pageModel instanceof PageModel) {
continue;
}

if ('show' !== $pageModel->geoip_visibility && 'hide' !== $pageModel->geoip_visibility) {
continue;
if (!$this->isAvailable($pageModel, $country)) {
$collection->remove($name);
}

$countries = explode(',', (string) $pageModel->geoip_countries);
$country = $this->countryProvider->getCountryCode($request);
// Disallow access to a page if its root page is not available for the current country
if ('root' !== $pageModel->type) {
$rootModel = PageModel::findById($pageModel->loadDetails()->rootId);

if (\in_array($country, $countries, true) !== ('show' === $pageModel->geoip_visibility)) {
$collection->remove($name);
if ($rootModel && !$this->isAvailable($rootModel, $country)) {
$collection->remove($name);
}
}
}

return $collection;
}

private function isAvailable(PageModel $pageModel, string $country): bool
{
if ('show' !== $pageModel->geoip_visibility && 'hide' !== $pageModel->geoip_visibility) {
return true;
}

$countries = explode(',', (string) $pageModel->geoip_countries);

return \in_array($country, $countries, true) === ('show' === $pageModel->geoip_visibility);
}
}

0 comments on commit 69f8c13

Please sign in to comment.