Skip to content

Commit

Permalink
Revert the Gate Filter changes and move logic to upper level
Browse files Browse the repository at this point in the history
  • Loading branch information
dfsmania committed Mar 15, 2024
1 parent 178917e commit 1c05c92
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 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']);
}
}
24 changes: 1 addition & 23 deletions src/Menu/Filters/GateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public function transform($item)
// Set a special attribute when item is not allowed. Items with this
// attribute will be filtered out of the menu.

$isWholeRestrictedSubmenu = MenuItemHelper::isSubmenu($item)
&& $this->allItemsRestricted($item['submenu']);

if (! $this->isAllowed($item) || $isWholeRestrictedSubmenu) {
if (! $this->isAllowed($item)) {
$item['restricted'] = true;
}

Expand Down Expand Up @@ -72,23 +69,4 @@ protected function isAllowed($item)

return true;
}

/**
* Check if a set of items are all restricted (or unallowed).
*
* @param array $items An array with the menu items to check
* @return bool
*/
protected function allItemsRestricted($items)
{
// Check if every provided item is restricted.

foreach ($items as $item) {
if ($this->isAllowed($item)) {
return false;
}
}

return true;
}
}
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
17 changes: 16 additions & 1 deletion tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ public function testCanOnWholeRestrictedSubmenu()

$builder->add(
[
'text' => 'Submenu',
'text' => 'Submenu1',
'submenu' => [
[
'text' => 'About',
Expand All @@ -863,6 +863,21 @@ public function testCanOnWholeRestrictedSubmenu()
'url' => '/',
'can' => 'show-home',
],
[
'text' => 'Submenu2',
'submenu' => [
[
'text' => 'Home1',
'url' => '/home1',
'can' => 'show-home',
],
[
'text' => 'Home2',
'url' => '/home2',
'can' => 'show-home',
],
],
],
],
]
);
Expand Down

0 comments on commit 1c05c92

Please sign in to comment.