vendor/sonata-project/page-bundle/src/CmsManager/CmsManagerSelector.php line 63

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\PageBundle\CmsManager;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\PageBundle\Model\PageInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  20. use Symfony\Component\Security\Http\Event\LogoutEvent;
  21. /**
  22.  * This class return the correct manager instance:
  23.  *   - sonata.page.cms.page if the user is an editor (ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT)
  24.  *   - sonata.page.cms.snapshot if the user is a standard user.
  25.  *
  26.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  27.  */
  28. final class CmsManagerSelector implements CmsManagerSelectorInterface
  29. {
  30.     /**
  31.      * @param AdminInterface<PageInterface> $pageAdmin
  32.      */
  33.     public function __construct(
  34.         private CmsPageManager $cmsPageManager,
  35.         private CmsSnapshotManager $cmsSnapshotManager,
  36.         private AdminInterface $pageAdmin,
  37.         private TokenStorageInterface $tokenStorage,
  38.         private RequestStack $requestStack
  39.     ) {
  40.     }
  41.     public function retrieve(): CmsManagerInterface
  42.     {
  43.         return $this->isEditor() ? $this->cmsPageManager $this->cmsSnapshotManager;
  44.     }
  45.     /**
  46.      * The current order of event is not suitable for the selector to be call
  47.      * by the router chain, so we need to use another mechanism. It is not perfect
  48.      * but do the job for now.
  49.      */
  50.     public function isEditor(): bool
  51.     {
  52.         $request $this->requestStack->getCurrentRequest();
  53.         return null !== $request &&
  54.             $request->hasSession() &&
  55.             false !== $request->getSession()->get('sonata/page/isEditor'false);
  56.     }
  57.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  58.     {
  59.         if (null !== $this->tokenStorage->getToken() &&
  60.             $this->pageAdmin->isGranted('EDIT')) {
  61.             $request $event->getRequest();
  62.             if ($request->hasSession()) {
  63.                 $request->getSession()->set('sonata/page/isEditor'true);
  64.             }
  65.         }
  66.     }
  67.     /**
  68.      * NEXT_MAJOR: Remove this method.
  69.      */
  70.     public function logout(Request $requestResponse $responseTokenInterface $token): void
  71.     {
  72.         if ($request->hasSession()) {
  73.             $request->getSession()->set('sonata/page/isEditor'false);
  74.         }
  75.         if ($request->cookies->has('sonata_page_is_editor')) {
  76.             $response->headers->clearCookie('sonata_page_is_editor');
  77.         }
  78.     }
  79.     public function onLogout(LogoutEvent $event): void
  80.     {
  81.         $request $event->getRequest();
  82.         if ($request->hasSession()) {
  83.             $request->getSession()->set('sonata/page/isEditor'false);
  84.         }
  85.         if ($request->cookies->has('sonata_page_is_editor')) {
  86.             $response $event->getResponse();
  87.             if (null !== $response) {
  88.                 $response->headers->clearCookie('sonata_page_is_editor');
  89.             }
  90.         }
  91.     }
  92. }