Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
50 / 50
UserController
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
13
100.00% covered (success)
100.00%
50 / 50
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
 listAction
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
11 / 11
 createAction
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
11 / 11
 editAction
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
14 / 14
 deleteAction
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
// Voir Doc Symfony : https://symfony.com/doc/current/components/security/authorization.html
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Entity\Usertodo;
use App\Repository\UsertodoRepository;
use App\Form\UsertodoType;
use App\Handler\PagingHandler;
// use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Security\Voter\UserVoter;
class UserController extends AbstractController
{
    /**
     * @var Security
     */
    private $security;
    /**
     * @var EntityManagerInterface
     */
    private $manager;
    /**
     * @var AuthorizationCheckerInterface
     */
    private $authorization;
    /**
     * @var UserPasswordEncoderInterface
     */
    private $encoder;
    /**
     * @var UsertodoRepository
     */
    private $usertodoRepo;
    /**
     * @var PagingHandler
     */
    private $pagingHandler;
    public function __construct(
        Security $security, 
        EntityManagerInterface $manager,
        AuthorizationCheckerInterface $authorization,
        UserPasswordEncoderInterface $encoder,
        UsertodoRepository $usertodoRepo, 
        PagingHandler $pagingHandler
        )
    {
        $this->security = $security;
        $this->manager = $manager;
        $this->authorization = $authorization;
        $this->encoder = $encoder;
        $this->usertodoRepo = $usertodoRepo;
        $this->pagingHandler = $pagingHandler;
    }
    /**
     * @Route("/users", name="user_list", methods={"GET"})
     * @return Response
     */
    public function listAction(Request $request): Response
    {
        // PAGINATION
        $pageValues = $this->pagingHandler->handle($request);
        if (!$pageValues[0] || !$pageValues[1]) {
            $start = 0;
            $limit = 10;
        } else {
            $start = (int) strip_tags($pageValues[0]);
            $limit = (int) strip_tags($pageValues[1]);
        }
        // RÉVISION DE L'APPEL DU REPOSITORY
        $users = $this->usertodoRepo->findBy(array(), array('id' => 'DESC'));
        return $this->render('user/list.html.twig', [
            'limit' => $limit,
            'start' => $start,
            'users' => $users
            // DÉSACTIVATION DE CETTE MÉTHODE DU PROJET DE DÉPART :
            // 'users' => $this->getDoctrine()->getRepository('App:Usertodo')->findAll(),
        ]);
    }
    /**
     * @Route("/users/create", name="user_create", methods={"GET","POST"})
     * @return Response
     */
    public function createAction(Request $request): Response
    {
        $user = new Usertodo();
        $form = $this->createForm(UsertodoType::class, $user);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            // $em = $this->getDoctrine()->getManager();
            // RÉVISON DU PASSWORD ENCODER
            $password = $this->encoder->encodePassword($user, $user->getPassword());
            $user->setPassword($password);
            // $em->persist($user);
            // $em->flush();
            // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface
            $this->manager->persist($user);
            $this->manager->flush();
            $this->addFlash('success', "L'utilisateur a bien été ajouté.");
            return $this->redirectToRoute('user_list');
        }
        return $this->render('user/create.html.twig', ['form' => $form->createView()]);
    }
    /**
     * @Route("/users/{id}/edit", name="user_edit", requirements={"id": "\d+"}, methods={"GET","POST"})
     * @return Response
     */
    public function editAction(Usertodo $user, Request $request): Response
    {
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR OU UN MANAGER DE MODIFIER SON PROPRE COMPTE
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER LE COMPTE ANONYME
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER UN COMPTE SUPER ADMIN
        /* if (($this->security->getUser()->getId() === $user->getId()) || 
            (!$this->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_ANONYMOUS') || 
            (!$this->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_SUPER_ADMIN'))
        {
            // throw $this->createNotFoundException('Access Denied.');
            $this->addFlash('error', 'Vous ne pouvez pas modifier ce compte !');
            return $this->redirectToRoute('user_list');
        } */
        // $this->denyAccessUnlessGranted('update', $user);
        if (!$this->authorization->isGranted(UserVoter::UPDATE, $user)) {
            $this->addFlash('error', 'Vous ne pouvez pas modifier ce compte !');
            return $this->redirectToRoute('user_list');
        }
        
        $form = $this->createForm(UsertodoType::class, $user);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            // $em = $this->getDoctrine()->getManager();
            $password = $this->encoder->encodePassword($user, $user->getPassword());
            $user->setPassword($password);
            // AJOUTÉ POUR LA DATE DE MISE À JOUR
            $user->setFreshDate(new \Datetime());
            // $em->persist($user);
            // $em->flush();
            // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface
            $this->manager->persist($user);
            $this->manager->flush();
            $this->addFlash('success', "L'utilisateur a bien été modifié");
            return $this->redirectToRoute('user_list');
        }
        return $this->render('user/edit.html.twig', ['form' => $form->createView(), 'user' => $user]);
    }
    /**
     * @Route("/users/{id}/delete", name="user_delete", requirements={"id": "\d+"}, methods={"GET", "DELETE"})
     * @return RedirectResponse
     */
    public function deleteAction(Usertodo $user): RedirectResponse
    {
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR OU UN MANAGER DE SUPPRIMER SON PROPRE COMPTE
        
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE SUPPRIMER LE COMPTE ANONYME
       
        // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE SUPPRIMER UN COMPTE SUPER ADMIN
        
        /* if (($this->security->getUser()->getId() === $user->getId()) || 
            (!$this->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_ANONYMOUS') || 
            (!$this->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_SUPER_ADMIN'))
        {
            // throw $this->createNotFoundException('Access Denied.');
            $this->addFlash('error', 'Vous ne pouvez pas supprimer ce compte !');
            return $this->redirectToRoute('user_list');
        } */
        // $this->denyAccessUnlessGranted('delete', $user);
        if (!$this->authorization->isGranted(UserVoter::DELETE, $user)) {
            $this->addFlash('error', 'Vous ne pouvez pas supprimer ce compte !');
            return $this->redirectToRoute('user_list');
        }
        // $em = $this->getDoctrine()->getManager();
        
        // $em->remove($user);
        // $em->flush();
        // AJOUTÉ POUR SUPPRIMER DES UTILISATEURS
        // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface
        $this->manager->remove($user);
            
        $this->manager->flush();
        $this->addFlash('success', 'L\'utilisateur a bien été supprimé.');
        return $this->redirectToRoute('user_list');
    }
}