From 70741932c67e3ff55db75a2bbd11a14317562f0a Mon Sep 17 00:00:00 2001 From: erikn69 Date: Tue, 14 Mar 2023 12:09:03 -0500 Subject: [PATCH 1/2] Register OctaneReloadPermissions listener for Laravel Octane --- config/permission.php | 7 +++++++ src/Listeners/OctaneReloadPermissions.php | 13 +++++++++++++ src/PermissionServiceProvider.php | 22 ++++++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/Listeners/OctaneReloadPermissions.php diff --git a/config/permission.php b/config/permission.php index 5b6e184c3..cf31e67dc 100644 --- a/config/permission.php +++ b/config/permission.php @@ -103,6 +103,13 @@ 'register_permission_check_method' => true, + /* + * When set to true, the Spatie\Permission\Listeners\OctaneReloadPermissions listener will be registered + * on the Laravel\Octane\Events\OperationTerminated event, this will refresh permissions on every + * TickTerminated, TaskTerminated and RequestTerminated + */ + 'register_octane_reset_listener' => false, + /* * When set to true the package implements teams using the 'team_foreign_key'. If you want * the migrations to register the 'team_foreign_key', you must set this to true diff --git a/src/Listeners/OctaneReloadPermissions.php b/src/Listeners/OctaneReloadPermissions.php new file mode 100644 index 000000000..6f4544e5e --- /dev/null +++ b/src/Listeners/OctaneReloadPermissions.php @@ -0,0 +1,13 @@ +sandbox->make(PermissionRegistrar::class)->clearPermissionsCollection(); + } +} diff --git a/src/PermissionServiceProvider.php b/src/PermissionServiceProvider.php index 49580a4de..f2643cef0 100644 --- a/src/PermissionServiceProvider.php +++ b/src/PermissionServiceProvider.php @@ -3,6 +3,7 @@ namespace Spatie\Permission; use Illuminate\Contracts\Auth\Access\Gate; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; use Illuminate\Filesystem\Filesystem; use Illuminate\Routing\Route; @@ -12,6 +13,7 @@ use Illuminate\View\Compilers\BladeCompiler; use Spatie\Permission\Contracts\Permission as PermissionContract; use Spatie\Permission\Contracts\Role as RoleContract; +use Spatie\Permission\Listeners\OctaneReloadPermissions; class PermissionServiceProvider extends ServiceProvider { @@ -25,6 +27,8 @@ public function boot() $this->registerModelBindings(); + $this->registerOctaneListener(); + $this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) { if ($this->app['config']->get('permission.register_permission_check_method')) { /** @var PermissionRegistrar $permissionLoader */ @@ -44,8 +48,7 @@ public function register() 'permission' ); - $this->callAfterResolving('blade.compiler', fn (BladeCompiler $bladeCompiler) => $this->registerBladeExtensions($bladeCompiler) - ); + $this->callAfterResolving('blade.compiler', fn (BladeCompiler $bladeCompiler) => $this->registerBladeExtensions($bladeCompiler)); } protected function offerPublishing(): void @@ -86,6 +89,21 @@ protected function registerCommands(): void ]); } + protected function registerOctaneListener(): void + { + if ($this->app->runningInConsole() || ! $this->app['config']->get('permission.register_octane_reset_listener')) { + return; + } + + if (! $this->app['config']->get('octane.listeners')) { + return; + } + + $dispatcher = $this->app[Dispatcher::class]; + // @phpstan-ignore-next-line + $dispatcher->listen(\Laravel\Octane\Events\OperationTerminated::class, OctaneReloadPermissions::class); + } + protected function registerModelBindings(): void { $this->app->bind(PermissionContract::class, fn ($app) => $app->make($app->config['permission.models.permission'])); From d39868082e3c907b51373010b459fdc2c7c187ad Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Wed, 16 Aug 2023 20:39:54 -0400 Subject: [PATCH 2/2] Add note to config file --- config/permission.php | 1 + 1 file changed, 1 insertion(+) diff --git a/config/permission.php b/config/permission.php index b69e749ee..a62d1cd69 100644 --- a/config/permission.php +++ b/config/permission.php @@ -107,6 +107,7 @@ * When set to true, the Spatie\Permission\Listeners\OctaneReloadPermissions listener will be registered * on the Laravel\Octane\Events\OperationTerminated event, this will refresh permissions on every * TickTerminated, TaskTerminated and RequestTerminated + * NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it. */ 'register_octane_reset_listener' => false,