vendor/sulu/sulu/src/Sulu/Bundle/SecurityBundle/EventListener/SuluSecurityListener.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\SecurityBundle\EventListener;
  11. use Sulu\Component\Security\Authorization\AccessControl\SecuredObjectControllerInterface;
  12. use Sulu\Component\Security\Authorization\PermissionTypes;
  13. use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
  14. use Sulu\Component\Security\Authorization\SecurityCondition;
  15. use Sulu\Component\Security\SecuredControllerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  20. /**
  21.  * Listens on the kernel.controller event and checks if Sulu allows this action.
  22.  */
  23. class SuluSecurityListener implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var SecurityCheckerInterface
  27.      */
  28.     private $securityChecker;
  29.     public function __construct(SecurityCheckerInterface $securityChecker)
  30.     {
  31.         $this->securityChecker $securityChecker;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [KernelEvents::CONTROLLER => 'onKernelController'];
  36.     }
  37.     /**
  38.      * Checks if the action is allowed for the current user, and throws an Exception otherwise.
  39.      *
  40.      * @throws AccessDeniedException
  41.      */
  42.     public function onKernelController(ControllerEvent $event)
  43.     {
  44.         $controller $event->getController();
  45.         $action '__invoke';
  46.         if (\is_array($controller)) {
  47.             if (isset($controller[1])) {
  48.                 $action $controller[1];
  49.             }
  50.             if (isset($controller[0])) {
  51.                 $controller $controller[0];
  52.             }
  53.         }
  54.         if (
  55.             !$controller instanceof SecuredControllerInterface &&
  56.             !$controller instanceof SecuredObjectControllerInterface
  57.         ) {
  58.             return;
  59.         }
  60.         $request $event->getRequest();
  61.         // find appropriate permission type for request
  62.         $permission '';
  63.         switch ($request->getMethod()) {
  64.             case 'GET':
  65.                 $permission PermissionTypes::VIEW;
  66.                 break;
  67.             case 'POST':
  68.                 if (\in_array($action, ['postAction''__invoke'])) { // means that the ClassResourceInterface has to be used
  69.                     $permission PermissionTypes::ADD;
  70.                 } else {
  71.                     $permission PermissionTypes::EDIT;
  72.                 }
  73.                 break;
  74.             case 'PUT':
  75.             case 'PATCH':
  76.                 $permission PermissionTypes::EDIT;
  77.                 break;
  78.             case 'DELETE':
  79.                 $permission PermissionTypes::DELETE;
  80.                 break;
  81.         }
  82.         $securityContext null;
  83.         $locale $controller->getLocale($request);
  84.         $objectType null;
  85.         $objectId null;
  86.         if ($controller instanceof SecuredObjectControllerInterface) {
  87.             $objectType $controller->getSecuredClass();
  88.             $objectId $controller->getSecuredObjectId($request);
  89.         }
  90.         // check permission
  91.         if ($controller instanceof SecuredControllerInterface) {
  92.             $securityContext $controller->getSecurityContext();
  93.         }
  94.         if (null !== $securityContext) {
  95.             $this->securityChecker->checkPermission(
  96.                 new SecurityCondition($securityContext$locale$objectType$objectId),
  97.                 $permission
  98.             );
  99.         }
  100.     }
  101. }