laravel / pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.
https://pulse.laravel.com
MIT License
1.43k stars 165 forks source link

Gate is overwritten by package #397

Closed niekdemelker closed 2 months ago

niekdemelker commented 2 months ago

Pulse Version

v1.2.4

Laravel Version

11.15.0

PHP Version

8.2.4

Livewire Version

v3.5.2

Database Driver & Version

No response

Description

I used the Gate::define('viewPulse', fn() => ...) to define who can see Pulse. Because I use Laravel Nova and i need to check this permission rendering the menu i define this gate in de booting state of the app service provider.

Now I noticed the gate will not work in production because it is overwritten by the Pulse Service Provider in the function registerAuthorization().

A solution is checking if the ability already exists, like in this example

    protected function registerAuthorization(): void
    {
        $this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
            if ($gate->has('viewPulse')) {
                return;
            }
            $gate->define('viewPulse', fn ($user = null) => $app->environment('local'));
        });
    }

Steps To Reproduce

define the gate in the $this->app->booting(fn () => ...)); in the app service provider. It will be overwritten.

timacdonald commented 2 months ago

@niekdemelker, you should not be defining the gate in the booting callback. You should be doing it inline within the boot method as documented.

Screenshot 2024-07-22 at 09 28 04