0

In NovaServiceProvider there is:

protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                '[email protected]',
            ]);
        });
    }

But what I would like to do is only allow people from the admins guard that I've setup in config/auth to access Nova. All users from the web guard should ideally get a 404 when they access any Nova URL.

This question for Telescope seems to be similar, but I can't seem to figure out where I should define this, and how to generate a 404 for the web guard.

A question that is probably related: what does viewNova in the gate method actually mean?

  • Can I define that specific action for a specific guard in config/auth? (I think I've seen this somewhere but can't seem to find it)?
  • There doesn't seem to be a Policy written for Nova?

1 Answer 1

2

Checkout vendor/laravel/nova/src/NovaApplicationServiceProvider.php. It has a method called authorization:

/**
 * Configure the Nova authorization services.
 *
 * @return void
 */
protected function authorization()
{
    $this->gate();

    Nova::auth(function ($request) {
        return app()->environment('local') ||
               Gate::check('viewNova', [$request->user()]);
    });
}

If the environment was local, it allows everyone to access the panel, but if the environment was something else, it checks for the definition on viewNova method and it passes the $request->user() to it.

In the same file, there's gate() method which defined viewNova:

/**
 * 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 in_array($user->email, [
            //
        ]);
    });
}

Basically, this method does nothing. You can implement it in app/Providers/NovaServiceProvider.php (which is the default implementation you see in the file and you've mentioned). In your case, you could implement it this way:

/**
 * 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) {
        Auth::guard('admin')->check();
    });
}

It returns true if the currently authenticated user is in admin guard. Hope I could answer all your questions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.