Refactor new user function

This commit is contained in:
Charles 2025-11-25 16:02:10 +01:00
parent 9513067ac5
commit f6a2159177
3 changed files with 37 additions and 27 deletions

View File

@ -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%'

View File

@ -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());

View File

@ -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);
}
}
}