update spatie/laravel-permission integration guide to match 5.x

This commit is contained in:
Samuel Štancl 2023-11-21 02:24:26 +01:00 committed by GitHub
parent 92a56bd0e2
commit 49941b625d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,17 +34,40 @@ Next, add the following listeners to the `TenancyBootstrapped` and `TenancyEnde
```php ```php
Events\TenancyBootstrapped::class => [ Events\TenancyBootstrapped::class => [
function (Events\TenancyBootstrapped $event) { 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 => [ Events\TenancyEnded::class => [
function (Events\TenancyEnded $event) { 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. 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} ## **laravel-medialibrary** {#laravel-medialibrary}