Adnan
Adnan

Reputation: 3347

How to create a Laravel Nova Gate/Policy to restrict access to Nova tools?

How can I create a policy/gate to restrict users from accessing Nova tools (e.g. Spatie Nova Backup Tool)?

Upvotes: 2

Views: 2618

Answers (2)

Fredrik
Fredrik

Reputation: 3293

Doing this the "Nova way", would be using the canSee()-method. You can read about it here.

public function tools()
{
    return [
        YourToolThatMightBeHidden::make()
            ->canSee(function ($request) {
                return $request->user()->can('access-this-tool');
            }),
    ];
}

Upvotes: 5

Fawzan
Fawzan

Reputation: 4849

I had the same issue and I resolved it like this.

  1. Go to NovaServiceProvider
  2. Add a gate/check in the tools() method

    public function tools()
    
    {
    
        if (Auth::user()->hasAnyRole(['admin'])) {
            return [new Foo, new Bar];
        }
    
        return [];
    }
    

This will solve the issue but I am not sure wether this is the Nova way of doing it or not.

Upvotes: 1

Related Questions