diff --git a/src/Card/NovaDashboard.php b/src/Card/NovaDashboard.php index 265f930..90c00c6 100644 --- a/src/Card/NovaDashboard.php +++ b/src/Card/NovaDashboard.php @@ -6,6 +6,7 @@ use Closure; use Laravel\Nova\Card; +use Laravel\Nova\Http\Requests\NovaRequest; class NovaDashboard extends Card { @@ -28,6 +29,10 @@ public function addView(?string $name, Closure|View $view): static $views = data_get($this->meta, 'views', []); $views[] = value($view, View::make($name)); - return $this->withMeta([ 'views' => $views ]); + return $this->withMeta([ + 'views' => collect($views) + ->filter(fn (View $view) => $view->authorizedToSee(resolve(NovaRequest::class))) + ->values(), + ]); } } diff --git a/src/Card/View.php b/src/Card/View.php index 5479969..77df424 100644 --- a/src/Card/View.php +++ b/src/Card/View.php @@ -7,7 +7,9 @@ use DigitalCreative\NovaDashboard\Traits\ResolveView; use Illuminate\Support\Collection; use JsonSerializable; +use Laravel\Nova\AuthorizedToSee; use Laravel\Nova\Filters\Filter; +use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Makeable; use Laravel\Nova\Metable; @@ -16,6 +18,7 @@ class View implements JsonSerializable use Makeable; use Metable; use ResolveView; + use AuthorizedToSee; public function __construct( private readonly string $name, @@ -31,7 +34,11 @@ public function addWidget(Widget ...$widgets): self $metaWidgets[] = $widget; } - return $this->withMeta([ 'widgets' => $metaWidgets ]); + return $this->withMeta([ + 'widgets' => collect($metaWidgets) + ->filter(fn (Widget $widget) => $widget->authorizedToSee(resolve(NovaRequest::class))) + ->values(), + ]); } public function addWidgets(array $widgets): self @@ -47,7 +54,11 @@ public function addFilter(Filter ...$filters): self $metaFilters[] = $filter; } - return $this->withMeta([ 'filters' => $metaFilters ]); + return $this->withMeta([ + 'filters' => collect($metaFilters) + ->filter(fn (Filter $filter) => $filter->authorizedToSee(resolve(NovaRequest::class))) + ->values(), + ]); } public function addFilters(array $filters): self