Skip to content

Commit

Permalink
EZP-31086: Redirect based on router, not url alias
Browse files Browse the repository at this point in the history
  • Loading branch information
damianz5 committed Oct 29, 2019
1 parent 934d534 commit 6dbce36
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/bundle/Resources/config/services/form_processors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ services:

EzSystems\EzPlatformAdminUi\RepositoryForms\Form\Processor\Content\UrlRedirectProcessor:
public: true
decorates: EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor
arguments:
$siteaccessGroups: '%ezpublish.siteaccess.groups%'
$systemUrlRedirectProcessor: '@EzSystems\EzPlatformAdminUi\RepositoryForms\Form\Processor\Content\UrlRedirectProcessor.inner'

Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,33 @@
use EzSystems\EzPlatformAdminUi\Specification\SiteAccess\IsAdmin;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;

class UrlRedirectProcessor implements EventSubscriberInterface
{
/** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
private $siteaccess;

/** @var \EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor */
private $systemUrlRedirectProcessor;
/** @var \Symfony\Component\Routing\RouterInterface */
private $router;

/** @var array */
private $siteaccessGroups;

/**
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteaccess
* @param \EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor $systemUrlRedirectProcessor
* @param \Symfony\Component\Routing\RouterInterface $router
* @param array $siteaccessGroups
*/
public function __construct(
SiteAccess $siteaccess,
SystemUrlRedirectProcessor $systemUrlRedirectProcessor,
RouterInterface $router,
array $siteaccessGroups
) {
$this->siteaccess = $siteaccess;
$this->systemUrlRedirectProcessor = $systemUrlRedirectProcessor;
$this->router = $router;
$this->siteaccessGroups = $siteaccessGroups;
}

Expand Down Expand Up @@ -69,7 +70,7 @@ public function processRedirectAfterPublish(FormActionEvent $event): void
return;
}

$this->systemUrlRedirectProcessor->processRedirectAfterPublish($event);
$this->resolveSystemUrlRedirect($event);
}

/**
Expand All @@ -84,8 +85,8 @@ public function processRedirectAfterCancel(FormActionEvent $event): void
if ($this->isAdminSiteaccess()) {
return;
}

$this->systemUrlRedirectProcessor->processRedirectAfterCancel($event);
$this->resolveSystemUrlRedirect($event);
}

/**
Expand All @@ -97,4 +98,33 @@ protected function isAdminSiteaccess(): bool
{
return (new IsAdmin($this->siteaccessGroups))->isSatisfiedBy($this->siteaccess);
}

/**
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function resolveSystemUrlRedirect(FormActionEvent $event): void
{
/** @var \Symfony\Component\HttpFoundation\RedirectResponse $response */
$response = $event->getResponse();

if (!$response instanceof RedirectResponse) {
return;
}

$params = $this->router->match($response->getTargetUrl());

if (!in_array('locationId', $params)) {
return;
}

$url = $this->router->generate(
'ez_urlalias', ['locationId' => $params['locationId']]
);

$event->setResponse(new RedirectResponse($url));
}
}

0 comments on commit 6dbce36

Please sign in to comment.