Skip to content

Commit

Permalink
Improve Gate Filter / Fix Issue #1111 (#1259)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dfsmania authored Mar 18, 2024
1 parent 17d9f1a commit 4c031a6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Helpers/MenuItemHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
17 changes: 12 additions & 5 deletions tests/AdminLteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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']);
}
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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']);
}

Expand Down
87 changes: 87 additions & 0 deletions tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 4c031a6

Please sign in to comment.