vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/ElementListener.php line 97

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener\Frontend;
  15. use Pimcore\Bundle\AdminBundle\Security\User\UserLoader;
  16. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  17. use Pimcore\Http\Request\Resolver\DocumentResolver;
  18. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  19. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  20. use Pimcore\Http\RequestHelper;
  21. use Pimcore\Model\DataObject\Service;
  22. use Pimcore\Model\Document;
  23. use Pimcore\Model\Staticroute;
  24. use Pimcore\Model\Version;
  25. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  26. use Psr\Log\LoggerAwareInterface;
  27. use Psr\Log\LoggerAwareTrait;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  31. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  32. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  33. use Symfony\Component\HttpKernel\KernelEvents;
  34. /**
  35.  * Handles element setup logic from request. Basically this does what the init() method
  36.  * on the ZF frontend controller did.
  37.  */
  38. class ElementListener implements EventSubscriberInterfaceLoggerAwareInterface
  39. {
  40.     use LoggerAwareTrait;
  41.     use PimcoreContextAwareTrait;
  42.     const FORCE_ALLOW_PROCESSING_UNPUBLISHED_ELEMENTS '_force_allow_processing_unpublished_elements';
  43.     /**
  44.      * @var DocumentResolver
  45.      */
  46.     protected $documentResolver;
  47.     /**
  48.      * @var EditmodeResolver
  49.      */
  50.     protected $editmodeResolver;
  51.     /**
  52.      * @var RequestHelper
  53.      */
  54.     protected $requestHelper;
  55.     /**
  56.      * @var UserLoader
  57.      */
  58.     protected $userLoader;
  59.     /**
  60.      * @var DocumentTargetingConfigurator
  61.      */
  62.     private $targetingConfigurator;
  63.     public function __construct(
  64.         DocumentResolver $documentResolver,
  65.         EditmodeResolver $editmodeResolver,
  66.         RequestHelper $requestHelper,
  67.         UserLoader $userLoader,
  68.         DocumentTargetingConfigurator $targetingConfigurator
  69.     ) {
  70.         $this->documentResolver $documentResolver;
  71.         $this->editmodeResolver $editmodeResolver;
  72.         $this->requestHelper $requestHelper;
  73.         $this->userLoader $userLoader;
  74.         $this->targetingConfigurator $targetingConfigurator;
  75.     }
  76.     /**
  77.      * @inheritDoc
  78.      */
  79.     public static function getSubscribedEvents()
  80.     {
  81.         return [
  82.             KernelEvents::REQUEST => ['onKernelRequest'3], // has to be after DocumentFallbackListener and after TargetingListener
  83.         ];
  84.     }
  85.     public function onKernelRequest(GetResponseEvent $event)
  86.     {
  87.         if ($event->isMasterRequest()) {
  88.             $request $event->getRequest();
  89.             if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  90.                 return;
  91.             }
  92.             $document $this->documentResolver->getDocument($request);
  93.             if (!$document && !Staticroute::getCurrentRoute()) {
  94.                 return;
  95.             }
  96.             $adminRequest =
  97.                 $this->requestHelper->isFrontendRequestByAdmin($request) ||
  98.                 $this->requestHelper->isFrontendRequestByAdmin($this->requestHelper->getMasterRequest());
  99.             $user null;
  100.             if ($adminRequest) {
  101.                 $user $this->userLoader->getUser();
  102.             }
  103.             if (!$document->isPublished() && !$user && !$request->attributes->get(self::FORCE_ALLOW_PROCESSING_UNPUBLISHED_ELEMENTS)) {
  104.                 $this->logger->warning('Denying access to document {document} as it is unpublished and there is no user in the session.', [
  105.                     $document->getFullPath()
  106.                 ]);
  107.                 throw new AccessDeniedHttpException(sprintf('Access denied for %s'$document->getFullPath()));
  108.             }
  109.             // editmode, pimcore_preview & pimcore_version
  110.             if ($user) {
  111.                 $document $this->handleAdminUserDocumentParams($request$document);
  112.                 $this->handleObjectParams($request);
  113.             }
  114.             // for public versions
  115.             $document $this->handleVersion($request$document);
  116.             // apply target group configuration
  117.             $this->applyTargetGroups($request$document);
  118.             $this->documentResolver->setDocument($request$document);
  119.         }
  120.     }
  121.     /**
  122.      * @param Request $request
  123.      * @param Document $document
  124.      *
  125.      * @return Document
  126.      */
  127.     protected function handleVersion(Request $requestDocument $document)
  128.     {
  129.         if ($request->get('v')) {
  130.             if ($version Version::getById($request->get('v'))) {
  131.                 if ($version->getPublic()) {
  132.                     $this->logger->info('Setting version to {version} for document {document}', [
  133.                         'version' => $version->getId(),
  134.                         'document' => $document->getFullPath()
  135.                     ]);
  136.                     $document $version->getData();
  137.                 }
  138.             } else {
  139.                 $this->logger->notice('Failed to load {version} for document {document}', [
  140.                     'version' => $request->get('v'),
  141.                     'document' => $document->getFullPath()
  142.                 ]);
  143.             }
  144.         }
  145.         return $document;
  146.     }
  147.     protected function applyTargetGroups(Request $requestDocument $document)
  148.     {
  149.         if (!$document instanceof Document\Targeting\TargetingDocumentInterface || null !== Staticroute::getCurrentRoute()) {
  150.             return;
  151.         }
  152.         // reset because of preview and editmode (saved in session)
  153.         $document->setUseTargetGroup(null);
  154.         $this->targetingConfigurator->configureTargetGroup($document);
  155.         if ($document->getUseTargetGroup()) {
  156.             $this->logger->info('Setting target group to {targetGroup} for document {document}', [
  157.                 'targetGroup' => $document->getUseTargetGroup(),
  158.                 'document' => $document->getFullPath()
  159.             ]);
  160.         }
  161.     }
  162.     /**
  163.      * @param Request $request
  164.      * @param Document $document
  165.      *
  166.      * @return Document
  167.      */
  168.     protected function handleAdminUserDocumentParams(Request $requestDocument $document)
  169.     {
  170.         // editmode document
  171.         if ($this->editmodeResolver->isEditmode($request)) {
  172.             $document $this->handleEditmode($document);
  173.         }
  174.         // document preview
  175.         if ($request->get('pimcore_preview')) {
  176.             // get document from session
  177.             // TODO originally, this was the following call. What was in this->getParam('document') and
  178.             // why was it an object?
  179.             // $docKey = "document_" . $this->getParam("document")->getId();
  180.             if ($documentFromSession Document\Service::getElementFromSession('document'$document->getId())) {
  181.                 // if there is a document in the session use it
  182.                 $this->logger->debug('Loading preview document {document} from session', [
  183.                     'document' => $document->getFullPath()
  184.                 ]);
  185.                 $document $documentFromSession;
  186.             }
  187.         }
  188.         // for version preview
  189.         if ($request->get('pimcore_version')) {
  190.             // TODO there was a check with a registry flag here - check if the master request handling is sufficient
  191.             if ($version Version::getById($request->get('pimcore_version'))) {
  192.                 $document $version->getData();
  193.                 $this->logger->debug('Loading version {version} for document {document} from pimcore_version parameter', [
  194.                     'version' => $version->getId(),
  195.                     'document' => $document->getFullPath()
  196.                 ]);
  197.             } else {
  198.                 $this->logger->warning('Failed to load {version} for document {document} from pimcore_version parameter', [
  199.                     'version' => $request->get('pimcore_version'),
  200.                     'document' => $document->getFullPath()
  201.                 ]);
  202.                 throw new NotFoundHttpException(
  203.                     sprintf('Failed to load %s for document %s from pimcore_version parameter',
  204.                         $request->get('pimcore_version'), $document->getFullPath()));
  205.             }
  206.         }
  207.         return $document;
  208.     }
  209.     /**
  210.      * @param Document $document
  211.      *
  212.      * @return mixed|Document|Document\PageSnippet
  213.      */
  214.     protected function handleEditmode(Document $document)
  215.     {
  216.         // check if there is the document in the session
  217.         if ($documentFromSession Document\Service::getElementFromSession('document'$document->getId())) {
  218.             // if there is a document in the session use it
  219.             $this->logger->debug('Loading editmode document {document} from session', [
  220.                 'document' => $document->getFullPath()
  221.             ]);
  222.             $document $documentFromSession;
  223.         } else {
  224.             $this->logger->debug('Loading editmode document {document} from latest version', [
  225.                 'document' => $document->getFullPath()
  226.             ]);
  227.             // set the latest available version for editmode if there is no doc in the session
  228.             $latestVersion $document->getLatestVersion();
  229.             if ($latestVersion) {
  230.                 $latestDoc $latestVersion->loadData();
  231.                 if ($latestDoc instanceof Document\PageSnippet) {
  232.                     $document $latestDoc;
  233.                 }
  234.             }
  235.         }
  236.         return $document;
  237.     }
  238.     /**
  239.      * @param Request $request
  240.      */
  241.     protected function handleObjectParams(Request $request)
  242.     {
  243.         // object preview
  244.         if ($objectId $request->get('pimcore_object_preview')) {
  245.             if ($object Service::getElementFromSession('object'$objectId)) {
  246.                 $this->logger->debug('Loading object {object} ({objectId}) from session', [
  247.                     'object' => $object->getFullPath(),
  248.                     'objectId' => $object->getId()
  249.                 ]);
  250.                 // TODO remove \Pimcore\Cache\Runtime
  251.                 // add the object to the registry so every call to DataObject::getById() will return this object instead of the real one
  252.                 \Pimcore\Cache\Runtime::set('object_' $object->getId(), $object);
  253.             }
  254.         }
  255.     }
  256. }