From f6a2159177cf8eaff47ea34876382f12096b808e Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 25 Nov 2025 16:02:10 +0100 Subject: [PATCH] Refactor new user function --- config/services.yaml | 4 ---- src/Controller/UserController.php | 31 ++++++++----------------------- src/Service/UserService.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index a4b0530..0c757b9 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -7,7 +7,6 @@ parameters: aws_url: '%env(AWS_ENDPOINT)%' aws_public_url: '%env(AWS_ENDPOINT)%' logos_directory: '%kernel.project_dir%/public/uploads/logos' - profile_directory: '%kernel.project_dir%/public/uploads/profile' services: # default configuration for services in *this* file @@ -29,9 +28,6 @@ services: App\Service\AwsService: arguments: $awsPublicUrl: '%aws_public_url%' - App\Service\UserService: - arguments: - $profileDirectory: '%profile_directory%' App\Service\OrganizationsService: arguments: $logoDirectory: '%logos_directory%' diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index ccbf65e..5e01860 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -27,6 +27,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; @@ -217,45 +218,30 @@ class UserController extends AbstractController $form = $this->createForm(UserForm::class, $user); $form->handleRequest($request); $orgId = $request->get('organizationId'); + if ($orgId){ + $org = $this->organizationRepository->find($orgId) ?? throw new NotFoundHttpException(sprintf('%s not found', $orgId)); + } if ($form->isSubmitted() && $form->isValid()) { $existingUser = $this->userRepository->findOneBy(['email' => $user->getEmail()]); if ($existingUser && $orgId) { - $org = $this->organizationRepository->find($orgId); - $uo = new UsersOrganizations(); - $uo->setUsers($existingUser); - $uo->setOrganization($org); - $uo->setStatut("INVITED"); - $uo->setIsActive(false); - $uo->setModifiedAt(new \DateTimeImmutable('now')); - $this->entityManager->persist($uo); - $this->entityManager->flush(); + $this->userService->handleExistingUser($existingUser, $org); + $this->actionService->createAction("Create new user", $existingUser, $org, "Added user to organization" . $existingUser->getUserIdentifier() . " for organization " . $org->getName()); $this->logger->notice("User added to organization " . $org->getName()); $this->emailService->sendExistingUserNotificationEmail($existingUser, $org); $this->logger->notice("Existing user notification email sent to " . $existingUser->getUserIdentifier()); - $data = ['user' => $uo->getUsers(), 'organization' => $uo->getOrganization()]; + $data = ['user' => $existingUser, 'organization' => $org]; $this->organizationsService->notifyOrganizationAdmins($data, 'USER_INVITED'); - return $this->redirectToRoute('organization_show', ['id' => $orgId]); } -// capitalize name and surname - $user->setName(ucfirst(strtolower($user->getName()))); - $user->setSurname(ucfirst(strtolower($user->getSurname()))); // Handle file upload $picture = $form->get('pictureUrl')->getData(); + $this->userService->formatNewUserData($user, $picture); - if ($picture) { - $this->userService->handleProfilePicture($user, $picture); - } - - //FOR TEST PURPOSES, SETTING A DEFAULT RANDOM PASSWORD - $user->setPassword($this->userService->generateRandomPassword()); if ($orgId) { - $org = $this->organizationRepository->find($orgId); - if ($org) { $uo = new UsersOrganizations(); $uo->setUsers($user); $uo->setOrganization($org); @@ -269,7 +255,6 @@ class UserController extends AbstractController $this->logger->notice("Password setup email sent to " . $user->getUserIdentifier()); $data = ['user' => $uo->getUsers(), 'organization' => $uo->getOrganization()]; $this->organizationsService->notifyOrganizationAdmins($data, 'USER_INVITED'); - } } $this->actionService->createAction("Create new user", $actingUser, null, $user->getUserIdentifier()); $this->logger->notice("User created " . $user->getUserIdentifier()); diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 77498a8..0a91c1c 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -449,4 +449,33 @@ class UserService $this->entityManager->persist($uo); $this->entityManager->flush(); } + + /** + * Format new user data. + * Capitalize name and surname. + * Trim strings. + * Set random password + * Handle picture if provided + * + * @param User $user + * @return void + */ + public function formatNewUserData(User $user, $picture): void + { + // capitalize name and surname + $user->setName(ucfirst(strtolower($user->getName()))); + $user->setSurname(ucfirst(strtolower($user->getSurname()))); + + // trim strings + $user->setName(trim($user->getName())); + $user->setSurname(trim($user->getSurname())); + $user->setEmail(trim($user->getEmail())); + + //FOR SETTING A DEFAULT RANDOM PASSWORD OF 50 CHARACTERS until user set his own password + $user->setPassword($this->generateRandomPassword()); + + if($picture) { + $this->handleProfilePicture($user, $picture); + } + } }