Edit user
This commit is contained in:
parent
e360019e58
commit
f24fb0180d
|
|
@ -4,6 +4,7 @@ namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Entity\UsersOrganizations;
|
use App\Entity\UsersOrganizations;
|
||||||
|
use App\Form\EditUserForm;
|
||||||
use App\Form\NewUserForm;
|
use App\Form\NewUserForm;
|
||||||
use App\Service\UserOrganizationService;
|
use App\Service\UserOrganizationService;
|
||||||
use App\Service\UserService;
|
use App\Service\UserService;
|
||||||
|
|
@ -99,19 +100,36 @@ class UserController extends AbstractController
|
||||||
/**
|
/**
|
||||||
* GET /user/{id}/edit - Show form to edit user
|
* GET /user/{id}/edit - Show form to edit user
|
||||||
*/
|
*/
|
||||||
#[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET'])]
|
#[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'POST'])]
|
||||||
public function edit(int $id, EntityManagerInterface $entityManager): Response
|
public function edit(int $id, EntityManagerInterface $entityManager, Request $request): Response
|
||||||
{
|
{
|
||||||
|
//Handle access control
|
||||||
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
||||||
throw $this->createAccessDeniedException('Access denied');
|
throw $this->createAccessDeniedException('Access denied');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Fetch user by ID and handle not found case
|
||||||
$user = $entityManager->getRepository(User::class)->find($id);
|
$user = $entityManager->getRepository(User::class)->find($id);
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Create form for editing user
|
||||||
|
$form = $this->createForm(EditUserForm::class, $user);
|
||||||
|
|
||||||
|
//Handle form submission
|
||||||
|
$form->handleRequest($request);
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
//Persist changes to the user entity
|
||||||
|
$entityManager->persist($user);
|
||||||
|
$entityManager->flush();
|
||||||
|
|
||||||
|
//Redirect to user profile after successful edit
|
||||||
|
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->render('user/edit.html.twig', [
|
return $this->render('user/edit.html.twig', [
|
||||||
|
'form' => $form->createView(),
|
||||||
'user' => $user,
|
'user' => $user,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Form;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||||
|
|
||||||
|
class EditUserForm extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->add('name', TextType::class, [
|
||||||
|
'label' => 'Prénom*',
|
||||||
|
'required' => true,
|
||||||
|
])
|
||||||
|
->add('surname', TextType::class, [
|
||||||
|
'label' => 'Nom*',
|
||||||
|
'required' => true,
|
||||||
|
])
|
||||||
|
->add('email', EmailType::class, [
|
||||||
|
'label' => 'Email*',
|
||||||
|
'required' => true,
|
||||||
|
])
|
||||||
|
->add('phoneNumber', TextType::class, [
|
||||||
|
'label' => 'Numéro de téléphone',
|
||||||
|
'required' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver): void
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'data_class' => \App\Entity\User::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-title shadow-sm p-3">
|
<div class="card-title shadow-sm p-3 d-flex justify-content-between align-items-center">
|
||||||
<h2>{{ user.surname|capitalize }} {{ user.name|capitalize }}</h2>
|
<h2>{{ user.surname|capitalize }} {{ user.name|capitalize }}</h2>
|
||||||
|
<a href="{{ path('user_edit', {'id': user.id}) }}" class="btn btn-primary">Modifier</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<p> <b>Email: </b>{{ user.email }}</p>
|
<p> <b>Email: </b>{{ user.email }}</p>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="w-100 h-100 p-5 m-auto">
|
||||||
|
<h1>Modifier l'utilisateur</h1>
|
||||||
|
<form method="post" action="{{ path('user_edit', {'id': user.id}) }}" enctype="multipart/form-data">
|
||||||
|
{{ form_start(form) }}
|
||||||
|
{{ form_widget(form) }}
|
||||||
|
<button type="submit" class="btn btn-primary">Enregistrer</button>
|
||||||
|
{{ form_end(form) }}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
Loading…
Reference in New Issue