Refactor new user function
This commit is contained in:
parent
9513067ac5
commit
f6a2159177
|
|
@ -7,7 +7,6 @@ parameters:
|
||||||
aws_url: '%env(AWS_ENDPOINT)%'
|
aws_url: '%env(AWS_ENDPOINT)%'
|
||||||
aws_public_url: '%env(AWS_ENDPOINT)%'
|
aws_public_url: '%env(AWS_ENDPOINT)%'
|
||||||
logos_directory: '%kernel.project_dir%/public/uploads/logos'
|
logos_directory: '%kernel.project_dir%/public/uploads/logos'
|
||||||
profile_directory: '%kernel.project_dir%/public/uploads/profile'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
|
|
@ -29,9 +28,6 @@ services:
|
||||||
App\Service\AwsService:
|
App\Service\AwsService:
|
||||||
arguments:
|
arguments:
|
||||||
$awsPublicUrl: '%aws_public_url%'
|
$awsPublicUrl: '%aws_public_url%'
|
||||||
App\Service\UserService:
|
|
||||||
arguments:
|
|
||||||
$profileDirectory: '%profile_directory%'
|
|
||||||
App\Service\OrganizationsService:
|
App\Service\OrganizationsService:
|
||||||
arguments:
|
arguments:
|
||||||
$logoDirectory: '%logos_directory%'
|
$logoDirectory: '%logos_directory%'
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Symfony\Component\Mailer\Mailer;
|
use Symfony\Component\Mailer\Mailer;
|
||||||
use Symfony\Component\Mailer\MailerInterface;
|
use Symfony\Component\Mailer\MailerInterface;
|
||||||
use Symfony\Component\Mime\Email;
|
use Symfony\Component\Mime\Email;
|
||||||
|
|
@ -217,45 +218,30 @@ class UserController extends AbstractController
|
||||||
$form = $this->createForm(UserForm::class, $user);
|
$form = $this->createForm(UserForm::class, $user);
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
$orgId = $request->get('organizationId');
|
$orgId = $request->get('organizationId');
|
||||||
|
if ($orgId){
|
||||||
|
$org = $this->organizationRepository->find($orgId) ?? throw new NotFoundHttpException(sprintf('%s not found', $orgId));
|
||||||
|
}
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$existingUser = $this->userRepository->findOneBy(['email' => $user->getEmail()]);
|
$existingUser = $this->userRepository->findOneBy(['email' => $user->getEmail()]);
|
||||||
if ($existingUser && $orgId) {
|
if ($existingUser && $orgId) {
|
||||||
$org = $this->organizationRepository->find($orgId);
|
$this->userService->handleExistingUser($existingUser, $org);
|
||||||
$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->actionService->createAction("Create new user", $existingUser, $org, "Added user to organization" . $existingUser->getUserIdentifier() . " for organization " . $org->getName());
|
$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->logger->notice("User added to organization " . $org->getName());
|
||||||
$this->emailService->sendExistingUserNotificationEmail($existingUser, $org);
|
$this->emailService->sendExistingUserNotificationEmail($existingUser, $org);
|
||||||
$this->logger->notice("Existing user notification email sent to " . $existingUser->getUserIdentifier());
|
$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');
|
$this->organizationsService->notifyOrganizationAdmins($data, 'USER_INVITED');
|
||||||
|
|
||||||
return $this->redirectToRoute('organization_show', ['id' => $orgId]);
|
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
|
// Handle file upload
|
||||||
$picture = $form->get('pictureUrl')->getData();
|
$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) {
|
if ($orgId) {
|
||||||
$org = $this->organizationRepository->find($orgId);
|
|
||||||
if ($org) {
|
|
||||||
$uo = new UsersOrganizations();
|
$uo = new UsersOrganizations();
|
||||||
$uo->setUsers($user);
|
$uo->setUsers($user);
|
||||||
$uo->setOrganization($org);
|
$uo->setOrganization($org);
|
||||||
|
|
@ -270,7 +256,6 @@ class UserController extends AbstractController
|
||||||
$data = ['user' => $uo->getUsers(), 'organization' => $uo->getOrganization()];
|
$data = ['user' => $uo->getUsers(), 'organization' => $uo->getOrganization()];
|
||||||
$this->organizationsService->notifyOrganizationAdmins($data, 'USER_INVITED');
|
$this->organizationsService->notifyOrganizationAdmins($data, 'USER_INVITED');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
$this->actionService->createAction("Create new user", $actingUser, null, $user->getUserIdentifier());
|
$this->actionService->createAction("Create new user", $actingUser, null, $user->getUserIdentifier());
|
||||||
$this->logger->notice("User created " . $user->getUserIdentifier());
|
$this->logger->notice("User created " . $user->getUserIdentifier());
|
||||||
$this->entityManager->persist($user);
|
$this->entityManager->persist($user);
|
||||||
|
|
|
||||||
|
|
@ -449,4 +449,33 @@ class UserService
|
||||||
$this->entityManager->persist($uo);
|
$this->entityManager->persist($uo);
|
||||||
$this->entityManager->flush();
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue