Skip to content

Commit

Permalink
Do not persist dynamic permission resources in layout definitions (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-moser authored Feb 13, 2024
1 parent efae711 commit 8df3f91
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/EventSubscriber/CustomLayoutSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace FrontendPermissionToolkitBundle\EventSubscriber;

use FrontendPermissionToolkitBundle\CoreExtensions\ClassDefinitions\DynamicPermissionResource;
use Pimcore\Event\DataObjectCustomLayoutEvents;
use Pimcore\Event\Model\DataObject\CustomLayoutEvent;
use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal
*/
final class CustomLayoutSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
DataObjectCustomLayoutEvents::PRE_ADD => 'onUpdate',
DataObjectCustomLayoutEvents::PRE_UPDATE => 'onUpdate',
];
}

public function onUpdate(CustomLayoutEvent $event): void
{
$customLayout = $event->getCustomLayout();
$this->resetPermissionResources($customLayout->getLayoutDefinitions());
}

private function resetPermissionResources(ClassDefinition\Data | ClassDefinition\Layout | null $layout): void
{
if ($layout === null) {
return;
}

if ($layout instanceof DynamicPermissionResource) {
$layout->setPermissionResources([]);
}
if (method_exists($layout, 'getChildren')) {
foreach ($layout->getChildren() ?? [] as $child) {
$this->resetPermissionResources($child);
}
}
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ services:

bundle.frontendpermissiontoolkit.service:
alias: FrontendPermissionToolkitBundle\Service

FrontendPermissionToolkitBundle\EventSubscriber\CustomLayoutSubscriber:
tags:
- { name: kernel.event_subscriber }

0 comments on commit 8df3f91

Please sign in to comment.