src/Platform/ControlPanelBundle/Listeners/RequestListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace Platform\ControlPanelBundle\Listeners;
  3. use Cms\CoreBundle\Service\ContextManager;
  4. use Platform\ControlPanelBundle\Controller\ContractorController;
  5. use Platform\ControlPanelBundle\Controller\DashboardController;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. final class RequestListener implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var ContextManager
  14.      */
  15.     private $cm;
  16.     /**
  17.      * @param ContextManager $cm
  18.      */
  19.     public function __construct(ContextManager $cm)
  20.     {
  21.         $this->cm $cm;
  22.     }
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::CONTROLLER => ['controlPanelCheck'],
  30.         ];
  31.     }
  32.     /**
  33.      * @param ControllerEvent $event
  34.      */
  35.     public function controlPanelCheck(ControllerEvent $event)
  36.     {
  37.         // get the controller
  38.         $controller $event->getController();
  39.         // should be an array of object then method call
  40.         if (is_array($controller) && $controller[0] instanceof DashboardController) {
  41.             // if we are not internal, throw 404
  42.             // IMPORTANT: we must check the authenticated account, otherwise there may be issues with impersonation
  43.             $account $this->cm->getGlobalContext()->getAuthenticatedAccount();
  44.             if (empty($account) || ! $account->isInternal()) {
  45.                 $event->setController(function () {
  46.                     throw new NotFoundHttpException();
  47.                 });
  48.             }
  49.         }
  50.         // should be an array of object then method call
  51.         if (is_array($controller) && $controller[0] instanceof ContractorController) {
  52.             // if we are not internal, throw 404
  53.             // IMPORTANT: we must check the authenticated account, otherwise there may be issues with impersonation
  54.             $account $this->cm->getGlobalContext()->getAuthenticatedAccount();
  55.             if (empty($account) || ! $account->isContractor()) {
  56.                 $event->setController(function () {
  57.                     throw new NotFoundHttpException();
  58.                 });
  59.             }
  60.         }
  61.     }
  62. }