From 49941b625d0dc06fb37426f7c0a931b8e349b487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 21 Nov 2023 02:24:26 +0100 Subject: [PATCH] update spatie/laravel-permission integration guide to match 5.x --- source/docs/v3/integrations/spatie.blade.md | 27 +++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/source/docs/v3/integrations/spatie.blade.md b/source/docs/v3/integrations/spatie.blade.md index 28a595a..a0dd1c6 100644 --- a/source/docs/v3/integrations/spatie.blade.md +++ b/source/docs/v3/integrations/spatie.blade.md @@ -34,17 +34,40 @@ Next, add the following listeners to the `TenancyBootstrapped` and `TenancyEnde ```php Events\TenancyBootstrapped::class => [ function (Events\TenancyBootstrapped $event) { - \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id; + $permissionRegistrar = app(\Spatie\Permission\PermissionRegistrar::class); + $permissionRegistrar->cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->getTenantKey(); } ], Events\TenancyEnded::class => [ function (Events\TenancyEnded $event) { - \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache'; + $permissionRegistrar = app(\Spatie\Permission\PermissionRegistrar::class); + $permissionRegistrar->cacheKey = 'spatie.permission.cache'; } ], ``` +Alternatively, create a bootstrapper: + +```php +class SpatiePermissionsBootstrapper implements TenancyBootstrapper +{ + public function __construct( + protected PermissionRegistrar $registrar, + ) {} + + public function bootstrap(Tenant $tenant): void + { + $this->registrar->cacheKey = 'spatie.permission.cache.tenant.' . $tenant->getTenantKey(); + } + + public function revert(): void + { + $this->registrar->cacheKey = 'spatie.permission.cache'; + } +} +``` + The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant. We also need to reset the cache key when tenancy ends so that the tenant's cache key isn't used in the central app. ## **laravel-medialibrary** {#laravel-medialibrary}