add forced domain

This commit is contained in:
Simon Vieille 2021-05-18 13:26:38 +02:00
parent 377abaedc3
commit 8e016e3b36
6 changed files with 89 additions and 0 deletions

View file

@ -39,6 +39,11 @@ class Navigation implements EntityInterface
*/
protected $domain;
/**
* @ORM\Column(type="boolean", options={"default"=0})
*/
protected $forceDomain = false;
/**
* @ORM\Column(type="text", nullable=true)
*/
@ -100,6 +105,18 @@ class Navigation implements EntityInterface
return $this;
}
public function getForceDomain(): ?bool
{
return $this->forceDomain;
}
public function setForceDomain(bool $forceDomain): self
{
$this->forceDomain = $forceDomain;
return $this;
}
public function getAdditionalDomains(): array
{
return (array) json_decode($this->additionalDomains, true);

View file

@ -0,0 +1,49 @@
<?php
namespace App\Core\EventSuscriber\Site;
use App\Core\Site\SiteRequest;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use function Symfony\Component\String\u;
class ForcedDomainEventSubscriber implements EventSubscriberInterface
{
protected SiteRequest $siteRequest;
public function __construct(SiteRequest $siteRequest)
{
$this->siteRequest = $siteRequest;
}
public function onKernelResponse(ResponseEvent $event)
{
$navigation = $this->siteRequest->getNavigation();
if (!$navigation) {
return;
}
if ($navigation->getDomain() === $this->siteRequest->getDomain()) {
return;
}
$uri = u($this->siteRequest->getUri())
->replace(
'://'.$this->siteRequest->getDomain(),
'://'.$navigation->getDomain()
);
$event->getResponse()->headers->set('Location', $uri);
$event->getResponse()->setStatusCode(Response::HTTP_MOVED_PERMANENTLY);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [['onKernelResponse', 20]],
];
}
}

View file

@ -11,6 +11,7 @@ use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use App\Core\Form\Site\NavigationAdditionalDomainType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class NavigationType extends AbstractType
{
@ -58,6 +59,19 @@ class NavigationType extends AbstractType
]
);
$builder->add(
'forceDomain',
CheckboxType::class,
[
'label' => 'Force this domain',
'required' => false,
'attr' => [
],
'constraints' => [
],
]
);
$builder->add(
'additionalDomains',
CollectionType::class,

View file

@ -148,5 +148,6 @@
"Setting": "Paramètre"
"Section": "Section"
"Filter": "Filtrer"
"Force this domain": "Forcer ce nom de domaine"
"Additional domains": "Domaines additionnels"
"Regular expression: do not add the delimiter": "Expréssion régulière : ne pas ajouter de délimiteur"

View file

@ -10,6 +10,9 @@
<div class="col-12">
{{ form_row(form.domain) }}
</div>
<div class="col-12">
{{ form_row(form.forceDomain) }}
</div>
{% set collection_name = 'additional-domains' %}
{% set label_add = 'Add' %}

View file

@ -71,4 +71,9 @@ class SiteRequest
{
return $this->requestStack->getCurrentRequest()->headers->get('host');
}
public function getUri(): string
{
return $this->requestStack->getCurrentRequest()->getUri();
}
}