Skip to content

Commit

Permalink
Improve Gate Filter
Browse files Browse the repository at this point in the history
The Gate filter now restrict submenu items with all restircted children
  • Loading branch information
dfsmania committed Mar 14, 2024
1 parent 21b94f6 commit 3d69412
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Menu/Filters/GateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;

use Illuminate\Contracts\Auth\Access\Gate;
use JeroenNoten\LaravelAdminLte\Helpers\MenuItemHelper;

class GateFilter implements FilterInterface
{
Expand Down Expand Up @@ -35,7 +36,10 @@ public function transform($item)
// Set a special attribute when item is not allowed. Items with this
// attribute will be filtered out of the menu.

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

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

Expand Down Expand Up @@ -68,4 +72,23 @@ 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;
}
}
72 changes: 72 additions & 0 deletions tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,78 @@ 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' => 'Submenu',
'submenu' => [
[
'text' => 'About',
'url' => 'about',
'can' => 'show-about',
],
[
'text' => 'Home',
'url' => '/',
'can' => 'show-home',
]
],
]
);

$this->assertCount(0, $builder->menu);
}

public function testLangTranslate()
{
$builder = $this->makeMenuBuilder('http://example.com');
Expand Down

0 comments on commit 3d69412

Please sign in to comment.