Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
       });
    }
like image 439
Adnan Avatar asked Sep 11 '18 21:09

Adnan


People also ask

How much does laravel Nova cost?

However, you should keep in mind that Nova is not a low code solution – building an up and running Laravel admin panel requires the knowledge of Laravel, PHP, and composer. Nova is not an open-source solution either. The pricing starts at $99 per one-time purchase license for one developer working on one project.

Is laravel Nova free?

A license for a solo developer with annual revenue less than $20k is $99/site, and a pro license is $199/site. Head over to nova.laravel.com to register and purchase a license.

How does laravel Nova work?

Laravel Nova is a beautiful administration dashboard for Laravel applications. The primary feature of Nova is the ability to administer your underlying database records using Eloquent. Nova accomplishes this by allowing you to define a Nova "resource" that corresponds to each Eloquent model in your application.


1 Answers

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

like image 84
Chin Leung Avatar answered Nov 15 '22 06:11

Chin Leung