From 336313a1c3034af58166d3a85edbc1de7ac9f5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Tue, 26 May 2020 15:58:33 +0200 Subject: [PATCH 1/2] Fixed EZP-31639: mdia and file fields not loaded correctly in edit mode --- src/bundle/Resources/config/services.yml | 6 + .../ContentDownloadRouteReferenceListener.php | 58 ++++++++++ ...tentDownloadRouteReferenceListenerTest.php | 106 ++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 src/lib/EventListener/ContentDownloadRouteReferenceListener.php create mode 100644 src/lib/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 9c47932934..14d962751b 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -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: diff --git a/src/lib/EventListener/ContentDownloadRouteReferenceListener.php b/src/lib/EventListener/ContentDownloadRouteReferenceListener.php new file mode 100644 index 0000000000..21b74d1d14 --- /dev/null +++ b/src/lib/EventListener/ContentDownloadRouteReferenceListener.php @@ -0,0 +1,58 @@ +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); + } + } + + private function isAdminSiteAccess(SiteAccess $siteAccess): bool + { + return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($siteAccess); + } +} diff --git a/src/lib/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php b/src/lib/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php new file mode 100644 index 0000000000..0183b4117a --- /dev/null +++ b/src/lib/Tests/EventListener/ContentDownloadRouteReferenceListenerTest.php @@ -0,0 +1,106 @@ + [ + '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() + ); + } +} From 5a56e93f222f27918448421995576b2967f189ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 27 May 2020 13:12:03 +0200 Subject: [PATCH 2/2] Update src/lib/EventListener/ContentDownloadRouteReferenceListener.php Co-authored-by: Andrew Longosz --- src/lib/EventListener/ContentDownloadRouteReferenceListener.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/EventListener/ContentDownloadRouteReferenceListener.php b/src/lib/EventListener/ContentDownloadRouteReferenceListener.php index 21b74d1d14..0077ce0185 100644 --- a/src/lib/EventListener/ContentDownloadRouteReferenceListener.php +++ b/src/lib/EventListener/ContentDownloadRouteReferenceListener.php @@ -16,6 +16,8 @@ /** * Ensures that download urls generated in ezplatform-admin-ui are in the scope of admin siteaccess. + * + * @internal for internal use by AdminUI */ final class ContentDownloadRouteReferenceListener implements EventSubscriberInterface {