src/Controller/ReglesController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use DateTime;
  4. use App\Entity\Regles;
  5. use App\Service\Securizer;
  6. use App\Library\GraphOutlook;
  7. use App\Repository\ReglesRepository;
  8. use App\Repository\ClientsRepository;
  9. use App\Repository\ContactsRepository;
  10. use App\Repository\ReglesVerificationRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  18. class ReglesController extends AbstractController
  19. {
  20.     /**
  21.      * @IsGranted("ROLE_TECH")
  22.      */
  23.     public function index(int $idReglesRepository $repoRegles): Response
  24.     {
  25.         $rules $repoRegles->findBy(['idClient' => $id]);
  26.         return $this->json($rules200, [], ['groups' => 'affichageRegles']);
  27.     }
  28.     /**
  29.      * @IsGranted("ROLE_TECH")
  30.      */
  31.     public function getByDate($dateReglesRepository $repoReglesReglesVerificationRepository $repoVerification): Response
  32.     {
  33.         $datetime = new DateTime(date('Y-m-d H:i:s'$date));
  34.         $rules $repoRegles->findAllActive();
  35.         // $res = [];
  36.         for ($i 0$i count($rules); $i++) {
  37.             $verifications $repoVerification->findByDateAndRuleID($datetime$rules[$i]->getId());
  38.             $rules[$i]->setReglesVerification($verifications);
  39.         }
  40.         return $this->json($rules200, [], ['groups' => 'affichageVerificationRegles']);
  41.     }
  42.     /**
  43.      * @IsGranted("ROLE_TECH")
  44.      */
  45.     public function creer(Request $requestContactsRepository $repoContactsSerializerInterface $serializerEntityManagerInterface $manager): Response
  46.     {
  47.         $jsonRecu $request->getContent();
  48.         $today = new DateTime();
  49.         $user $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()]);
  50.         try {
  51.             $rule $serializer->deserialize($jsonRecuRegles::class, 'json');
  52.             $rule->setCreatedBy($user);
  53.             $rule->setDateCreated($today);
  54.             $manager->persist($rule);
  55.             $manager->flush();
  56.         } catch (\Throwable $e) {
  57.             return $this->json([
  58.                 'status' => 400,
  59.                 'message' => $e->getMessage()
  60.             ], 400);
  61.         }
  62.         return $this->json($rule200, [], ['groups' => 'affichageRegles']);
  63.     }
  64.     /**
  65.      * @IsGranted("ROLE_TECH")
  66.      */
  67.     public function modification(int $idRequest $requestReglesRepository $repoReglesContactsRepository $repoContactsSerializerInterface $serializerEntityManagerInterface $managerAccessDecisionManagerInterface $accessDecisionManager): Response
  68.     {
  69.         //permets de modifier une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
  70.         $securizer = new Securizer($accessDecisionManager);
  71.         if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
  72.             $jsonRecu $request->getContent();
  73.             $rule $repoRegles->find($id);
  74.             if (!$rule) {
  75.                 return $this->json([
  76.                     'status' => 404,
  77.                     'message' => "Cette règle n'existe pas"
  78.                 ], 404);
  79.             }
  80.             try {
  81.                 $rule $serializer->deserialize($jsonRecuRegles::class, 'json', ['object_to_populate' => $rule]);
  82.                 $manager->persist($rule);
  83.                 $manager->flush();
  84.             } catch (\Throwable $e) {
  85.                 return $this->json([
  86.                     'status' => 400,
  87.                     'message' => $e->getMessage()
  88.                 ], 400);
  89.             }
  90.             return $this->json($rule200, [], ['groups' => 'affichageRegles']);
  91.         } else {
  92.             return $this->json([
  93.                 'status' => 400,
  94.                 'message' => "Vous ne pouvez pas consulter cet élément"
  95.             ], 400);
  96.         }
  97.     }
  98.     // /**
  99.     //  * @IsGranted("ROLE_TECH")
  100.     //  */
  101.     // public function delete(Int $id, ReglesRepository $repoRegles, ReglesVerificationRepository $repoVerification, EntityManagerInterface $manager, AccessDecisionManagerInterface $accessDecisionManager): Response
  102.     // {
  103.     //     //permets de effacer une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
  104.     //     $securizer = new Securizer($accessDecisionManager);
  105.     //     if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
  106.     //         $regle = $repoRegles->find($id);
  107.     //         $repoRegles->remove($regle, true);
  108.     //     } else {
  109.     //         return $this->json([
  110.     //             'status' => 400,
  111.     //             'message' => "Vous ne pouvez pas consulter cet élément"
  112.     //         ], 400);
  113.     //     }
  114.     //     return $this->json(true, 200, [], ['groups' => 'affichageRegles']);
  115.     // }
  116. }