vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/EventListener/SecurityListener.php line 46

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\WebsiteBundle\EventListener;
  11. use Sulu\Bundle\PageBundle\Document\BasePageDocument;
  12. use Sulu\Component\Content\Compat\Structure\PageBridge;
  13. use Sulu\Component\Security\Authorization\PermissionTypes;
  14. use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
  15. use Sulu\Component\Security\Authorization\SecurityCondition;
  16. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. class SecurityListener implements EventSubscriberInterface
  21. {
  22. /**
  23. * @var SecurityCheckerInterface|null
  24. */
  25. private $securityChecker;
  26. public function __construct(
  27. ?SecurityCheckerInterface $securityChecker = null
  28. ) {
  29. $this->securityChecker = $securityChecker;
  30. }
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [
  34. KernelEvents::REQUEST => [
  35. ['onKernelRequest', 7], // set the security listener after the firewall and after the routing listener
  36. ],
  37. ];
  38. }
  39. public function onKernelRequest(RequestEvent $event): void
  40. {
  41. $request = $event->getRequest();
  42. if (null === $this->securityChecker) {
  43. return;
  44. }
  45. $requestAttributes = $request->attributes->get('_sulu');
  46. if (!$requestAttributes instanceof RequestAttributes) {
  47. return;
  48. }
  49. $webspace = $requestAttributes->getAttribute('webspace');
  50. $structure = $request->attributes->get('structure');
  51. if (!$structure instanceof PageBridge) {
  52. return;
  53. }
  54. $document = $structure->getDocument();
  55. if (!$document instanceof BasePageDocument) {
  56. return;
  57. }
  58. if ($webspace->hasWebsiteSecurity()) {
  59. $this->securityChecker->checkPermission(
  60. new SecurityCondition(
  61. 'sulu.webspaces.' . $document->getWebspaceName(),
  62. $document->getLocale(),
  63. \get_class($document),
  64. $document->getUuid()
  65. ),
  66. PermissionTypes::VIEW
  67. );
  68. }
  69. }
  70. }