Adnan
Adnan

Reputation: 3347

Enabling certain roles to access Laravel Nova dashboard?

I have the Spatie Permissions package installed, and I have created policies to restrict access for my models using this package.

However, I'm struggling a bit on the creating a gate to enable certain roles such as 'Admin' and 'Content Editor' to access the Nova dashboard?

I assume it would involve the gate() function in the NovaServiceProvider. Here is what i tried.

   protected function gate()
    {
        Gate::define('viewNova', function ($user) {
             if ($user->hasRole('Admin') || $user->hasRole('Content Editor'))
    {
        return true;
    }
       });
    }

Upvotes: 7

Views: 7914

Answers (1)

Chin Leung
Chin Leung

Reputation: 14921

You can achieve what you want like this:

/**
 * Register the Nova gate.
 *
 * This gate determines who can access Nova in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewNova', function ($user) {
        return $user->hasAnyRole(['Admin', 'Content Editor']);
    });
}

More information from the documentation on authorization for access to Nova: https://nova.laravel.com/docs/1.0/installation.html#authorizing-nova

Upvotes: 7

Related Questions