F
Filamentβ€’15mo ago
usmcgator

Policy for Settings Plugin?

I tried creating a policy for the settings plugin, but it appears to have no effect. All other policies I have work as expected, except this one. This is how I generated the policy:
php artisan make:policy SettingsPolicy --model=Spatie\LaravelSettings\Settings
php artisan make:policy SettingsPolicy --model=Spatie\LaravelSettings\Settings
5 Replies
Dennis Koch
Dennis Kochβ€’15mo ago
Policies only work for Resources. For custom pages you need to use mount() and the method Leandro linked
usmcgator
usmcgatorβ€’15mo ago
I just implemented this code, which works to protect the page, but it appears to have caused a kink in the settings plugin. I can get to the settings page with the proper role, but it's no longer pulling up existing values from the database into the form. It writes the values to the database correctly, but going to the form after a successful save shows empty values in the form fields, yet the updated values are in the database. After uncommenting out the following code, it goes back to working correctly, except other users can get to it without this code:
protected static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasAnyRole('admin');
}

public function mount(): void
{
abort_unless(auth()->user()->hasAnyRole('admin'), 403);
}
protected static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasAnyRole('admin');
}

public function mount(): void
{
abort_unless(auth()->user()->hasAnyRole('admin'), 403);
}
Dennis Koch
Dennis Kochβ€’15mo ago
You overwrote mount() without calling the parent method πŸ˜‰
usmcgator
usmcgatorβ€’15mo ago
That solved the problem, thanks.