From 4c031a6f45d182847010fc841deaa298602460c9 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Mon, 18 Mar 2024 11:42:51 -0300 Subject: [PATCH] Improve Gate Filter / Fix Issue #1111 (#1259) * Improve Gate Filter The Gate filter now restrict submenu items with all restircted children * Fix code style * Revert the Gate Filter changes and move logic to upper level * Fix CS --- src/Helpers/MenuItemHelper.php | 8 ++++ tests/AdminLteTest.php | 17 +++++-- tests/Menu/BuilderTest.php | 87 ++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/Helpers/MenuItemHelper.php b/src/Helpers/MenuItemHelper.php index edc62729..5df95a43 100644 --- a/src/Helpers/MenuItemHelper.php +++ b/src/Helpers/MenuItemHelper.php @@ -64,6 +64,14 @@ public static function isLegacySearch($item) */ public static function isAllowed($item) { + // We won't allow empty submenu items on the menu. + + if (self::isSubmenu($item) && ! count($item['submenu'])) { + return false; + } + + // In any other case, fallback to the restricted property. + return $item && empty($item['restricted']); } } diff --git a/tests/AdminLteTest.php b/tests/AdminLteTest.php index 9acaa0a2..fafa234d 100644 --- a/tests/AdminLteTest.php +++ b/tests/AdminLteTest.php @@ -43,9 +43,16 @@ public function addMenuItems(BuildingMenu $event) $event->menu->add(['text' => 'searchLT', 'type' => 'navbar-search', 'topnav' => true]); - // Add (1) submenu to the sidebar menu. + // Add (1) empty submenu to the sidebar menu. This item should be + // filtered out of the menu. - $event->menu->add(['text' => 'submenu', 'submenu' => []]); + $event->menu->add(['text' => 'submenu1', 'submenu' => []]); + + // Add (1) non empty submenu to the sidebar menu. + + $event->menu->add(['text' => 'submenu2', 'submenu' => [ + ['text' => 'subitem', 'url' => 'url'], + ]]); // Add (1) invalid item. @@ -70,7 +77,7 @@ public function testMenuWithoutFilters() $this->assertEquals('topnavRT', $menu[6]['text']); $this->assertEquals('topnavUT', $menu[7]['text']); $this->assertEquals('searchLT', $menu[8]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('invalid', $menu[10]['text']); $this->assertEquals('search', $menu[11]['text']); } @@ -90,7 +97,7 @@ public function testMenuSidebarFilter() $this->assertEquals('topnavLF', $menu[2]['text']); $this->assertEquals('topnavRF', $menu[3]['text']); $this->assertEquals('topnavUF', $menu[4]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('search', $menu[11]['text']); } @@ -131,7 +138,7 @@ public function testMenuNavbarLeftFilter() $this->assertEquals('topnavUF', $menu[4]['text']); $this->assertEquals('topnavLT', $menu[5]['text']); $this->assertEquals('searchLT', $menu[8]['text']); - $this->assertEquals('submenu', $menu[9]['text']); + $this->assertEquals('submenu2', $menu[9]['text']); $this->assertEquals('search', $menu[11]['text']); } diff --git a/tests/Menu/BuilderTest.php b/tests/Menu/BuilderTest.php index 592622f0..ef7c5b97 100644 --- a/tests/Menu/BuilderTest.php +++ b/tests/Menu/BuilderTest.php @@ -798,6 +798,93 @@ function () { $this->assertStringContainsString('HEADER', $builder->menu[0]['header']); } + public function testCanOnSubmenu() + { + $gate = $this->makeGate(); + + $gate->define('show-about', function () { + return true; + }); + + $gate->define('show-home', function () { + return false; + }); + + $builder = $this->makeMenuBuilder('http://example.com', $gate); + + $builder->add( + [ + 'text' => 'Submenu', + 'submenu' => [ + [ + 'text' => 'About', + 'url' => 'about', + 'can' => 'show-about', + ], + [ + 'text' => 'Home', + 'url' => '/', + 'can' => 'show-home', + ], + ], + ] + ); + + $this->assertCount(1, $builder->menu); + $this->assertCount(1, $builder->menu[0]['submenu']); + $this->assertEquals('About', $builder->menu[0]['submenu'][0]['text']); + } + + public function testCanOnWholeRestrictedSubmenu() + { + $gate = $this->makeGate(); + + $gate->define('show-about', function () { + return false; + }); + + $gate->define('show-home', function () { + return false; + }); + + $builder = $this->makeMenuBuilder('http://example.com', $gate); + + $builder->add( + [ + 'text' => 'Submenu1', + 'submenu' => [ + [ + 'text' => 'About', + 'url' => 'about', + 'can' => 'show-about', + ], + [ + 'text' => 'Home', + 'url' => '/', + 'can' => 'show-home', + ], + [ + 'text' => 'Submenu2', + 'submenu' => [ + [ + 'text' => 'Home1', + 'url' => '/home1', + 'can' => 'show-home', + ], + [ + 'text' => 'Home2', + 'url' => '/home2', + 'can' => 'show-home', + ], + ], + ], + ], + ] + ); + + $this->assertCount(0, $builder->menu); + } + public function testLangTranslate() { $builder = $this->makeMenuBuilder('http://example.com');