Skip to content

Commit feb3014

Browse files
erikn69drbyte
andauthored
[V6] Register OctaneReloadPermissions listener for Laravel Octane (#2403)
* Register OctaneReloadPermissions listener for Laravel Octane --------- Co-authored-by: Chris Brown <[email protected]>
1 parent 0c2422b commit feb3014

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

config/permission.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@
103103

104104
'register_permission_check_method' => true,
105105

106+
/*
107+
* When set to true, the Spatie\Permission\Listeners\OctaneReloadPermissions listener will be registered
108+
* on the Laravel\Octane\Events\OperationTerminated event, this will refresh permissions on every
109+
* TickTerminated, TaskTerminated and RequestTerminated
110+
* NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it.
111+
*/
112+
'register_octane_reset_listener' => false,
113+
106114
/*
107115
* Teams Feature.
108116
* When set to true the package implements teams using the 'team_foreign_key'.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\Permission\Listeners;
4+
5+
use Spatie\Permission\PermissionRegistrar;
6+
7+
class OctaneReloadPermissions
8+
{
9+
public function handle($event): void
10+
{
11+
$event->sandbox->make(PermissionRegistrar::class)->clearPermissionsCollection();
12+
}
13+
}

src/PermissionServiceProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\Permission;
44

55
use Illuminate\Contracts\Auth\Access\Gate;
6+
use Illuminate\Contracts\Events\Dispatcher;
67
use Illuminate\Contracts\Foundation\Application;
78
use Illuminate\Filesystem\Filesystem;
89
use Illuminate\Routing\Route;
@@ -12,6 +13,7 @@
1213
use Illuminate\View\Compilers\BladeCompiler;
1314
use Spatie\Permission\Contracts\Permission as PermissionContract;
1415
use Spatie\Permission\Contracts\Role as RoleContract;
16+
use Spatie\Permission\Listeners\OctaneReloadPermissions;
1517

1618
class PermissionServiceProvider extends ServiceProvider
1719
{
@@ -25,6 +27,8 @@ public function boot()
2527

2628
$this->registerModelBindings();
2729

30+
$this->registerOctaneListener();
31+
2832
$this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
2933
if ($this->app['config']->get('permission.register_permission_check_method')) {
3034
/** @var PermissionRegistrar $permissionLoader */
@@ -85,6 +89,21 @@ protected function registerCommands(): void
8589
]);
8690
}
8791

92+
protected function registerOctaneListener(): void
93+
{
94+
if ($this->app->runningInConsole() || ! $this->app['config']->get('permission.register_octane_reset_listener')) {
95+
return;
96+
}
97+
98+
if (! $this->app['config']->get('octane.listeners')) {
99+
return;
100+
}
101+
102+
$dispatcher = $this->app[Dispatcher::class];
103+
// @phpstan-ignore-next-line
104+
$dispatcher->listen(\Laravel\Octane\Events\OperationTerminated::class, OctaneReloadPermissions::class);
105+
}
106+
88107
protected function registerModelBindings(): void
89108
{
90109
$this->app->bind(PermissionContract::class, fn ($app) => $app->make($app->config['permission.models.permission']));

0 commit comments

Comments
 (0)