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 } %}
diff --git a/templates/user/new.html.twig b/templates/user/new.html.twig index 2ddb27f..f4b58d4 100644 --- a/templates/user/new.html.twig +++ b/templates/user/new.html.twig @@ -12,6 +12,11 @@
{{ form_start(form) }} {{ form_widget(form) }} + {% if organizationId is defined %} +
+ +
+ {% endif %} {{ form_end(form) }}
diff --git a/templates/user/userListSmall.html.twig b/templates/user/userListSmall.html.twig index 1a6ce77..7a30d9f 100644 --- a/templates/user/userListSmall.html.twig +++ b/templates/user/userListSmall.html.twig @@ -3,6 +3,9 @@

{{ title }}

+ {% if organizationId is defined %} + Ajouter un utilisateur + {% endif %}