From e6e2ea9608c7c62c53eb0f7791e3580ddede9ead Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Thu, 10 Dec 2020 15:45:45 +0100 Subject: [PATCH 1/4] EZP-32173: Fixed UDW 'allowed_content_types' configuration --- .../RichTextEmbedAllowedContentTypes.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php index 9d0083cc08..049bf66b7f 100644 --- a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php +++ b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php @@ -42,16 +42,16 @@ public function __construct( /** * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException */ - private function getAllowedContentTypesIdentifiers(): array + private function getAllowedContentTypesIdentifiers(array $contentTypesAllowedViaConfig): ?array { $access = $this->permissionResolver->hasAccess('content', 'read'); if (!\is_array($access)) { - return []; + return count($contentTypesAllowedViaConfig) ? $contentTypesAllowedViaConfig : null; } $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($access, ContentTypeLimitation::class); if (empty($restrictedContentTypesIds)) { - return []; + return count($contentTypesAllowedViaConfig) ? $contentTypesAllowedViaConfig : null; } $allowedContentTypesIdentifiers = []; @@ -61,7 +61,12 @@ private function getAllowedContentTypesIdentifiers(): array $allowedContentTypesIdentifiers[] = $contentType->identifier; } - return $allowedContentTypesIdentifiers; + $allowedContentTypesIdentifiers = count($contentTypesAllowedViaConfig) + ? array_intersect($contentTypesAllowedViaConfig, $allowedContentTypesIdentifiers) + : $allowedContentTypesIdentifiers; + + //hacky, but as of now null or empty array means UDW allows all Content Types, which shouldn't be the case + return empty($allowedContentTypesIdentifiers) ? [microtime()] : array_values($allowedContentTypesIdentifiers); } public static function getSubscribedEvents(): array @@ -80,10 +85,10 @@ public function onUdwConfigResolve(ConfigResolveEvent $event): void } if ($this->allowedContentTypesIdentifiers === null) { - $this->allowedContentTypesIdentifiers = $this->getAllowedContentTypesIdentifiers(); + $this->allowedContentTypesIdentifiers = $this->getAllowedContentTypesIdentifiers($config['allowed_content_types'] ?? []); } - $config['allowed_content_types'] = !empty($this->allowedContentTypesIdentifiers) ? $this->allowedContentTypesIdentifiers : null; + $config['allowed_content_types'] = $this->allowedContentTypesIdentifiers ?: null; $event->setConfig($config); } From 2e5e5bae271df639a9d53a1cf4bc87978d410251 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Fri, 11 Dec 2020 12:34:27 +0100 Subject: [PATCH 2/4] EZP-31110: Ternary operators, changed UDW config array output --- .../Event/Subscriber/RichTextEmbedAllowedContentTypes.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php index 049bf66b7f..c9412cff92 100644 --- a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php +++ b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php @@ -45,13 +45,13 @@ public function __construct( private function getAllowedContentTypesIdentifiers(array $contentTypesAllowedViaConfig): ?array { $access = $this->permissionResolver->hasAccess('content', 'read'); - if (!\is_array($access)) { - return count($contentTypesAllowedViaConfig) ? $contentTypesAllowedViaConfig : null; + if (!\is_array($access) && $access) { + return $contentTypesAllowedViaConfig ?: null; } $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($access, ContentTypeLimitation::class); if (empty($restrictedContentTypesIds)) { - return count($contentTypesAllowedViaConfig) ? $contentTypesAllowedViaConfig : null; + return $contentTypesAllowedViaConfig ?: null; } $allowedContentTypesIdentifiers = []; @@ -66,7 +66,7 @@ private function getAllowedContentTypesIdentifiers(array $contentTypesAllowedVia : $allowedContentTypesIdentifiers; //hacky, but as of now null or empty array means UDW allows all Content Types, which shouldn't be the case - return empty($allowedContentTypesIdentifiers) ? [microtime()] : array_values($allowedContentTypesIdentifiers); + return empty($allowedContentTypesIdentifiers) ? [null] : array_values($allowedContentTypesIdentifiers); } public static function getSubscribedEvents(): array From 879801659d14e80b34e4573fde0bb706c60fc87b Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Fri, 11 Dec 2020 12:37:21 +0100 Subject: [PATCH 3/4] EZP-31110: Removed unnecessary comment --- .../Event/Subscriber/RichTextEmbedAllowedContentTypes.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php index c9412cff92..4ebdb20bdd 100644 --- a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php +++ b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php @@ -65,7 +65,6 @@ private function getAllowedContentTypesIdentifiers(array $contentTypesAllowedVia ? array_intersect($contentTypesAllowedViaConfig, $allowedContentTypesIdentifiers) : $allowedContentTypesIdentifiers; - //hacky, but as of now null or empty array means UDW allows all Content Types, which shouldn't be the case return empty($allowedContentTypesIdentifiers) ? [null] : array_values($allowedContentTypesIdentifiers); } From 54ceb421f7e29698abd3c9d1db0a39c51d2415dd Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 14 Dec 2020 15:50:06 +0100 Subject: [PATCH 4/4] EZP-32173: Changed return data when $access is bool --- .../RichTextEmbedAllowedContentTypesTest.php | 20 +++++++++---------- .../RichTextEmbedAllowedContentTypes.php | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php b/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php index fbc83e1c37..981db10fb2 100644 --- a/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php +++ b/src/lib/Tests/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypesTest.php @@ -64,12 +64,9 @@ public function testUdwConfigResolveOnUnsupportedConfigName(): void $this->assertEquals([], $event->getConfig()); } - /** - * @dataProvider dataProviderForUdwConfigResolveWhenThereIsNoContentReadLimitations - */ - public function testUdwConfigResolveWhenThereIsNoContentReadLimitations(bool $hasAccess): void + public function testUdwConfigResolveWhenThereIsNoContentReadLimitations(): void { - $this->permissionResolver->method('hasAccess')->with('content', 'read')->willReturn($hasAccess); + $this->permissionResolver->method('hasAccess')->with('content', 'read')->willReturn(true); $this->permissionChecker->expects($this->never())->method('getRestrictions'); $this->contentTypeService->expects($this->never())->method('loadContentTypeList'); @@ -78,12 +75,15 @@ public function testUdwConfigResolveWhenThereIsNoContentReadLimitations(bool $ha ]); } - public function dataProviderForUdwConfigResolveWhenThereIsNoContentReadLimitations(): iterable + public function testUdwConfigResolveWhenThereIsNoContentReadLimitationsAndNoAccess(): void { - return [ - ['hasAccess' => false], - ['hasAccess' => true], - ]; + $this->permissionResolver->method('hasAccess')->with('content', 'read')->willReturn(false); + $this->permissionChecker->expects($this->never())->method('getRestrictions'); + $this->contentTypeService->expects($this->never())->method('loadContentTypeList'); + + $this->assertConfigurationResolvingResult([ + 'allowed_content_types' => [null], + ]); } public function testUdwConfigResolveWhenThereAreContentReadLimitations(): void diff --git a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php index 4ebdb20bdd..06454f2541 100644 --- a/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php +++ b/src/lib/UniversalDiscovery/Event/Subscriber/RichTextEmbedAllowedContentTypes.php @@ -45,8 +45,8 @@ public function __construct( private function getAllowedContentTypesIdentifiers(array $contentTypesAllowedViaConfig): ?array { $access = $this->permissionResolver->hasAccess('content', 'read'); - if (!\is_array($access) && $access) { - return $contentTypesAllowedViaConfig ?: null; + if (!\is_array($access)) { + return $access ? ($contentTypesAllowedViaConfig ?: null) : [null]; } $restrictedContentTypesIds = $this->permissionChecker->getRestrictions($access, ContentTypeLimitation::class); @@ -87,7 +87,7 @@ public function onUdwConfigResolve(ConfigResolveEvent $event): void $this->allowedContentTypesIdentifiers = $this->getAllowedContentTypesIdentifiers($config['allowed_content_types'] ?? []); } - $config['allowed_content_types'] = $this->allowedContentTypesIdentifiers ?: null; + $config['allowed_content_types'] = $this->allowedContentTypesIdentifiers; $event->setConfig($config); }