filament icon indicating copy to clipboard operation
filament copied to clipboard

Unnecessary Policy Checks with Multi Tenancy

Open wannesmatthys opened this issue 1 year ago • 2 comments

Package

filament/filament

Package Version

v3.2.124

Laravel Version

v11.32.0

Livewire Version

v3.5.12

PHP Version

PHP 8.3.3

Problem description

When we have multiple teams, filament always does a check of the policies for every team instead of only for the current team. In large applications with lots of teams this can slow down the application a bit.

So for example, here I have a simple filament dashboard with only one Resource "Posts". If I have one Team, this is the output for the Gates in debugbar. image

When registering a new Team, it loads the policies (of the navigation?) multiple times.

With 2 teams: image

With 3 teams: image

And so on

Expected behavior

It should only load the correct policies once.

Steps to reproduce

Clone the repository, create a couple of teams and check the debugbar. Everytime you add a new team, the "Gates" will increase in debugbar.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/wannesmatthys/filament-gates-reproduction

Relevant log output

No response

wannesmatthys avatar Nov 18 '24 13:11 wannesmatthys

I'm debugging this further, and apparently to generate all the tenant URL's (see photo), it calls

$this->navigationManager = new NavigationManager;
$navigation = $this->navigationManager->get();

in the file HasRoutes.php (line 194). This loads all navigation items (and policies) for every tenant.

Would there be another way to generate this URL maybe?

image

wannesmatthys avatar Nov 18 '24 13:11 wannesmatthys

This becomes a problem when logged in user has access to many tenants. @wannesmatthys have you found a workaround for this?

Update: I started using https://github.com/InnoGE/laravel-policy-soft-cache and performance is now very good.

silviugd avatar Feb 24 '25 10:02 silviugd

https://github.com/filamentphp/filament/issues/13290#issuecomment-2993613462

danharrin avatar Jun 22 '25 16:06 danharrin