Skip to content

Commit

Permalink
Use the Knp menu for the back end header menu (see contao#860)
Browse files Browse the repository at this point in the history
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
leofeyer committed Jan 3, 2020
1 parent 6b3340b commit 2aeda7f
Show file tree
Hide file tree
Showing 26 changed files with 1,889 additions and 479 deletions.
108 changes: 0 additions & 108 deletions core-bundle/src/EventListener/BackendMenuListener.php

This file was deleted.

108 changes: 108 additions & 0 deletions core-bundle/src/EventListener/Menu/BackendLogoutListener.php
@@ -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);
}
}

0 comments on commit 2aeda7f

Please sign in to comment.