Skip to content

Commit

Permalink
Merge pull request #1886 from pboivin/fix/3x-legacy-nav-items
Browse files Browse the repository at this point in the history
[3.x] fix: Hide nav item if user is not authorized (legacy config)
  • Loading branch information
haringsrob authored Nov 7, 2022
2 parents 5e9664b + e87f2e5 commit 42c1808
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/TwillNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ private function transformLegacyToNew(string $key, array $legacy): NavigationLin
$link = NavigationLink::make()->forModule($key);
}

if ($link && ($legacy['title'] ?? false)) {
if ($legacy['title'] ?? false) {
$link->title($legacy['title']);
}

if ($legacy['can'] ?? false) {
$link->onlyWhen(fn () => (bool) Auth::user()?->can($legacy['can']));
}

return $link;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/integration/Navigation/LegacyNavigationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace A17\Twill\Tests\Integration\Navigation;

use A17\Twill\Facades\TwillNavigation;
use A17\Twill\Tests\Integration\TestCase;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Auth;

class LegacyNavigationTest extends TestCase
{
public function testNavItemIsNotVisibleIfNotAuthorized(): void
{
config()->set('twill-navigation', [
'pages' => [
'module' => true,
'can' => 'see-nav-item',
]
]);

$this->login();

$navigation = TwillNavigation::buildNavigationTree();

$this->assertFalse(Auth::user()->can('see-nav-item'));
$this->assertEmpty($navigation['left']);
}

public function testNavItemIsVisibleIfAuthorized(): void
{
config()->set('twill-navigation', [
'pages' => [
'module' => true,
'can' => 'see-nav-item',
]
]);

Gate::define('see-nav-item', function () {
return true;
});

$this->login();

$navigation = TwillNavigation::buildNavigationTree();

$this->assertTrue(Auth::user()->can('see-nav-item'));
$this->assertNotEmpty($navigation['left']);
$this->assertEquals('Pages', $navigation['left'][0]->getTitle());
}
}

0 comments on commit 42c1808

Please sign in to comment.