Add user to organization

This commit is contained in:
Charles 2025-08-05 11:16:45 +02:00
parent 6efbeb0fa2
commit 1053a2ab22
4 changed files with 38 additions and 5 deletions

View File

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

View File

@ -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
} %}
</div>
<div class="col mb-3 mb-sm-0">

View File

@ -12,6 +12,11 @@
<form method="post" action="{{ path('user_new') }}" enctype="multipart/form-data">
{{ form_start(form) }}
{{ form_widget(form) }}
{% if organizationId is defined %}
<div class="form-group">
<input hidden type="text" value="{{ organizationId }}" name="organization_id">
</div>
{% endif %}
<button type="submit" class="btn btn-primary">Enregistrer</button>
{{ form_end(form) }}
</form>

View File

@ -3,6 +3,9 @@
<div class="card border-0">
<div class="card-title p-3 d-flex justify-content-between align-items-center ">
<h3>{{ title }}</h3>
{% if organizationId is defined %}
<a href="{{ path('user_new', {'organizationId': organizationId}) }}" class="btn btn-primary">Ajouter un utilisateur</a>
{% endif %}
</div>
<div class="card-body">
<table class="table align-middle table-borderless">