vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Markup/Link/PageLinkProvider.php line 96

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\PageBundle\Markup\Link;
  11. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
  12. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
  13. use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;
  14. use Sulu\Bundle\PageBundle\Admin\PageAdmin;
  15. use Sulu\Component\Content\Repository\Content;
  16. use Sulu\Component\Content\Repository\ContentRepositoryInterface;
  17. use Sulu\Component\Content\Repository\Mapping\MappingBuilder;
  18. use Sulu\Component\Security\Authentication\UserInterface;
  19. use Sulu\Component\Security\Authorization\AccessControl\AccessControlManagerInterface;
  20. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. class PageLinkProvider implements LinkProviderInterface
  25. {
  26.     /**
  27.      * @var ContentRepositoryInterface
  28.      */
  29.     protected $contentRepository;
  30.     /**
  31.      * @var WebspaceManagerInterface
  32.      */
  33.     protected $webspaceManager;
  34.     /**
  35.      * @var RequestStack
  36.      */
  37.     protected $requestStack;
  38.     /**
  39.      * @var TranslatorInterface
  40.      */
  41.     private $translator;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $environment;
  46.     /**
  47.      * @var AccessControlManagerInterface
  48.      */
  49.     private $accessControlManager;
  50.     /**
  51.      * @var TokenStorageInterface
  52.      */
  53.     private $tokenStorage;
  54.     public function __construct(
  55.         ContentRepositoryInterface $contentRepository,
  56.         WebspaceManagerInterface $webspaceManager,
  57.         RequestStack $requestStack,
  58.         TranslatorInterface $translator,
  59.         string $environment,
  60.         AccessControlManagerInterface $accessControlManager,
  61.         TokenStorageInterface $tokenStorage null
  62.     ) {
  63.         $this->contentRepository $contentRepository;
  64.         $this->webspaceManager $webspaceManager;
  65.         $this->requestStack $requestStack;
  66.         $this->translator $translator;
  67.         $this->environment $environment;
  68.         $this->accessControlManager $accessControlManager;
  69.         $this->tokenStorage $tokenStorage;
  70.     }
  71.     public function getConfiguration()
  72.     {
  73.         return LinkConfigurationBuilder::create()
  74.             ->setTitle($this->translator->trans('sulu_page.pages', [], 'admin'))
  75.             ->setResourceKey('pages')
  76.             ->setListAdapter('column_list')
  77.             ->setDisplayProperties(['title'])
  78.             ->setOverlayTitle($this->translator->trans('sulu_page.single_selection_overlay_title', [], 'admin'))
  79.             ->setEmptyText($this->translator->trans('sulu_page.no_page_selected', [], 'admin'))
  80.             ->setIcon('su-document')
  81.             ->getLinkConfiguration();
  82.     }
  83.     public function preload(array $hrefs$locale$published true)
  84.     {
  85.         $request $this->requestStack->getCurrentRequest();
  86.         $scheme 'http';
  87.         $domain null;
  88.         if ($request) {
  89.             $scheme $request->getScheme();
  90.             $domain $request->getHost();
  91.         }
  92.         $contents $this->contentRepository->findByUuids(
  93.             \array_unique(\array_values($hrefs)),
  94.             $locale,
  95.             MappingBuilder::create()
  96.                 ->setResolveUrl(true)
  97.                 ->addProperties(['title''published'])
  98.                 ->setOnlyPublished($published)
  99.                 ->setHydrateGhost(false)
  100.                 ->getMapping()
  101.         );
  102.         $contents \array_filter($contents, function(Content $content) {
  103.             $webspaceKey $content->getWebspaceKey();
  104.             $targetWebspace $this->webspaceManager->findWebspaceByKey($webspaceKey);
  105.             $security $targetWebspace->getSecurity();
  106.             $system $security $security->getSystem() : null;
  107.             if (!$targetWebspace->hasWebsiteSecurity()) {
  108.                 return true;
  109.             }
  110.             $userPermissions $this->accessControlManager->getUserPermissionByArray(
  111.                 $content->getLocale(),
  112.                 PageAdmin::SECURITY_CONTEXT_PREFIX $webspaceKey,
  113.                 $content->getPermissions(),
  114.                 $this->getCurrentUser(),
  115.                 $system
  116.             );
  117.             return !isset($userPermissions['view']) || $userPermissions['view'];
  118.         });
  119.         return \array_map(
  120.             function(Content $content) use ($locale$scheme$domain) {
  121.                 return $this->getLinkItem($content$locale$scheme$domain);
  122.             },
  123.             $contents
  124.         );
  125.     }
  126.     /**
  127.      * Returns new link item.
  128.      *
  129.      * @param string $locale
  130.      * @param string $scheme
  131.      *
  132.      * @return LinkItem
  133.      */
  134.     protected function getLinkItem(Content $content$locale$scheme$domain null)
  135.     {
  136.         $published = !empty($content->getPropertyWithDefault('published'));
  137.         $url $this->webspaceManager->findUrlByResourceLocator(
  138.             $content->getUrl(),
  139.             $this->environment,
  140.             $locale,
  141.             $content->getWebspaceKey(),
  142.             $domain,
  143.             $scheme
  144.         );
  145.         return new LinkItem($content->getId(), $content->getPropertyWithDefault('title'), $url$published);
  146.     }
  147.     /**
  148.      * Returns current user or null if no user is loggedin.
  149.      *
  150.      * @return UserInterface|null
  151.      */
  152.     private function getCurrentUser()
  153.     {
  154.         if (!$this->tokenStorage) {
  155.             return null;
  156.         }
  157.         $token $this->tokenStorage->getToken();
  158.         if (!$token) {
  159.             return null;
  160.         }
  161.         $user $token->getUser();
  162.         if ($user instanceof UserInterface) {
  163.             return $user;
  164.         }
  165.         return null;
  166.     }
  167. }