Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too much Role-Models loaded #2602

Closed
renky opened this issue Feb 6, 2024 · 3 comments
Closed

Too much Role-Models loaded #2602

renky opened this issue Feb 6, 2024 · 3 comments

Comments

@renky
Copy link

renky commented Feb 6, 2024

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

@parallels999
Copy link
Contributor

parallels999 commented Feb 6, 2024

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

@hsmfawaz
Copy link

Same issue with the TenancyForLaravel package

image
image

@renky
Copy link
Author

renky commented Mar 18, 2024

Just as a follow up:
in my case indeed it was our own fault:
Everytime when Switching a tenant, we run an action "MakeTenantCurrent" and inside of this we called the
`app()->make(PermissionRegistrar::class)->forgetCachedPermissions();``
finally this was the reason why it always loaded all permissions.

Due to our setup I'm not sure how to proceed with that, because this indeed has it's usecase: users can be part of multiple tenants in our setup and are allowed to switch between their tenants, and finally we allow all tenants to define their own roles - many of them use the same setup but in the end they differ in small permission-sets. We would somehow have to cache the permissions per tenant and user...
For now we stick with the current situation postponing it to a time, when our amount of tenants raise...

If somebody has a good solution for such a setup - you're welcome.

@renky renky closed this as completed Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants