'ROLE_ADMIN', 'app_manager_' => 'ROLE_MANAGER', 'app_house_manager_' => 'ROLE_HOUSE_MANAGER', 'app_teamer_' => 'ROLE_TEAMER', ]; /** * Priority order of the area roles, must stay in sync with User::getDefaultRoute(). */ protected const ROLE_PRIORITY = [ 'ROLE_ADMIN', 'ROLE_MANAGER', 'ROLE_HOUSE_MANAGER', 'ROLE_TEAMER', ]; protected const AREA_SESSION_KEY = 'menu_area'; public function __construct( protected FactoryInterface $factory, protected Security $security, protected RequestStack $requestStack, protected TranslatorInterface $translator, ) { } protected function createRootElement(): ItemInterface { return $this->factory->createItem('root'); } /** * Returns the role owning the area the current request belongs to. * * Entering an area that has pages of its own selects that area and keeps it * for the rest of the session. The routes shared by all administrative roles * (app_administrative_*) carry no area of their own and therefore stay in the * area the user entered last, so that switching areas sticks. Without a * remembered area the highest role wins. */ protected function resolveArea(): ?string { $request = $this->requestStack->getMainRequest(); $session = null !== $this->security->getUser() && true === $request?->hasSession() ? $request->getSession() : null; $route = $request?->attributes->get('_route'); if (is_string($route)) { foreach (self::AREA_ROUTE_PREFIXES as $prefix => $role) { if (str_starts_with($route, $prefix) && $this->security->isGranted($role)) { $session?->set(self::AREA_SESSION_KEY, $role); return $role; } } } $rememberedArea = $session?->get(self::AREA_SESSION_KEY); // The remembered area is re-checked, roles can be revoked mid-session. if (is_string($rememberedArea) && $this->security->isGranted($rememberedArea)) { return $rememberedArea; } foreach (self::ROLE_PRIORITY as $role) { if ($this->security->isGranted($role)) { return $role; } } return null; } protected function getDefaultRouteParameters(string $parameter = 'id', string $default = '0'): array { $request = $this->requestStack->getMainRequest(); return [ $parameter => $request->get($parameter, $default), 'r' => $request->get('r'), ]; } /** * Adds the link leading back out of a detail view. * * The return url is taken from the menu options first, then from the request, * and falls back to the overview itself. Without that fallback the item ends up * without an uri and is rendered as a dead label instead of a link. * * @param array $options */ protected function addBackItem( ItemInterface $menu, array $options, string $defaultRoute = 'app_administrative_teamer_index', ): void { $this->addDivider($menu); // Already decoded by ReturnUrlTrait::getReturnUrl(), must not be decoded again. $returnUrl = $options['return_url'] ?? null; if (false === is_string($returnUrl) || '' === $returnUrl) { $requestReturnUrl = $this->requestStack->getMainRequest()?->get('r'); $returnUrl = is_string($requestReturnUrl) && '' !== $requestReturnUrl ? rawurldecode($requestReturnUrl) : null; } $menu->addChild('zurück zur Übersicht', [ ...(null !== $returnUrl ? ['uri' => $returnUrl] : ['route' => $defaultRoute]), 'extras' => [ 'icon' => 'arrow-left', ], ]); } protected function addAdminItem(ItemInterface $menu): void { if ($this->security->isGranted('ROLE_ADMIN')) { $this->addDivider($menu); $menu->addChild('zum Adminbereich', [ 'route' => 'app_admin_index', 'linkAttributes' => [ 'title' => 'zum Adminbereich', ], 'extras' => [ 'icon' => 'user', ], ]); } } protected function addManagerItem(ItemInterface $menu): void { if ($this->security->isGranted('ROLE_MANAGER')) { $this->addDivider($menu); $menu->addChild('zum Managementbereich', [ 'route' => 'app_manager_index', 'linkAttributes' => [ 'title' => 'zum Managementbereich', ], 'extras' => [ 'icon' => 'user', ], ]); } } protected function addTeamerItem(ItemInterface $menu): void { if ($this->security->isGranted('ROLE_TEAMER')) { $this->addDivider($menu); $menu->addChild('zum Teambereich', [ 'route' => 'app_teamer_index', 'linkAttributes' => [ 'title' => 'zum Teambereich', ], 'extras' => [ 'icon' => 'user', ], ]); } } protected function addHouseManagerItem(ItemInterface $menu): void { if ($this->security->isGranted('ROLE_HOUSE_MANAGER')) { $this->addDivider($menu); $menu->addChild('zum Hausleitungsbereich', [ 'route' => 'app_house_manager_index', 'linkAttributes' => [ 'title' => 'zum Hausleitungsbereich', ], 'extras' => [ 'icon' => 'user', ], ]); } } protected function addLogoutItem(ItemInterface $menu): void { $this->addDivider($menu); $token = $this->security->getToken(); if ($token instanceof SwitchUserToken) { /** @var User $originalUser */ $originalUser = $token->getOriginalToken()->getUser(); $menu->addChild('zum eigenen Benutzer', [ 'route' => $originalUser->getDefaultRoute(), 'routeParameters' => ['_switch_user' => '_exit'], 'linkAttributes' => [ 'title' => 'zurück', ], 'extras' => [ 'icon' => 'logout', ], ]); } else { $menu->addChild('Logout', [ 'route' => 'app_security_logout', 'linkAttributes' => [ 'title' => 'Logout', ], 'extras' => [ 'icon' => 'logout', ], ]); } } protected function addDivider(ItemInterface $menu): void { $menu->addChild(Uuid::v4(), [ 'extras' => [ 'divider' => true, ], ]); } }