forked from contao/contao
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the Knp menu for the back end header menu (see contao#860)
Description ----------- This PR makes the necessary changes so the back end header menu is built via Knp menu and the debug button can be added in the manager bundle (this was an open TODO). The PR also "fixes" the back end main menu, which does not adhere to the Knp menu standards and thus is only partially extendable. ### TODOs - [x] Rework the main menu - [x] Fix the existing unit tests - [x] Add unit tests for the new classes Commits ------- fe43581 Make the back end header menu a Knp menu f4dff99 Use separate listeners for the preview URL and logout URL cde9bb1 Do not use the CURRENT_ID constant 17493b9 Fix the listener priorities 33c8a07 Merge branch 'master' into feature/knp-header-menu 8cc2107 Adjust to the new event registration 1bf08f8 Fix a few minor issues 2e36141 Check for the user role instead of the class
- Loading branch information
Showing
26 changed files
with
1,889 additions
and
479 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
core-bundle/src/EventListener/Menu/BackendLogoutListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener\Menu; | ||
|
||
use Contao\CoreBundle\Event\MenuEvent; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Http\Firewall\SwitchUserListener; | ||
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator as BaseLogoutUrlGenerator; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class BackendLogoutListener | ||
{ | ||
/** | ||
* @var Security | ||
*/ | ||
private $security; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @var BaseLogoutUrlGenerator | ||
*/ | ||
private $urlGenerator; | ||
|
||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
public function __construct(Security $security, RouterInterface $router, BaseLogoutUrlGenerator $urlGenerator, TranslatorInterface $translator) | ||
{ | ||
$this->security = $security; | ||
$this->router = $router; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->translator = $translator; | ||
} | ||
|
||
public function __invoke(MenuEvent $event): void | ||
{ | ||
if (!$this->security->isGranted('ROLE_USER')) { | ||
return; | ||
} | ||
|
||
$tree = $event->getTree(); | ||
|
||
if ('headerMenu' !== $tree->getName() || !$submenu = $tree->getChild('submenu')) { | ||
return; | ||
} | ||
|
||
$logout = $event | ||
->getFactory() | ||
->createItem('logout') | ||
->setLabel($this->getLogoutLabel()) | ||
->setUri($this->getLogoutUrl()) | ||
->setLinkAttribute('class', 'icon-logout') | ||
->setLinkAttribute('accesskey', 'q') | ||
->setExtra('translation_domain', false) | ||
; | ||
|
||
$submenu->addChild($logout); | ||
} | ||
|
||
private function getLogoutLabel(): string | ||
{ | ||
$token = $this->security->getToken(); | ||
|
||
if ($token instanceof SwitchUserToken) { | ||
return $this->translator->trans( | ||
'MSC.switchBT', | ||
[$token->getOriginalToken()->getUsername()], | ||
'contao_default' | ||
); | ||
} | ||
|
||
return $this->translator->trans('MSC.logoutBT', [], 'contao_default'); | ||
} | ||
|
||
private function getLogoutUrl(): string | ||
{ | ||
$token = $this->security->getToken(); | ||
|
||
if (!$token instanceof SwitchUserToken) { | ||
return $this->urlGenerator->getLogoutUrl(); | ||
} | ||
|
||
$params = ['do' => 'user', '_switch_user' => SwitchUserListener::EXIT_VALUE]; | ||
|
||
return $this->router->generate('contao_backend', $params); | ||
} | ||
} |
Oops, something went wrong.