src/EventSubscriber/Menu/ImportMenuSubscriber.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Menu;
  3. use App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface;
  4. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. /**
  8.  * Class ImportMenuSubscriber.
  9.  */
  10. class ImportMenuSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * The configuration helper.
  14.      *
  15.      * @var \App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface
  16.      */
  17.     private $importConfigHelper;
  18.     /**
  19.      * The authorization service helper.
  20.      *
  21.      * @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
  22.      */
  23.     private $authorizationChecker;
  24.     /**
  25.      * ImportMenuSubscriber constructor.
  26.      *
  27.      * @param \App\Application\EntityImportBundle\Contract\ConfigurationHelperInterface $importConfigHelper
  28.      *   The configuration helper\
  29.      * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $authorizationChecker
  30.      *   The authorization service helper
  31.      */
  32.     public function __construct(
  33.         ConfigurationHelperInterface $importConfigHelper,
  34.         AuthorizationCheckerInterface $authorizationChecker
  35.     ) {
  36.         $this->importConfigHelper $importConfigHelper;
  37.         $this->authorizationChecker $authorizationChecker;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      *
  42.      * @uses \App\EventSubscriber\Menu\ImportMenuSubscriber::onEventDispatched()
  43.      */
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             ConfigureMenuEvent::SIDEBAR => 'onEventDispatched',
  48.         ];
  49.     }
  50.     /**
  51.      * Public callback responsible for creating additional menu group.
  52.      *
  53.      * @param \Sonata\AdminBundle\Event\ConfigureMenuEvent $event
  54.      *   The instance of the dispatched event
  55.      */
  56.     public function onEventDispatched(ConfigureMenuEvent $event): void
  57.     {
  58.         $menu $event->getMenu();
  59.         $menuItems $this->importConfigHelper->getMenuItems();
  60.         if (!$this->isGrantedAtLeastOnePermission($menuItems)) {
  61.             return;
  62.         }
  63.         $group $menu
  64.             ->addChild('Import')
  65.             ->setExtra('icon''<i class="fa fa-cloud-upload"></i>');
  66.         foreach ($menuItems as $item) {
  67.             if (!$this->authorizationChecker->isGranted($item['menuPermission'])) {
  68.                 continue;
  69.             }
  70.             $group
  71.                 ->addChild($item['menuTitle'], [
  72.                     'route' => 'app.import.init',
  73.                     'routeParameters' => [
  74.                         'type' => $item['menuRouteKey'],
  75.                     ],
  76.                 ])
  77.                 ->setExtra('icon''<i class="fas fa-angle-double-right" aria-hidden="true"></i>');
  78.         }
  79.     }
  80.     /**
  81.      * Responsible for validating whether or not the user has access to the entire group.
  82.      *
  83.      * The validation is triggered before we append the "Import" group to the sidebar
  84.      * and we only need to make sure that the user has access to at least one of the
  85.      * menu items in order to show the menu group. Further validations will be
  86.      * performed once we start to append the menu items.
  87.      *
  88.      * @param array $menuItems
  89.      *   The list with all menu items
  90.      *
  91.      * @return bool
  92.      *   The security check result
  93.      */
  94.     protected function isGrantedAtLeastOnePermission(array $menuItems): bool
  95.     {
  96.         foreach ($menuItems as $item) {
  97.             if ($this->authorizationChecker->isGranted($item['menuPermission'])) {
  98.                 return true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103. }