403 - "This action is unauthorized."

Viewed 264

I have a Settings nova Resource and a SettingsPolicy like below for this resource. The main goal of the policy is to hide the create button when there is already one Setting. The issue is that after adding the policy after update the resource I get a 403 error:

exception: "Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException"
file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php"

message: "This action is unauthorized."
trace: [{file: "/var/www/html/vendor/laravel/

Policy:

class SettingsPolicy
{
    use HandlesAuthorization;


    public function create()
    {
        return Settings::count() === 0;
    }

    public function update()
    {
        return true;
    }

    public function delete()
    {
        return true;
    }
}

I tried to register the policy on the AuthServiceProvider but it seems that is not working, dd($this->registerPolicies()) shows always null. And it still shows the 403 error.

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        'App\Models\ Settings' => 'App\Policies\SettingsPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        dd($this->registerPolicies()); // shows always null
    }
}
0 Answers
Related