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 91bcf0c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/bundle/Resources/config/services/form_processors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ services:
arguments:
$siteAccessGroups: '%ezpublish.siteaccess.groups%'

EzSystems\EzPlatformAdminUi\RepositoryForms\Form\Processor\Content\UrlRedirectProcessor:
EzSystems\EzPlatformAdminUi\RepositoryForms\Form\Processor\Content\SystemUrlRedirectProcessor:
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
class SystemUrlRedirectProcessor 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 @@ -65,11 +66,11 @@ public function processRedirectAfterPublish(FormActionEvent $event): void
return;
}

if ($this->isAdminSiteaccess()) {
if (!$this->isAdminSiteaccess()) {
return;
}

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

/**
Expand All @@ -81,11 +82,11 @@ public function processRedirectAfterPublish(FormActionEvent $event): void
*/
public function processRedirectAfterCancel(FormActionEvent $event): void
{
if ($this->isAdminSiteaccess()) {
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(
'_ezpublishLocation', ['locationId' => $params['locationId']]
);

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

0 comments on commit 91bcf0c

Please sign in to comment.