F
Filamentβ€’13mo ago
Geoff.

multitenacy combined with tenancyforlaravel and filament shield permission problem

Hello all, I have a a problem with the permissions of filament shield. When i run the shield:generate on the admin app all is working for the admin. But then when I run the shield:generate for the tenant (different database) with tenants:run … the permissions are ok for the tenant but not anymore for the admin. I think it has something todo with the caching of the permissions. So two quesions: Is there a way to disable the caching of the permission for testing purpose? Is there a way to cache the permissions for a panel? I hope someone knows the answer 😬 Thanks already
Solution:
1. To disable cache in dev mode set the CACHE_DRIVER=array in .env file or set the cache store to array from permission config file. learn more https://spatie.be/docs/laravel-permission/v5/advanced-usage/cache 2. since you are using that package you need to add the following in TenancyServiceProvider ```php Events\TenancyBootstrapped::class => [ function (Events\TenancyBootstrapped $event) {...
Jump to solution
5 Replies
Solution
Bezhan
Bezhanβ€’13mo ago
1. To disable cache in dev mode set the CACHE_DRIVER=array in .env file or set the cache store to array from permission config file. learn more https://spatie.be/docs/laravel-permission/v5/advanced-usage/cache 2. since you are using that package you need to add the following in TenancyServiceProvider
Events\TenancyBootstrapped::class => [
function (Events\TenancyBootstrapped $event) {
\Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id;
}
],

Events\TenancyEnded::class => [
function (Events\TenancyEnded $event) {
\Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache';
}
],
Events\TenancyBootstrapped::class => [
function (Events\TenancyBootstrapped $event) {
\Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id;
}
],

Events\TenancyEnded::class => [
function (Events\TenancyEnded $event) {
\Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache';
}
],
learn more https://tenancyforlaravel.com/docs/v3/integrations/spatie
Geoff.
Geoff.OPβ€’13mo ago
Thanks man will give it a try and let you know @Bezhan first tests seems to be okay 😍 . I think you made my day for now hehe Will test tomorrow better and let you know Thanks already
Bezhan
Bezhanβ€’13mo ago
Glad it could help, just mark it as solved if that was all you needed. 🍻
Geoff.
Geoff.OPβ€’13mo ago
Will do it for sure, gonna start testing different scenarios right now πŸ™‚ Seems to be okay Thanks man
Geoff.
Geoff.OPβ€’12mo ago
With the changes to laravel-permission 6 some things have changed. You need to create a Bootstrapper to change the cacheKey.
<?php

namespace App\Bootstrappers;

use Spatie\Permission\PermissionRegistrar;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

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';
}
}
<?php

namespace App\Bootstrappers;

use Spatie\Permission\PermissionRegistrar;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

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';
}
}
and add the bootstrapper to tenancy.php
'bootstrappers' => [
...
\App\Bootstrappers\SpatiePermissionsBootstrapper::class,
],
'bootstrappers' => [
...
\App\Bootstrappers\SpatiePermissionsBootstrapper::class,
],
Want results from more Discord servers?
Add your server