Skip to content

Commit

Permalink
Register OctaneReloadPermissions listener for Laravel Octane
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Apr 26, 2023
1 parent ec9079a commit 7074193
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Listeners/OctaneReloadPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Permission\Listeners;

use Spatie\Permission\PermissionRegistrar;

class OctaneReloadPermissions
{
public function handle($event): void
{
$event->sandbox->make(PermissionRegistrar::class)->clearPermissionsCollection();
}
}
22 changes: 20 additions & 2 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -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']));
Expand Down

0 comments on commit 7074193

Please sign in to comment.