vendor/contao/core-bundle/src/EventListener/Menu/BackendLogoutListener.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener\Menu;
  11. use Contao\CoreBundle\Event\MenuEvent;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
  16. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator as BaseLogoutUrlGenerator;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. /**
  19.  * @internal
  20.  */
  21. class BackendLogoutListener
  22. {
  23.     /**
  24.      * @var Security
  25.      */
  26.     private $security;
  27.     /**
  28.      * @var RouterInterface
  29.      */
  30.     private $router;
  31.     /**
  32.      * @var BaseLogoutUrlGenerator
  33.      */
  34.     private $urlGenerator;
  35.     /**
  36.      * @var TranslatorInterface
  37.      */
  38.     private $translator;
  39.     public function __construct(Security $securityRouterInterface $routerBaseLogoutUrlGenerator $urlGeneratorTranslatorInterface $translator)
  40.     {
  41.         $this->security $security;
  42.         $this->router $router;
  43.         $this->urlGenerator $urlGenerator;
  44.         $this->translator $translator;
  45.     }
  46.     public function __invoke(MenuEvent $event): void
  47.     {
  48.         if (!$this->security->isGranted('ROLE_USER')) {
  49.             return;
  50.         }
  51.         $tree $event->getTree();
  52.         if ('headerMenu' !== $tree->getName() || !$submenu $tree->getChild('submenu')) {
  53.             return;
  54.         }
  55.         $logout $event
  56.             ->getFactory()
  57.             ->createItem('logout')
  58.             ->setLabel($this->getLogoutLabel())
  59.             ->setUri($this->getLogoutUrl())
  60.             ->setLinkAttribute('class''icon-logout')
  61.             ->setLinkAttribute('accesskey''q')
  62.             ->setExtra('translation_domain'false)
  63.         ;
  64.         $submenu->addChild($logout);
  65.     }
  66.     private function getLogoutLabel(): string
  67.     {
  68.         $token $this->security->getToken();
  69.         if ($token instanceof SwitchUserToken) {
  70.             return $this->translator->trans(
  71.                 'MSC.switchBT',
  72.                 [$token->getOriginalToken()->getUsername()],
  73.                 'contao_default'
  74.             );
  75.         }
  76.         return $this->translator->trans('MSC.logoutBT', [], 'contao_default');
  77.     }
  78.     private function getLogoutUrl(): string
  79.     {
  80.         $token $this->security->getToken();
  81.         if (!$token instanceof SwitchUserToken) {
  82.             return $this->urlGenerator->getLogoutUrl();
  83.         }
  84.         $params = ['do' => 'user''_switch_user' => SwitchUserListener::EXIT_VALUE];
  85.         return $this->router->generate('contao_backend'$params);
  86.     }
  87. }