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

Fixed bug of loading user roles of different teams to current team #2803

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mohamedds-12
Copy link

@mohamedds-12 mohamedds-12 commented Feb 4, 2025

@drbyte
Copy link
Collaborator

drbyte commented Feb 4, 2025

It would be best to also add tests for the specific functionality you're trying to fix.

@parallels999
Copy link
Contributor

parallels999 commented Feb 4, 2025

You are adding extra queries,
That was developed to take advantage of the preloaded relationships when teams are not enabled, this would affect the performance of all of us who do not use teams

There are many notes in the documentation about using unsetRelation in teams, changing teams in the same session is not something that should happen, it is designed to maintain a single team

#1094 (comment)

@cesarreyes3
Copy link

cesarreyes3 commented Feb 4, 2025

You could just do attach directly or syncRoles

$user->roles()->attach($roleIdsArray, $theTeamId);

@mohamedds-12
Copy link
Author

mohamedds-12 commented Feb 7, 2025

@drbyte, @parallels999
I have changed the fix to Reloading user roles before role assigning only if teams feature is enabled to avoid running extra queries when not using teams, And I have changed the Tests accordingly. (I don't think we should worry too much about sql queries in this one cuz it's not like Role-Assignment happens frequently as for ex Role-Checking).

I think it's a logical thing to expect that the package would take into consideration teamSessionId getting changed on the fly as the functionality is supported therefore it should assert to reload user roles before role-assigning (of course only when teams are enabled) as for there is no reason for reloading when not using teams.

I think we should add this functionality as it is not a breaking change and it's been mentioned earlier
and it's an excepted behavior

erikn69 referenced this pull request in wakeup0706/laravel-permission Feb 13, 2025
if (app(PermissionRegistrar::class)->teams) {
$this->load('roles');
}

if ($model->exists) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the relationship be loaded only if the model already exists?

@drbyte
Copy link
Collaborator

drbyte commented Feb 13, 2025

I still think this needs some specific tests regarding the problem that's being addressed.

It's probable that the it_can_assign_same_and_different_roles_on_same_user_different_teams test doesn't cover this scenario, so another test that does cover the present problem would be beneficial.

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

Successfully merging this pull request may close these issues.

4 participants