diff --git a/src/Menu/Matcher/Voter/AdminVoter.php b/src/Menu/Matcher/Voter/AdminVoter.php index acdef58f8a..a326127b41 100644 --- a/src/Menu/Matcher/Voter/AdminVoter.php +++ b/src/Menu/Matcher/Voter/AdminVoter.php @@ -32,31 +32,22 @@ public function __construct( public function matchItem(ItemInterface $item): ?bool { - $admin = $item->getExtra('admin'); $request = $this->requestStack->getMainRequest(); + if (null === $request) { + return null; + } - if ($admin instanceof AdminInterface + $admin = $item->getExtra('admin'); + if ( + $admin instanceof AdminInterface && $admin->hasRoute('list') && $admin->hasAccess('list') - && null !== $request + && $this->match($admin, $request->get('_sonata_admin')) ) { - $requestCode = $request->get('_sonata_admin'); - - if ($admin->getCode() === $requestCode) { - return true; - } - - if ($this->hasChildren($admin)) { - $isMatch = $this->matchChildren($admin->getChildren(), $requestCode); - - if (null !== $isMatch) { - return $isMatch; - } - } + return true; } $route = $item->getExtra('route'); - - if (null !== $route && null !== $request && $route === $request->get('_route')) { + if (null !== $route && $route === $request->get('_route')) { return true; } @@ -66,26 +57,18 @@ public function matchItem(ItemInterface $item): ?bool /** * @param AdminInterface $admin */ - private function hasChildren(AdminInterface $admin): bool + private function match(AdminInterface $admin, mixed $requestCode): bool { - return [] !== $admin->getChildren(); - } - - /** - * @param array> $children - */ - private function matchChildren(array $children, mixed $requestCode): ?bool - { - foreach ($children as $child) { - if ($child->getBaseCodeRoute() === $requestCode) { - return true; - } + if ($admin->getBaseCodeRoute() === $requestCode) { + return true; + } - if ($this->hasChildren($child) && true === $this->matchChildren($child->getChildren(), $requestCode)) { + foreach ($admin->getChildren() as $child) { + if ($this->match($child, $requestCode)) { return true; } } - return null; + return false; } }