feat: add a Mein Account group to the main menu and lock the page while the drawer is open
This commit is contained in:
1 parent
4530d2434b
commit
8a02b28d00
6 files changed
+164
-24
No files matched your search
@@ -252,6 +252,14 @@
|
||||
@apply m-0 h-dvh max-h-none w-80 max-w-[calc(100vw-4rem)] bg-page p-0;
|
||||
}
|
||||
|
||||
/*
|
||||
* a modal dialog doesn't stop the page behind it from scrolling: on touch screens the page took the
|
||||
* gesture first, and a long navigation in the drawer only scrolled once the page was at its end
|
||||
*/
|
||||
html:has(dialog.drawer:modal) {
|
||||
@apply overflow-hidden;
|
||||
}
|
||||
|
||||
/* lists in a card: the outer cells line up with the card's padding */
|
||||
.data-table {
|
||||
@apply w-full;
|
||||
|
||||
@@ -150,20 +150,33 @@ abstract class AbstractMenuBuilder
|
||||
];
|
||||
|
||||
/**
|
||||
* Adds the links to the other areas the user has, then logout, or the way back to the
|
||||
* own user while impersonating someone.
|
||||
* Adds the own profile, the links to the other areas the user has, then logout, or the way
|
||||
* back to the own user while impersonating someone.
|
||||
*
|
||||
* Without icons for the sidebar, where they are sub-items and sub-items carry none.
|
||||
*/
|
||||
protected function addAccountItems(ItemInterface $menu): void
|
||||
protected function addAccountItems(ItemInterface $menu, bool $withIcons = true, bool $withProfile = true): void
|
||||
{
|
||||
$currentArea = $this->resolveArea();
|
||||
$user = $this->security->getUser();
|
||||
$profileRoute = match (true) {
|
||||
$user instanceof User && null !== $user->getTeamer() => 'app_teamer_profile_index',
|
||||
$this->security->isGranted('ROLE_ADMINISTRATIVE') => 'app_administrative_profile_index',
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($withProfile && null !== $profileRoute) {
|
||||
$menu->addChild('Mein Profil', [
|
||||
'route' => $profileRoute,
|
||||
'extras' => $withIcons ? ['icon' => 'user'] : [],
|
||||
]);
|
||||
}
|
||||
|
||||
foreach (self::AREA_LINKS as $role => [$label, $route]) {
|
||||
if ($role !== $currentArea && $this->security->isGranted($role)) {
|
||||
$menu->addChild($label, [
|
||||
'route' => $route,
|
||||
'extras' => [
|
||||
'icon' => 'arrow-right',
|
||||
],
|
||||
'extras' => $withIcons ? ['icon' => 'arrow-right'] : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -176,16 +189,12 @@ abstract class AbstractMenuBuilder
|
||||
$menu->addChild('zum eigenen Benutzer', [
|
||||
'route' => $originalUser->getDefaultRoute(),
|
||||
'routeParameters' => ['_switch_user' => '_exit'],
|
||||
'extras' => [
|
||||
'icon' => 'logout',
|
||||
],
|
||||
'extras' => $withIcons ? ['icon' => 'logout'] : [],
|
||||
]);
|
||||
} else {
|
||||
$menu->addChild('Logout', [
|
||||
'route' => 'app_security_logout',
|
||||
'extras' => [
|
||||
'icon' => 'logout',
|
||||
],
|
||||
'extras' => $withIcons ? ['icon' => 'logout'] : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Menu;
|
||||
|
||||
use App\Entity\User;
|
||||
use Knp\Menu\FactoryInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
@@ -31,17 +32,36 @@ class MenuBuilder extends AbstractMenuBuilder
|
||||
|
||||
public function createMainMenu(array $options): ItemInterface
|
||||
{
|
||||
return match ($this->resolveArea()) {
|
||||
$area = $this->resolveArea();
|
||||
$menu = match ($area) {
|
||||
'ROLE_TEAM_ADMIN' => $this->adminMenuBuilder->createMainMenu($options),
|
||||
'ROLE_MANAGER' => $this->managerMenuBuilder->createMainMenu($options),
|
||||
'ROLE_HOUSE_MANAGER' => $this->houseManagerMenuBuilder->createMainMenu($options),
|
||||
'ROLE_TEAMER' => $this->teamerMenuBuilder->createMainMenu($options),
|
||||
default => $this->createRootElement(),
|
||||
};
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if ($user instanceof User) {
|
||||
$this->addDivider($menu);
|
||||
|
||||
// A group without uri, so the sidebar renders it collapsible; the teamer menu has its own "Mein Profil".
|
||||
$account = $menu->addChild('account', [
|
||||
'label' => 'Mein Account',
|
||||
'extras' => [
|
||||
'icon' => 'user',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->addAccountItems($account, withIcons: false, withProfile: 'ROLE_TEAMER' !== $area);
|
||||
}
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* The account menu behind the user's picture in the header: the other areas and logout.
|
||||
* The account menu behind the user's picture in the header: own profile, the other areas and logout.
|
||||
*/
|
||||
public function createUserMenu(array $options): ItemInterface
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{#
|
||||
The account menu behind the user's picture: own profile, the other areas (MenuBuilder::createUserMenu)
|
||||
and logout. Opens on click, closes on a click elsewhere and on Escape.
|
||||
The account menu behind the user's picture: own profile, the other areas and logout
|
||||
(MenuBuilder::createUserMenu). Opens on click, closes on a click elsewhere and on Escape. Only from lg
|
||||
up; below that the same items are reached through the main menu in the drawer.
|
||||
#}
|
||||
{% set user_menu = knp_menu_get('default_user') %}
|
||||
<div class="relative"
|
||||
@@ -22,11 +23,6 @@
|
||||
<div class="hidden absolute right-0 top-full mt-2 z-40 min-w-56 border border-line bg-white py-1 text-sm shadow-lg"
|
||||
role="menu"
|
||||
{{ stimulus_target('toggle', 'toggle') }}>
|
||||
{% if app.user.teamer %}
|
||||
<a href="{{ path('app_teamer_profile_index') }}" class="menu-item flex items-center gap-x-3" role="menuitem">{{ icon('user', 'w-5 h-5 text-muted') }}Mein Profil</a>
|
||||
{% elseif is_granted('ROLE_ADMINISTRATIVE') %}
|
||||
<a href="{{ path('app_administrative_profile_index') }}" class="menu-item flex items-center gap-x-3" role="menuitem">{{ icon('user', 'w-5 h-5 text-muted') }}Mein Profil</a>
|
||||
{% endif %}
|
||||
{% for item in user_menu.children %}
|
||||
{% if loop.last %}<hr class="my-1 border-line">{% endif %}
|
||||
<a href="{{ item.uri }}" class="menu-item flex items-center gap-x-3" role="menuitem">{{ icon(item.extras.icon, 'w-5 h-5 text-muted') }}{{ item.label }}</a>
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
{#
|
||||
Sidebar layout of the E&P corporate design (ep-corporate-design, layout_sidebar.html.twig): the
|
||||
main menu in a left sidebar from lg up, below that in a drawer behind the "Menü" button. The area
|
||||
layouts fill `main_menu` and `mobile_menu` with the same KnpMenu menu.
|
||||
layouts fill `main_menu` and `mobile_menu` with the same KnpMenu menu, which ends with the account
|
||||
group; the header's user menu shows from lg up only.
|
||||
#}
|
||||
{% block body %}
|
||||
<div {{ stimulus_controller('drawer') }}>
|
||||
@@ -13,7 +14,7 @@
|
||||
} %}
|
||||
{% block header_end %}
|
||||
{% if app.user %}
|
||||
<div class="flex items-center">
|
||||
<div class="hidden lg:flex items-center">
|
||||
{% include '_partials/_user_menu.html.twig' %}
|
||||
</div>
|
||||
<button type="button" class="btn-tool self-center lg:hidden" aria-haspopup="dialog"
|
||||
@@ -42,7 +43,7 @@
|
||||
<img src="{{ asset('build/images/ep-logo.svg') }}" alt="" class="h-9 w-auto">
|
||||
<button type="button" class="btn-tool" {{ stimulus_action('drawer', 'close') }}>Schließen</button>
|
||||
</div>
|
||||
<nav class="flex-1 overflow-y-auto px-4 pb-8">
|
||||
<nav class="flex-1 overflow-y-auto overscroll-contain px-4 pb-8">
|
||||
{% block mobile_menu %}{% endblock %}
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Menu;
|
||||
|
||||
use App\Entity\Teamer;
|
||||
use App\Entity\User;
|
||||
use App\Menu\MenuBuilder;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
|
||||
/**
|
||||
* The account items (profile, area switch, logout) close the main menu as one group, so they are
|
||||
* reachable from the sidebar and from the drawer on small screens, not only from the header.
|
||||
*/
|
||||
class MainMenuAccountGroupTest extends KernelTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testTheGroupClosesTheMenuWithProfileAreaSwitchAndLogout(): void
|
||||
{
|
||||
$this->login($this->user(['ROLE_TEAM_ADMIN', 'ROLE_TEAMER']), 'app_admin_index');
|
||||
|
||||
$account = $this->accountGroup();
|
||||
|
||||
self::assertSame('Mein Account', $account->getLabel());
|
||||
self::assertNull($account->getUri());
|
||||
self::assertSame(['Mein Profil', 'zum Teambereich', 'Logout'], array_keys($account->getChildren()));
|
||||
self::assertSame('/administrative/profile', $account->getChild('Mein Profil')->getUri());
|
||||
}
|
||||
|
||||
public function testTheSubItemsCarryNoIcons(): void
|
||||
{
|
||||
$this->login($this->user(['ROLE_TEAM_ADMIN', 'ROLE_TEAMER']), 'app_admin_index');
|
||||
|
||||
foreach ($this->accountGroup()->getChildren() as $child) {
|
||||
self::assertArrayNotHasKey('icon', $child->getExtras(), $child->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function testTheTeamerAreaKeepsItsOwnProfileGroup(): void
|
||||
{
|
||||
$user = $this->user(['ROLE_TEAMER']);
|
||||
$user->setTeamer(new Teamer());
|
||||
$this->login($user, 'app_teamer_index');
|
||||
|
||||
self::assertSame(['Logout'], array_keys($this->accountGroup()->getChildren()));
|
||||
}
|
||||
|
||||
public function testImpersonationOffersTheWayBackInsteadOfLogout(): void
|
||||
{
|
||||
$admin = $this->user(['ROLE_TEAM_ADMIN']);
|
||||
$manager = $this->user(['ROLE_MANAGER']);
|
||||
$original = new UsernamePasswordToken($admin, 'main', $admin->getRoles());
|
||||
$this->login($manager, 'app_manager_index', new SwitchUserToken($manager, 'main', $manager->getRoles(), $original));
|
||||
|
||||
$children = array_keys($this->accountGroup()->getChildren());
|
||||
|
||||
self::assertContains('zum eigenen Benutzer', $children);
|
||||
self::assertNotContains('Logout', $children);
|
||||
}
|
||||
|
||||
private function accountGroup(): ItemInterface
|
||||
{
|
||||
$menu = self::getContainer()->get(MenuBuilder::class)->createMainMenu([]);
|
||||
$account = $menu->getChild('account');
|
||||
|
||||
self::assertNotNull($account);
|
||||
self::assertSame($account, $menu->getLastChild());
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
private function user(array $roles): User
|
||||
{
|
||||
return new User()
|
||||
->setFirstName('Ada')
|
||||
->setEmail('[email protected]')
|
||||
->setRoles($roles);
|
||||
}
|
||||
|
||||
private function login(User $user, string $route, ?UsernamePasswordToken $token = null): void
|
||||
{
|
||||
self::getContainer()->get(TokenStorageInterface::class)
|
||||
->setToken($token ?? new UsernamePasswordToken($user, 'main', $user->getRoles()));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_route', $route);
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
self::getContainer()->get(RequestStack::class)->push($request);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user