spatie/laravel-permission

Too much Role-Models loaded

renky opened this issue · 3 comments

Describe the bug
I'm using laravel-permissions in combination with laravel-multitenancy. Therefor i use the teams-functionality of laravel-permissions to receive tenant-roles. This works in general like a charm.

But it seems like laravel-permissions always loads ALL roles everytime. Independent if a team is set or not. This is "polluting" the RAM because each of my tenants has the same kind of roles - maybe with different permissions - and with every new tenant, the loaded models are increased.

this can be easily reproduced by just using teams, and creating roles per teams and finally using for example laravel-debugbar to see how many role-models are loaded. In my case I'd expect only a hand full (every tenant has about 5-10 roles) - but debugbar shows me over 1000 loaded role-models:
image

I already debugged into it and came to the conclusion that it is indeed originated in the laravel-permission-package. Originated in PermissionRegistrar, method getPermissionsWithRoles. And the reason is, because Permission-Model's roles-relation doesn't care about team-ids - see Permission-Model line 58 in latest version.

What I'm wondering about is: would it break other things if the knowledge about teams would be added there too? I just opened a PR for this change, and think it doesn't break anything, but it reduces loaded role-models significantly.
#2601

Versions
You can use composer show to get the version numbers of:

  • spatie/laravel-permission package version: 6.3.0
  • laravel/framework package 10.42.0

PHP version: 8.2

Database version: mariadb 10

this can be easily reproduced by just using teams, and creating roles per teams and finally using for example laravel-debugbar to see how many role-models are loaded

I did try laravel-debugbar, and I couldn't reproduce the problem, It only loads the roles of the logged user-team

image

Maybe you are not using cache feature
Also, instead of using so many roles per team, you should use global roles.
#2444