Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| UserController | |
100.00% |
24 / 24 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| listAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createAction | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| editAction | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| deleteAction | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Controller; |
| 4 | |
| 5 | use App\Entity\User; |
| 6 | use App\Form\UserType; |
| 7 | use App\Repository\UserRepository; |
| 8 | use Doctrine\ORM\EntityManagerInterface; |
| 9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
| 10 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 11 | use Symfony\Component\HttpFoundation\Request; |
| 12 | use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
| 13 | use Symfony\Component\Routing\Annotation\Route; |
| 14 | |
| 15 | #[IsGranted("ROLE_ADMIN")] |
| 16 | class UserController extends AbstractController |
| 17 | { |
| 18 | #[Route("/admin/users", name:"app_user_list")] |
| 19 | public function listAction(UserRepository $userRepository) |
| 20 | { |
| 21 | return $this->render('user/list.html.twig', ['users' => $userRepository->findAll()]); |
| 22 | } |
| 23 | |
| 24 | #[Route("/admin/users/create", name:"app_user_create")] |
| 25 | public function createAction(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $em) |
| 26 | { |
| 27 | $user = new User(); |
| 28 | $form = $this->createForm(UserType::class, $user); |
| 29 | $form->handleRequest($request); |
| 30 | |
| 31 | if ($form->isSubmitted() && $form->isValid()) { |
| 32 | $user->setPassword($userPasswordHasher->hashPassword($user, $form->get('password')->getData())); |
| 33 | |
| 34 | $em->persist($user); |
| 35 | $em->flush(); |
| 36 | |
| 37 | $this->addFlash('success', sprintf('L\'utilisateur <strong>%s</strong> a bien été ajouté.', $user->getUserIdentifier())); |
| 38 | |
| 39 | return $this->redirectToRoute('app_user_list'); |
| 40 | } |
| 41 | |
| 42 | return $this->render('user/create.html.twig', ['form' => $form->createView()]); |
| 43 | } |
| 44 | |
| 45 | #[Route("/admin/users/{id}/edit", name:"app_user_edit")] |
| 46 | public function editAction(User $user, Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $em) |
| 47 | { |
| 48 | $form = $this->createForm(UserType::class, $user); |
| 49 | $form->handleRequest($request); |
| 50 | |
| 51 | if ($form->isSubmitted() && $form->isValid()) { |
| 52 | $user->setPassword($userPasswordHasher->hashPassword($user, $form->get('password')->getData())); |
| 53 | |
| 54 | $em->persist($user); |
| 55 | $em->flush(); |
| 56 | |
| 57 | $this->addFlash('success', sprintf('L\'utilisateur <strong>%s</strong> a bien été modifié.', $user->getUserIdentifier())); |
| 58 | |
| 59 | return $this->redirectToRoute('app_user_list'); |
| 60 | } |
| 61 | |
| 62 | return $this->render('user/edit.html.twig', ['form' => $form->createView(), 'user' => $user]); |
| 63 | } |
| 64 | |
| 65 | #[Route("/admin/users/{id}/delete", name:"app_user_delete")] |
| 66 | public function deleteAction(User $user, EntityManagerInterface $em) |
| 67 | { |
| 68 | $em->remove($user); |
| 69 | $em->flush(); |
| 70 | |
| 71 | $this->addFlash('success', sprintf('L\'utilisateur <strong>%s</strong> a bien été supprimé.', $user->getUserIdentifier())); |
| 72 | |
| 73 | return $this->redirectToRoute('app_user_list'); |
| 74 | } |
| 75 | } |