Repository navigation
[2026.3][Class] Require objects permission for the custom layout editor collection route #2079
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
Merged
+212
−2
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
tests/Unit/Class/Service/CustomLayoutServiceEditorCollectionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This source file is available under the terms of the | ||
| * Pimcore Open Core License (POCL) | ||
| * Full copyright and license information is available in | ||
| * LICENSE.md which is distributed with this source code. | ||
| * | ||
| * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) | ||
| * @license Pimcore Open Core License (POCL) | ||
| */ | ||
|
|
||
| namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Class\Service; | ||
|
|
||
| use Codeception\Test\Unit; | ||
| use Exception; | ||
| use Pimcore\Bundle\StudioBackendBundle\Class\Hydrator\CustomLayout\CustomLayoutHydratorInterface; | ||
| use Pimcore\Bundle\StudioBackendBundle\Class\Repository\CustomLayoutRepositoryInterface; | ||
| use Pimcore\Bundle\StudioBackendBundle\Class\Schema\CustomLayout\CustomLayoutCompact; | ||
| use Pimcore\Bundle\StudioBackendBundle\Class\Service\CustomLayoutService; | ||
| use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataObjectServiceInterface; | ||
| use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException; | ||
| use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface; | ||
| use Pimcore\Bundle\StudioBackendBundle\Security\Service\LayoutServiceInterface; | ||
| use Pimcore\Model\DataObject; | ||
| use Pimcore\Model\DataObject\ClassDefinition\CustomLayout as CoreLayout; | ||
| use Pimcore\Model\UserInterface; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
|
||
| /** | ||
| * The editor collection route only requires the 'objects' permission | ||
| * (pimcore/platform-version#597), so the service has to enforce access on its own. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class CustomLayoutServiceEditorCollectionTest extends Unit | ||
| { | ||
| private const OBJECT_ID = 42; | ||
|
|
||
| private const CLASS_ID = 'CAR'; | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function testUserWithoutViewPermissionOnObjectIsRejected(): void | ||
| { | ||
| $user = $this->createUser(false); | ||
|
|
||
| $dataObjectService = $this->createMock(DataObjectServiceInterface::class); | ||
| $dataObjectService->method('getDataObjectElement') | ||
| ->with($user, self::OBJECT_ID) | ||
| ->willThrowException(new ForbiddenException('You dont have view permission')); | ||
|
|
||
| $repository = $this->createMock(CustomLayoutRepositoryInterface::class); | ||
| $repository->expects($this->never())->method('getCustomLayoutsByClass'); | ||
|
|
||
| $service = $this->createService($dataObjectService, $repository); | ||
|
|
||
| $this->expectException(ForbiddenException::class); | ||
|
|
||
| $service->getCustomLayoutEditorCollection(self::OBJECT_ID, $user); | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function testNonAdminOnlyGetsAllowedLayouts(): void | ||
| { | ||
| $user = $this->createUser(false); | ||
| $service = $this->createServiceForObject( | ||
| $user, | ||
| ['layout_a', 'layout_b', 'layout_c'], | ||
| allowedLayouts: ['layout_b'] | ||
| ); | ||
|
|
||
| $result = $service->getCustomLayoutEditorCollection(self::OBJECT_ID, $user); | ||
|
|
||
| // The main layout ('0') is not part of the allowed layouts, so it must not be added either. | ||
| $this->assertSame(['layout_b'], $this->getIds($result)); | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function testNonAdminWithoutRestrictionGetsMainAndClassLayouts(): void | ||
| { | ||
| $user = $this->createUser(false); | ||
| $service = $this->createServiceForObject($user, ['layout_a', 'layout_b']); | ||
|
|
||
| $result = $service->getCustomLayoutEditorCollection(self::OBJECT_ID, $user); | ||
|
|
||
| // Sorted by name: layout_a, layout_b, main ('0'). | ||
| $this->assertSame(['layout_a', 'layout_b', '0'], $this->getIds($result)); | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function testAdminAdditionallyGetsAdminMainLayout(): void | ||
| { | ||
| $user = $this->createUser(true); | ||
| $service = $this->createServiceForObject($user, ['layout_a']); | ||
|
|
||
| $result = $service->getCustomLayoutEditorCollection(self::OBJECT_ID, $user); | ||
|
|
||
| // Sorted by name: layout_a, main ('0'), main_admin ('-1'). | ||
| $this->assertSame(['layout_a', '0', '-1'], $this->getIds($result)); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $layoutIds | ||
| * @param string[] $allowedLayouts | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| private function createServiceForObject( | ||
| UserInterface $user, | ||
| array $layoutIds, | ||
| array $allowedLayouts = [] | ||
| ): CustomLayoutService { | ||
| $dataObject = $this->createMock(DataObject::class); | ||
| $dataObject->method('getClassId')->willReturn(self::CLASS_ID); | ||
|
|
||
| $dataObjectService = $this->createMock(DataObjectServiceInterface::class); | ||
| $dataObjectService->method('getDataObjectElement') | ||
| ->with($user, self::OBJECT_ID) | ||
| ->willReturn($dataObject); | ||
|
|
||
| $repository = $this->createMock(CustomLayoutRepositoryInterface::class); | ||
| $repository->method('getCustomLayoutsByClass') | ||
| ->with([self::CLASS_ID]) | ||
| ->willReturn(array_map( | ||
| static fn (string $id): CoreLayout => (new CoreLayout())->setId($id)->setName($id), | ||
| $layoutIds | ||
| )); | ||
|
|
||
| $layoutService = $this->createMock(LayoutServiceInterface::class); | ||
| $layoutService->method('getUserAllowedLayoutsByClass')->willReturn($allowedLayouts); | ||
|
|
||
| return $this->createService($dataObjectService, $repository, $layoutService); | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| private function createService( | ||
| DataObjectServiceInterface $dataObjectService, | ||
| CustomLayoutRepositoryInterface $repository, | ||
| ?LayoutServiceInterface $layoutService = null | ||
| ): CustomLayoutService { | ||
| $hydrator = $this->createMock(CustomLayoutHydratorInterface::class); | ||
| $hydrator->method('hydrateCompactLayout')->willReturnCallback( | ||
| static fn (CoreLayout $layout): CustomLayoutCompact => new CustomLayoutCompact( | ||
| $layout->getId(), | ||
| $layout->getName(), | ||
| $layout->getDefault() | ||
| ) | ||
| ); | ||
|
|
||
| return new CustomLayoutService( | ||
| $repository, | ||
| $hydrator, | ||
| $dataObjectService, | ||
| $this->createMock(DownloadServiceInterface::class), | ||
| $this->createMock(EventDispatcherInterface::class), | ||
| $layoutService ?? $this->createMock(LayoutServiceInterface::class) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| private function createUser(bool $isAdmin): UserInterface | ||
| { | ||
| $user = $this->createMock(UserInterface::class); | ||
| $user->method('isAdmin')->willReturn($isAdmin); | ||
|
|
||
| return $user; | ||
| } | ||
|
|
||
| /** | ||
| * @param CustomLayoutCompact[] $layouts | ||
| * | ||
| * @return string[] | ||
| */ | ||
| private function getIds(array $layouts): array | ||
| { | ||
| return array_map(static fn (CustomLayoutCompact $layout): string => $layout->getId(), $layouts); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.