vendor/kunstmaan/node-bundle/EventListener/SlugSecurityListener.php line 73

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\NodeBundle\EventListener;
  3. use Doctrine\ORM\EntityManager;
  4. use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
  5. use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
  6. use Kunstmaan\NodeBundle\Helper\NodeMenu;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  10. class SlugSecurityListener
  11. {
  12.     /**
  13.      * @var AuthorizationCheckerInterface
  14.      */
  15.     protected $authorizationChecker;
  16.     /**
  17.      * @var EntityManager
  18.      */
  19.     protected $em;
  20.     /**
  21.      * @var NodeMenu
  22.      */
  23.     protected $nodeMenu;
  24.     /**
  25.      * @var bool
  26.      */
  27.     private $permissionsEnabled;
  28.     /**
  29.      * @param EntityManager                 $entityManager
  30.      * @param AuthorizationCheckerInterface $authorizationChecker
  31.      * @param NodeMenu                      $nodeMenu
  32.      */
  33.     public function __construct(
  34.         EntityManager $entityManager,
  35.         AuthorizationCheckerInterface $authorizationChecker,
  36.         NodeMenu $nodeMenu,
  37.         $permissionsEnabled true
  38.     ) {
  39.         $this->em $entityManager;
  40.         $this->authorizationChecker $authorizationChecker;
  41.         $this->nodeMenu $nodeMenu;
  42.         $this->permissionsEnabled $permissionsEnabled;
  43.     }
  44.     /**
  45.      * Perform basic security checks
  46.      *
  47.      * @param SlugSecurityEvent $event
  48.      *
  49.      * @throws AccessDeniedException
  50.      * @throws NotFoundHttpException
  51.      */
  52.     public function onSlugSecurityEvent(SlugSecurityEvent $event)
  53.     {
  54.         $node $event->getNode();
  55.         $nodeTranslation $event->getNodeTranslation();
  56.         $request $event->getRequest();
  57.         if ($this->permissionsEnabled && false === $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_VIEW$node)) {
  58.             throw new AccessDeniedException('You do not have sufficient rights to access this page.');
  59.         }
  60.         $isPreview $request->attributes->get('preview');
  61.         if (!$isPreview && !$nodeTranslation->isOnline()) {
  62.             throw new NotFoundHttpException('The requested page is not online');
  63.         }
  64.         $nodeMenu $this->nodeMenu;
  65.         $nodeMenu->setLocale($nodeTranslation->getLang());
  66.         $nodeMenu->setCurrentNode($node);
  67.         $nodeMenu->setIncludeOffline($isPreview);
  68.         $request->attributes->set('_nodeMenu'$nodeMenu);
  69.     }
  70. }