diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 0760464..c3bc337 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Actions; use App\Entity\Apps; +use App\Entity\Organizations; use App\Entity\Roles; use App\Entity\User; use App\Form\UserForm; @@ -88,6 +89,7 @@ class UserController extends AbstractController public function new(Request $request): Response { $form = $this->createForm(UserForm::class); + $organizationId = $request->query->get('organizationId'); $form->handleRequest($request); @@ -96,17 +98,38 @@ class UserController extends AbstractController $data = $form->getData(); // Handle user creation logic here + //FOR DEV PURPOSES ONLY $data->setPictureUrl(""); $data->setPassword($this->userService->generateRandomPassword()); //FOR DEV PURPOSES ONLY + $orgId = $request->get('organization_id'); + if ($orgId) { + $organization = $this->entityManager->getRepository(Organizations::class)->find($orgId); + $roleUser = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'USER']); + if (!$organization || !$roleUser) { + throw $this->createNotFoundException(self::NOT_FOUND); + } + $uo = new UsersOrganizations(); + $uo->setOrganization($organization); + $uo->setRole($roleUser); + $uo->setUsers($data); + //log the action + $action = new Actions(); + $action->setActionType('Création utilisateur dans une organisation'); + $action->setUsers($this->getUser()); + $action->setOrganization($organization); + $this->entityManager->persist($uo); + }else{ + $action = new Actions(); + $action->setActionType('Création utilisateur'); + $action->setUsers($this->getUser()); + } $this->entityManager->persist($data); - - $action = new Actions(); - $action->setActionType('Création utilisateur'); - $action->setUsers($this->getUser()); $this->entityManager->persist($action); + + $this->entityManager->flush(); // Redirect to user index @@ -115,6 +138,7 @@ class UserController extends AbstractController return $this->render('user/new.html.twig', [ 'form' => $form->createView(), + 'organizationId' => $organizationId, ]); } diff --git a/templates/organization/show.html.twig b/templates/organization/show.html.twig index 107d47f..1945cb0 100644 --- a/templates/organization/show.html.twig +++ b/templates/organization/show.html.twig @@ -16,7 +16,8 @@ {% include 'user/userListSmall.html.twig' with { title: 'Nouveaux utilisateurs', users: newUsers, - empty_message: 'Aucun nouvel utilisateur trouvé.' + empty_message: 'Aucun nouvel utilisateur trouvé.', + organizationId: organization.id } %}