<?php
namespace App\EventSubscriber\Menu;
use App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Class ImportMenuSubscriber.
*/
class ImportMenuSubscriber implements EventSubscriberInterface
{
/**
* The configuration helper.
*
* @var \App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface
*/
private $importConfigHelper;
/**
* The authorization service helper.
*
* @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* ImportMenuSubscriber constructor.
*
* @param \App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface $importConfigHelper
* The configuration helper\
* @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $authorizationChecker
* The authorization service helper
*/
public function __construct(
ConfigurationHelperInterface $importConfigHelper,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->importConfigHelper = $importConfigHelper;
$this->authorizationChecker = $authorizationChecker;
}
/**
* {@inheritdoc}
*
* @uses \App\EventSubscriber\Menu\ImportMenuSubscriber::onEventDispatched()
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::SIDEBAR => 'onEventDispatched',
];
}
/**
* Public callback responsible for creating additional menu group.
*
* @param \Sonata\AdminBundle\Event\ConfigureMenuEvent $event
* The instance of the dispatched event
*/
public function onEventDispatched(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$menuItems = $this->importConfigHelper->getMenuItems();
if (!$this->isGrantedAtLeastOnePermission($menuItems)) {
return;
}
$group = $menu
->addChild('Import')
->setExtra('icon', '<i class="fa fa-cloud-upload"></i>');
foreach ($menuItems as $item) {
if (!$this->authorizationChecker->isGranted($item['menuPermission'])) {
continue;
}
$group
->addChild($item['menuTitle'], [
'route' => 'app.import.init',
'routeParameters' => [
'type' => $item['menuRouteKey'],
],
])
->setExtra('icon', '<i class="fas fa-angle-double-right" aria-hidden="true"></i>');
}
}
/**
* Responsible for validating whether or not the user has access to the entire group.
*
* The validation is triggered before we append the "Import" group to the sidebar
* and we only need to make sure that the user has access to at least one of the
* menu items in order to show the menu group. Further validations will be
* performed once we start to append the menu items.
*
* @param array $menuItems
* The list with all menu items
*
* @return bool
* The security check result
*/
protected function isGrantedAtLeastOnePermission(array $menuItems): bool
{
foreach ($menuItems as $item) {
if ($this->authorizationChecker->isGranted($item['menuPermission'])) {
return true;
}
}
return false;
}
}