ninewiki/src/Controller/UserController.php
2024-11-26 20:43:55 +01:00

111 lines
3.6 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
class UserController extends AbstractController
{
#[Route('/admin/user', name: 'app_admin_user')]
public function list(UserRepository $userRepository): Response
{
$users = $userRepository->findAll();
return $this->render('user/list.html.twig', [
'usemenu' => true,
'usesidebar' => true,
'title' => 'Liste des Utilisateurs',
'routesubmit' => 'app_admin_user_submit',
'routeupdate' => 'app_admin_user_update',
'users' => $users,
]);
}
#[Route('/admin/user/submit', name: 'app_admin_user_submit')]
public function submit(Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user, ['mode' => 'submit']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$hashedPassword = $passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('app_admin_user');
}
return $this->render('user/edit.html.twig', [
'usemenu' => true,
'usesidebar' => true,
'title' => 'Création Utilisateur',
'routecancel' => 'app_admin_user',
'routedelete' => 'app_admin_user_delete',
'mode' => 'submit',
'form' => $form,
]);
}
#[Route('/admin/user/update/{id}', name: 'app_admin_user_update')]
public function update(int $id, Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
{
$user = $em->getRepository(User::class)->find($id);
if (!$user) {
return $this->redirectToRoute('app_admin_user');
}
$form = $this->createForm(UserType::class, $user, ['mode' => 'update']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$hashedPassword = $passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('app_admin_user');
}
return $this->render('user/edit.html.twig', [
'usemenu' => true,
'usesidebar' => true,
'title' => 'Création Utilisateur',
'routecancel' => 'app_admin_user',
'routedelete' => 'app_admin_user_delete',
'mode' => 'update',
'form' => $form,
]);
}
#[Route('/admin/user/delete', name: 'app_admin_user_delete')]
public function delete(): Response
{
return $this->redirectToRoute('app_admin_user');
}
#[Route('/user', name: 'app_user_profil')]
public function profil(): Response
{
return $this->redirectToRoute('app_home');
}
}