Skip to content

Commit

Permalink
Do not persist dynamic permission resources in layout definitions as …
Browse files Browse the repository at this point in the history
…then the permission resources are not refreshed when the layout opens
  • Loading branch information
markus-moser committed Feb 8, 2024
1 parent 3091979 commit 1320f1f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/EventSubscriber/CustomLayoutSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

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 1320f1f

Please sign in to comment.