Repository: spatie/laravel-permission (php)

Issue:
Bug when assigning role to user that has same role in a different team
### Bug Description :
This happens when the user has role for ex: `admin` in team `A` and want to assign it same role but in team `B`.
What happens is that the user doesnt get assigned that role `admin` in that team `B` cuz in the treat that the method `assignRole()` sits in, the the `$this->roles` relation property is called to get user's roles and run `array_diff` on those currentRoles and the new Roles to assign (in this case **admin** role), the problem is that if the roles are already loaded on the user object before the teamsId was changed, then the `$this->roles` would return the role `admin` cuz the user has it in the other team not this one, thus the array_diff would return an empty array thus no roles would be assigned, 

#### HasRoles trait, assignRole method :

https://github.com/spatie/laravel-permission/blob/9d4fa2987f9b745854a13e1b36ba68e0b6dd4bdb/src/Traits/HasRoles.php#L154-L159



### Fix Suggestion :
We should instead of calling `$this->roles` which could load the already loaded roles, we should use `$this->roles()` which reload the roles with taking into consideration the new team_id that's set on the session thus no roles interference would happen.

Pull request:
Fixed bug of loading user roles of different teams to current team
Fixes : 
- #2802 

Patch:
diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php
index 149303a..d10dd77 100644
--- a/src/Traits/HasRoles.php
+++ b/src/Traits/HasRoles.php
@@ -154,6 +154,11 @@ trait HasRoles
             [app(PermissionRegistrar::class)->teamsKey => getPermissionsTeamId()] : [];
 
         if ($model->exists) {
+            if (app(PermissionRegistrar::class)->teams) {
+                // explicit reload in case team has been changed since last load
+                $this->load('roles');
+            }
+
             $currentRoles = $this->roles->map(fn ($role) => $role->getKey())->toArray();
 
             $this->roles()->attach(array_diff($roles, $currentRoles), $teamPivot);


Test patch:
diff --git a/tests/HasRolesTest.php b/tests/HasRolesTest.php
index 40a7193..696fe85 100644
--- a/tests/HasRolesTest.php
+++ b/tests/HasRolesTest.php
@@ -13,6 +13,7 @@ use Spatie\Permission\Events\RoleAttached;
 use Spatie\Permission\Events\RoleDetached;
 use Spatie\Permission\Exceptions\GuardDoesNotMatch;
 use Spatie\Permission\Exceptions\RoleDoesNotExist;
+use Spatie\Permission\PermissionRegistrar;
 use Spatie\Permission\Tests\TestModels\Admin;
 use Spatie\Permission\Tests\TestModels\SoftDeletingUser;
 use Spatie\Permission\Tests\TestModels\User;
@@ -394,6 +395,11 @@ class HasRolesTest extends TestCase
         DB::disableQueryLog();
 
         $necessaryQueriesCount = 2;
+      
+        // Teams reloads relation, adding an extra query
+        if (app(PermissionRegistrar::class)->teams) {
+            $necessaryQueriesCount++;
+        }
 
         $this->assertCount($necessaryQueriesCount, DB::getQueryLog());
     }
diff --git a/tests/TeamHasRolesTest.php b/tests/TeamHasRolesTest.php
index 928ed74..9f76da4 100644
--- a/tests/TeamHasRolesTest.php
+++ b/tests/TeamHasRolesTest.php
@@ -55,6 +55,11 @@ class TeamHasRolesTest extends HasRolesTest
         setPermissionsTeamId(1);
         $this->testUser->assignRole('testRole', 'testRole2');
 
+        // explicit load of roles to assert no mismatch
+        // when same role assigned in diff teams
+        // while old team's roles are loaded 
+        $this->testUser->load('roles');
+
         setPermissionsTeamId(2);
         $this->testUser->assignRole('testRole', 'testRole3');