src/Controller/ContactController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use finfo;
  4. use App\Entity\Contacts;
  5. use App\Service\Securizer;
  6. use App\Repository\ClientsRepository;
  7. use App\Repository\ContactsRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Serializer\SerializerInterface;
  12. use Symfony\Component\Validator\Validator\ValidatorInterface;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  16. class ContactController extends AbstractController
  17. {
  18.     /**
  19.      * @IsGranted("ROLE_USER")
  20.      */
  21.     public function index(ContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  22.     {
  23.         //voir la liste de tous les contacts
  24.         $securizer = new Securizer($accessDecisionManager);
  25.         $contacts $repoContact->findContactTous($this->getUser(), $securizer);
  26.         return $this->json($contacts200, [], ['groups' => 'affichageContact']);
  27.     }
  28.     /**
  29.      * @IsGranted("ROLE_CLIENT")
  30.      */
  31.     public function actif(ContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  32.     {
  33.         //voir la liste des contacts actif
  34.         $securizer = new Securizer($accessDecisionManager);
  35.         $contacts $repoContact->findContactActif($this->getUser(), $securizer);
  36.         return $this->json($contacts200, [], ['groups' => 'affichageContact']);
  37.     }
  38.     /**
  39.      * @IsGranted("ROLE_TECH")
  40.      */
  41.     public function actifList(ContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  42.     {
  43.         //lister nom, prenom et id de contacts actif
  44.         $contacts $repoContact->findBy(["archive" => 0]);
  45.         return $this->json($contacts200, [], ['groups' => 'affichageContactList']);
  46.     }
  47.     /**
  48.      * @IsGranted("ROLE_CLIENT")
  49.      */
  50.     public function archiver(ContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  51.     {
  52.         //voir la liste des contacts archivé
  53.         $securizer = new Securizer($accessDecisionManager);
  54.         $contacts $repoContact->findContactArchiver($this->getUser(), $securizer);
  55.         return $this->json($contacts200, [], ['groups' => 'affichageContact']);
  56.     }
  57.     /**
  58.      * @IsGranted("ROLE_TECH")
  59.      */
  60.     public function tech(ContactsRepository $repoContact): Response
  61.     {
  62.         //voir la liste des contacts avec le role commercial
  63.         $contactsActif $repoContact->findBy(['archive' => false]);
  64.         $contacts = [];
  65.         foreach ($contactsActif as $contact) {
  66.             if (in_array("ROLE_TECH"$contact->getRoles()))
  67.                 array_push($contacts$contact);
  68.         }
  69.         return $this->json(
  70.             $contacts,
  71.             200,
  72.             ['Cache-Control' => 'max-age=86400'],
  73.             ['groups' => 'affichageContact']
  74.         );
  75.     }
  76.     /**
  77.      * @IsGranted("ROLE_TECH")
  78.      */
  79.     public function commercial(ContactsRepository $repoContact): Response
  80.     {
  81.         //voir la liste des contacts avec le role tech
  82.         $contactsActif $repoContact->findBy(['archive' => false]);
  83.         $contacts = [];
  84.         foreach ($contactsActif as $contact) {
  85.             if (in_array("ROLE_COMMERCIAL"$contact->getRoles()))
  86.                 array_push($contacts$contact);
  87.         }
  88.         return $this->json(
  89.             $contacts,
  90.             200,
  91.             ['Cache-Control' => 'max-age=86400'],
  92.             ['groups' => 'affichageContact']
  93.         );
  94.     }
  95.     /**
  96.      * @IsGranted("ROLE_USER")
  97.      */
  98.     public function avatarContact(Request $request): Response
  99.     {
  100.         //le dossier de destination du fichier telecharger
  101.         $destination $this->getParameter('kernel.project_dir') . '/public/img/avatar_contact';
  102.         //le fichier lelecharger
  103.         $img $request->files->get("myFile");
  104.         //tester le type de fichier envoyer par l'utilisateur
  105.         // test de l'extension, pas suffisant il faut tester le type MINE du fichier
  106.         //tableau des extention acceptées
  107.         $extensions = array('.png''.gif''.jpg''.jpeg');
  108.         //recuperation de l'extention du fichier
  109.         $extension strrchr($_FILES['myFile']['name'], '.');
  110.         //renvoie un json d'erreur si l'extension du fichier n'est pas dans le tableau
  111.         if (!in_array($extension$extensions)) {
  112.             return $this->json([
  113.                 'status' => 400,
  114.                 'message' => "Vous ne pouvez uploder que des fichiers de type .png, .gif, .jpeg, .jpg"
  115.             ], 400);
  116.         }
  117.         //test du type MINE
  118.         //tableau des type MINE accepter
  119.         $mineTypes = array('image/png''image/gif''image/jpg''image/jpeg');
  120.         //retourne le type MINE
  121.         $finfo = new finfo(FILEINFO_MIME_TYPENULL);
  122.         $fileMineType $finfo->file($_FILES['myFile']['tmp_name']);
  123.         //renvoie un json d'erreur si le MINE type n'est pas dans le tableau
  124.         if (!in_array($fileMineType$mineTypes)) {
  125.             return $this->json([
  126.                 'status' => 400,
  127.                 'message' => "Vous ne pouvez uploder que des fichiers de type .png, .gif, .jpeg, .jpg"
  128.             ], 400);
  129.         }
  130.         //renomage du fichier pour qu'il soit unique sur la serveur
  131.         $newFilename uniqid() . '-' $img->getClientOriginalName();
  132.         //enregistrement du fichier dans le dossier de destination
  133.         $img->move($destination$newFilename);
  134.         //json de retour pour envoi de l'url en base
  135.         return $this->json(
  136.             [
  137.                 'status' => 200,
  138.                 'url' => 'img' '/avatar_contact/' $newFilename
  139.             ],
  140.             200,
  141.             ['Cache-Control' => 'max-age=86400']
  142.         );
  143.     }
  144.     /**
  145.      * @IsGranted("ROLE_USER")
  146.      */
  147.     public function voir($idContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  148.     {
  149.         //voir les details d'un contact par id
  150.         $securizer = new Securizer($accessDecisionManager);
  151.         $contacts $repoContact->findContactTous($this->getUser(), $securizer);
  152.         //verifi que le contact que l'on cherche fait bien partie de la liste de ceux qu'il est possible d'afficher
  153.         if (in_array($repoContact->find($id), $contacts)) {
  154.             $contact $repoContact->find($id);
  155.         } else {
  156.             return $this->json([
  157.                 'status' => 400,
  158.                 'message' => "Vous ne pouvez pas consulter ce contact"
  159.             ], 400);
  160.         }
  161.         return $this->json($contact200, [], ['groups' => 'affichageContact']);
  162.     }
  163.     /**
  164.      * @IsGranted("ROLE_TECH")
  165.      */
  166.     public function searchContact($searchContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  167.     {
  168.         $securizer = new Securizer($accessDecisionManager);
  169.         $contacts $repoContact->globalSearch($search$this->getUser(), $securizer);
  170.         return $this->json($contacts200, [], ['groups' => 'affichageContact']);
  171.     }
  172.     /**
  173.      * @IsGranted("ROLE_CLIENT")
  174.      */
  175.     public function creer(
  176.         Request $request,
  177.         ValidatorInterface $validator,
  178.         SerializerInterface $serializer,
  179.         ContactsRepository $contactsRepo,
  180.         EntityManagerInterface $manager,
  181.         ClientsRepository $repoClient,
  182.         AccessDecisionManagerInterface $accessDecisionManager
  183.     ): Response {
  184.         //creer un nouveau contact, seul les role client ou superieur peuvent acceder
  185.         $securizer = new Securizer($accessDecisionManager);
  186.         $user $this->getUser();
  187.         $clients $repoClient->findClientActif($user$securizer);
  188.         $jsonRecu $request->getContent();
  189.         try {
  190.             $jsonRecu json_decode($jsonRecu);
  191.             if (!isset($jsonRecu->mail) || empty($jsonRecu->mail)) {
  192.                 $contacts $contactsRepo->findAll();
  193.                 $jsonRecu->mail 'nomail' count($contacts) . '@exist.fr';
  194.             }
  195.             $jsonRecu json_encode($jsonRecu);
  196.             //transforme le json reçu en entity
  197.             $contact $serializer->deserialize($jsonRecuContacts::class, 'json');
  198.             //interdit la creation d'un client avec role superieur au sien
  199.             $creationPossible true;
  200.             foreach ($contact->getRoles() as $role) {
  201.                 if (!$securizer->isGranted($user$role)) {
  202.                     $creationPossible false;
  203.                     break;
  204.                 }
  205.             }
  206.             //verifi que le role que l'on souhaite donné au nouveau contact est bien inferieur a celui de l'utilisateur
  207.             if ($creationPossible) {
  208.                 //verifi que le client au quel on souhaite lier le nouveau contact fait bien partie de la liste de ceux qu'il est possible d'afficher
  209.                 if (in_array($contact->getIdClient(), $clients)) {
  210.                     $contact->setRoles(["ROLE_USER"])->setPassword('12345');
  211.                     $errors $validator->validate($contact);
  212.                     if (count($errors) > 0) {
  213.                         return $this->json($errors400);
  214.                     }
  215.                     $manager->persist($contact);
  216.                     $manager->flush();
  217.                 } else {
  218.                     return $this->json([
  219.                         'status' => 400,
  220.                         'message' => "Vous ne pouvez pas lier un contact à ce client"
  221.                     ], 400);
  222.                 }
  223.             } else {
  224.                 return $this->json([
  225.                     'status' => 400,
  226.                     'message' => "Vous ne pouvez pas donner ce rôle"
  227.                 ], 400);
  228.             }
  229.         } catch (\throwable $e) {
  230.             return $this->json([
  231.                 'status' => 400,
  232.                 'message' => $e->getMessage()
  233.             ], 400);
  234.         }
  235.         return $this->json($contact201, [], ['groups' => 'affichageContact']);
  236.     }
  237.     /**
  238.      * @IsGranted("ROLE_CLIENT")
  239.      */
  240.     public function modif($idRequest $requestContactsRepository $repoContactValidatorInterface $validatorSerializerInterface $serializerEntityManagerInterface $managerClientsRepository $repoClientAccessDecisionManagerInterface $accessDecisionManager): Response
  241.     {
  242.         //modifi un contact existant, seul les role client ou superieur peuvent acceder a cette page
  243.         $securizer = new Securizer($accessDecisionManager);
  244.         $user $this->getUser();
  245.         $clients $repoClient->findClientTous($this->getUser(), $securizer);
  246.         $contacts $repoContact->findContactTous($this->getUser(), $securizer);
  247.         //verifi que le contact que l'on cherche fait bien partie de la liste de ceux qu'il est possible d'afficher
  248.         if (in_array($repoContact->find($id), $contacts)) {
  249.             $contact $repoContact->find($id);
  250.         } else {
  251.             return $this->json([
  252.                 //retourne un json avec message d'erreur si l'on a pas le droit d'afficher ou si l'id n'existe pas
  253.                 'status' => 400,
  254.                 'message' => "Vous ne pouvez pas acceder a ce contact"
  255.             ], 400);
  256.         }
  257.         $jsonRecu $request->getContent();
  258.         try {
  259.             //transforme le json reçu en entity
  260.             $serializer->deserialize($jsonRecuContacts::class, 'json', ['object_to_populate' => $contact]);
  261.             //interdit la creation d'un client avec role superieur au sien
  262.             $creationPossible true;
  263.             foreach ($contact->getRoles() as $role) {
  264.                 if (!$securizer->isGranted($user$role)) {
  265.                     $creationPossible false;
  266.                     break;
  267.                 }
  268.             }
  269.             //verifi que le role que l'on souhaite donné au nouveau contact est bien inferieur a celui de l'utilisateur
  270.             if ($creationPossible) {
  271.                 //verifi que le client au quel on souhaite lier le nouveau contact fait bien partie de la liste de ceux qu'il est possible d'afficher
  272.                 if (in_array($contact->getIdClient(), $clients)) {
  273.                     $errors $validator->validate($contact);
  274.                     if (count($errors) > 0) {
  275.                         return $this->json($errors400);
  276.                     }
  277.                     $manager->persist($contact);
  278.                     $manager->flush();
  279.                 } else {
  280.                     return $this->json([
  281.                         'status' => 400,
  282.                         'message' => "Vous ne pouvez pas lier un contact à ce client"
  283.                     ], 400);
  284.                 }
  285.             } else {
  286.                 return $this->json([
  287.                     'status' => 400,
  288.                     'message' => "Vous ne pouvez pas donner ce rôle"
  289.                 ], 400);
  290.             }
  291.         } catch (\throwable $e) {
  292.             return $this->json([
  293.                 'status' => 400,
  294.                 'message' => $e->getMessage()
  295.             ], 400);
  296.         }
  297.         return $this->json($contact201, [], ['groups' => 'affichageContact']);
  298.     }
  299.     /**
  300.      * @IsGranted("ROLE_USER")
  301.      */
  302.     public function getContactsByClient($idClientContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManager): Response
  303.     {
  304.         $contacts $repoContact->findBy(['idClient' => $idClient]);
  305.         $securizer = new Securizer($accessDecisionManager);
  306.         // Si le utilisateur n'est pas ROLE_TECH retourne la liste seulement si il apartient au client
  307.         if (!$securizer->isGranted($this->getUser(), "ROLE_TECH")) {
  308.             $contact $repoContact->findOneBy(["mail" => $this->getUser()->getUserIdentifier()]);
  309.             if ($contact->getIdClient()->getId() != $idClient)
  310.                 return $this->json([
  311.                     'status' => 400,
  312.                     'message' => "Vous ne pouvez pas consulter ce contact"
  313.                 ], 400);
  314.         }
  315.         return $this->json($contacts200, [], ['groups' => 'affichageContact']);
  316.     }
  317. }