vendor/sulu/sulu/src/Sulu/Bundle/DocumentManagerBundle/Document/Subscriber/SecuritySubscriber.php line 48

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\DocumentManagerBundle\Document\Subscriber;
  11. use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
  12. use Sulu\Component\DocumentManager\Events;
  13. use Sulu\Component\Security\Authentication\UserInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. /**
  18.  * Responsible for integrating the security part of Sulu to the DocumentManager.
  19.  */
  20. class SecuritySubscriber implements EventSubscriberInterface
  21. {
  22.     public const USER_OPTION 'user';
  23.     /**
  24.      * @var TokenStorageInterface
  25.      */
  26.     private $tokenStorage;
  27.     public function __construct(TokenStorageInterface $tokenStorage null)
  28.     {
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             Events::CONFIGURE_OPTIONS => 'setDefaultUser',
  35.         ];
  36.     }
  37.     /**
  38.      * Sets the default user from the session.
  39.      */
  40.     public function setDefaultUser(ConfigureOptionsEvent $event)
  41.     {
  42.         $optionsResolver $event->getOptions();
  43.         $optionsResolver->setDefault(static::USER_OPTIONnull);
  44.         if (null === $this->tokenStorage) {
  45.             return;
  46.         }
  47.         $token $this->tokenStorage->getToken();
  48.         if (null === $token || $token instanceof AnonymousToken) {
  49.             return;
  50.         }
  51.         $user $token->getUser();
  52.         if (!$user instanceof UserInterface) {
  53.             return;
  54.         }
  55.         $optionsResolver->setDefault(static::USER_OPTION$user->getId());
  56.     }
  57. }