diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index e86d7c2..819c664 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -872,6 +872,9 @@ class UserController extends AbstractController $email = $user->getEmail(); $existingUser = $this->userRepository->findOneBy(['email' => $email]); + if($this->userService->checkUserOrganizationLinkExists($existingUser, $org)){ + return $this->json(['error' => "L'utilisateur existe déjà dans votre organisation"], 400); + } // CASE A: User exists -> Add to org if ($existingUser) { // Check if already in org to avoid logic errors or duplicate logs diff --git a/src/Service/UserService.php b/src/Service/UserService.php index a40dc47..2c981e8 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -750,4 +750,21 @@ class UserService $this->entityManager->flush(); } + /* + * Check if the user organization link doesn't already exist + * Return true if the link exists, false otherwise. + * + * @param User $user + * @param Organization $organization + * @return bool + * */ + public function checkUserOrganizationLinkExists(User $user, Organizations $organization): bool + { + $existingLink = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy([ + 'users' => $user, + 'organization' => $organization + ]); + + return $existingLink !== null; + } }