Add instruction to reset cache key on TenancyEnded

This commit is contained in:
lukinovec 2022-09-01 16:35:25 +02:00 committed by GitHub
parent 196ebaa5b6
commit 938775e6fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -26,19 +26,16 @@ php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvid
mv database/migrations/*_create_permission_tables.php database/migrations/tenant mv database/migrations/*_create_permission_tables.php database/migrations/tenant
``` ```
Then add this to your `AppServiceProvider::boot()` method: Then add this to your `TenancyServiceProvider::boot()`:
```php ```php
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) { Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
// TenancyBootstraped event is called if you are creating tenants through the central app,
// so to prevent making caches with wrong permissions the code below can be used
//
// $isACentralDomain = in_array(request()->getHost(), config('tenancy.central_domains'), true);
// if(!$isACentralDomain) {
// \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id;
// }
\Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id; \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id;
}); });
Event::listen(TenancyEnded::class, function (TenancyEnded $event) {
\Spatie\Permission\PermissionRegistrar::$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. 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.