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

Fixed EZP-31639: media and file fields not loaded correctly in edit mode #1391

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ services:
tags:
- {name: kernel.event_subscriber}

EzSystems\EzPlatformAdminUi\EventListener\ContentDownloadRouteReferenceListener:
arguments:
$siteAccessGroups: '%ezpublish.siteaccess.groups%'
tags:
- {name: kernel.event_subscriber}

EzSystems\EzPlatformAdminUi\UI\Dataset\DatasetFactory:
lazy: true
arguments:
Expand Down
60 changes: 60 additions & 0 deletions src/lib/EventListener/ContentDownloadRouteReferenceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\EventListener;

use eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use EzSystems\EzPlatformAdminUi\Specification\SiteAccess\IsAdmin;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Ensures that download urls generated in ezplatform-admin-ui are in the scope of admin siteaccess.
adamwojs marked this conversation as resolved.
Show resolved Hide resolved
*
* @internal for internal use by AdminUI
*/
final class ContentDownloadRouteReferenceListener implements EventSubscriberInterface
{
public const CONTENT_DOWNLOAD_ROUTE_NAME = 'ez_content_download';

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

public function __construct(array $siteAccessGroups)
{
$this->siteAccessGroups = $siteAccessGroups;
}

public static function getSubscribedEvents(): array
{
return [
MVCEvents::ROUTE_REFERENCE_GENERATION => 'onRouteReferenceGeneration',
];
}

public function onRouteReferenceGeneration(RouteReferenceGenerationEvent $event): void
{
$routeReference = $event->getRouteReference();

if ($routeReference->getRoute() != self::CONTENT_DOWNLOAD_ROUTE_NAME) {
return;
}

/** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteaccess */
$siteaccess = $event->getRequest()->attributes->get('siteaccess');
if ($this->isAdminSiteAccess($siteaccess)) {
$routeReference->set('siteaccess', $siteaccess->name);
}
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}

private function isAdminSiteAccess(SiteAccess $siteAccess): bool
{
return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($siteAccess);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Tests\EventListener;

use eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent;
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use EzSystems\EzPlatformAdminUi\EventListener\ContentDownloadRouteReferenceListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class ContentDownloadRouteReferenceListenerTest extends TestCase
{
private const EXAMPLE_SITEACCESS_GROUPS = [
'admin_group' => [
'admin',
],
'site_group' => [
'non-admin',
],
];

public function testOnRouteReferenceGenerationSkipNonSupportedRoutes(): void
{
$expectedRouteReference = new RouteReference(
'non_supported_route',
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
]
);

$event = new RouteReferenceGenerationEvent(
clone $expectedRouteReference,
$this->createMock(Request::class)
);

$listener = new ContentDownloadRouteReferenceListener(self::EXAMPLE_SITEACCESS_GROUPS);
$listener->onRouteReferenceGeneration($event);

$this->assertEquals($expectedRouteReference, $event->getRouteReference());
}

public function testOnRouteReferenceGenerationSkipNonAdminSiteAccesses(): void
{
$expectedRouteReference = new RouteReference(
ContentDownloadRouteReferenceListener::CONTENT_DOWNLOAD_ROUTE_NAME,
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
]
);

$request = new Request();
$request->attributes->set('siteaccess', new SiteAccess('non-admin'));

$event = new RouteReferenceGenerationEvent(clone $expectedRouteReference, $request);

$listener = new ContentDownloadRouteReferenceListener(self::EXAMPLE_SITEACCESS_GROUPS);
$listener->onRouteReferenceGeneration($event);

$this->assertEquals($expectedRouteReference, $event->getRouteReference());
}

public function testOnRouteReferenceGenerationForcesAdminSiteAccess(): void
{
$request = new Request();
$request->attributes->set('siteaccess', new SiteAccess('admin'));

$event = new RouteReferenceGenerationEvent(
new RouteReference(
ContentDownloadRouteReferenceListener::CONTENT_DOWNLOAD_ROUTE_NAME,
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
]
),
$request
);

$listener = new ContentDownloadRouteReferenceListener(self::EXAMPLE_SITEACCESS_GROUPS);
$listener->onRouteReferenceGeneration($event);

$this->assertEquals(
new RouteReference(
ContentDownloadRouteReferenceListener::CONTENT_DOWNLOAD_ROUTE_NAME,
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
'siteaccess' => 'admin',
]
),
$event->getRouteReference()
);
}
}