Skip to content

Commit

Permalink
Add authorization to views, widgets and filters (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski authored Feb 5, 2024
1 parent 29c4b93 commit 03b1ee3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Card/NovaDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Laravel\Nova\Card;
use Laravel\Nova\Http\Requests\NovaRequest;

class NovaDashboard extends Card
{
Expand All @@ -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(),
]);
}
}
15 changes: 13 additions & 2 deletions src/Card/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,6 +18,7 @@ class View implements JsonSerializable
use Makeable;
use Metable;
use ResolveView;
use AuthorizedToSee;

public function __construct(
private readonly string $name,
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 03b1ee3

Please sign in to comment.