From d1d2848d49a6bcbcdf9049322f36d3a708ffdd2f Mon Sep 17 00:00:00 2001 From: Shishir <75600200+shishir-intelli@users.noreply.github.com> Date: Wed, 31 May 2023 11:50:14 +0530 Subject: [PATCH] Fix issue which removes users from all team (#840) --- .../src/TeamMembershipManager.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/modules/apigee_edge_teams/src/TeamMembershipManager.php b/modules/apigee_edge_teams/src/TeamMembershipManager.php index d451536ab..fa716f189 100644 --- a/modules/apigee_edge_teams/src/TeamMembershipManager.php +++ b/modules/apigee_edge_teams/src/TeamMembershipManager.php @@ -142,18 +142,22 @@ public function removeMembers(string $team, array $developers): void { $controller->removeMember($developer); // Remove team member's roles from Drupal. if (array_key_exists($developer, $users_by_mail)) { - /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface[] $team_member_roles_in_teams */ - $team_member_roles_in_teams = $team_member_role_storage->loadByDeveloper($users_by_mail[$developer]); - foreach ($team_member_roles_in_teams as $team_member_roles_in_team) { - try { + /** @var \Drupal\user\Entity\User $account */ + $account = user_load_by_mail($users_by_mail[$developer]->getEmail()); + $team_entity = $this->entityTypeManager->getStorage('team')->load($team); + + /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface[] $team_member_roles_in_team */ + $team_member_roles_in_team = $team_member_role_storage->loadByDeveloperAndTeam($account, $team_entity); + try { + if (!empty($team_member_roles_in_team)) { $team_member_roles_in_team->delete(); } - catch (EntityStorageException $e) { - $this->logger->critical("Failed to remove %developer team member's roles in %team team with its membership.", [ - '%developer' => $developer, - '%team' => $team_member_roles_in_team->getTeam()->id(), - ]); - } + } + catch (EntityStorageException $e) { + $this->logger->critical("Failed to remove %developer team member's roles in %team team with its membership.", [ + '%developer' => $developer, + '%team' => $team_member_roles_in_team->getTeam()->id(), + ]); } } }