src/Controller/SecurityController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Dompdf\Dompdf;
  4. use Dompdf\Options;
  5. use Knp\Snappy\Pdf;
  6. use App\Form\LoginType;
  7. use App\Form\AccountType;
  8. use App\Service\GenererPDF;
  9. use spipu\html2pdf\HTML2PDF;
  10. use App\Entity\PasswordUpdate;
  11. use App\Service\MyOtherService;
  12. use App\Form\PasswordUpdateType;
  13. use App\Repository\UserRepository;
  14. use Symfony\Component\Form\FormError;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  20. use Nucleos\DompdfBundle\Wrapper\DompdfWrapperInterface;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  24. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  25. class SecurityController extends AbstractController
  26. {
  27.     /**
  28.      * @Route("/login", name="security_login")
  29.      */
  30.     public function login(AuthenticationUtils  $authenticationUtils): Response
  31.     {
  32.         $form $this->createForm(LoginType::class, [
  33.             'email' => $authenticationUtils->getLastUsername()
  34.         ]);
  35.         // $form = $factory->createNamed('', LoginType::class, [
  36.         //     '_username' => $utils->getLastUsername()
  37.         // ]);
  38.         $error $authenticationUtils->getLastAuthenticationError();
  39.         return $this->render('security/login.html.twig', [
  40.             'formView' => $form->createView(),
  41.             'error' => $error
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/logout", name="security_logout")
  46.      */
  47.     public function logout()
  48.     {
  49.     }
  50.     /**
  51.      * Permet d'afficher et de traiter le formulaire de modification de profil
  52.      * 
  53.      * @Route("/profile/edit", name="security_profile")
  54.      * @IsGranted("ROLE_USER")
  55.      * 
  56.      * @return Response
  57.      */
  58.     public function profile(Request $requestEntityManagerInterface $manager)
  59.     {
  60.         $user $this->getUser();
  61.         $accountForm $this->createForm(AccountType::class, $user);
  62.         $passwordUpdateForm $this->createForm(PasswordUpdateType::class);
  63.         $accountForm->handleRequest($request);
  64.         $passwordUpdateForm->handleRequest($request);
  65.         if ($accountForm->isSubmitted() && $accountForm->isValid()) {
  66.             $manager->persist($user);
  67.             $manager->flush();
  68.             $this->addFlash(
  69.                 'success',
  70.                 "Les données du profil ont été enregistrée avec succès !"
  71.             );
  72.             return $this->redirectToRoute('homepage');
  73.         }
  74.         if ($passwordUpdateForm->isSubmitted() && $passwordUpdateForm->isValid()) {
  75.             // $manager->persist();
  76.             // $manager->flush();
  77.             // return $this->redirectToRoute('homepage');
  78.         }
  79.         return $this->render('security/profile.html.twig', [
  80.             'user' => $user,
  81.             'etudiant' => $user->getEtudiant(),
  82.             'admin' => $user->getAdmin(),
  83.             'accountForm' => $accountForm->createView(),
  84.             'passwordUpdateForm' => $passwordUpdateForm->createView()
  85.         ]);
  86.     }
  87.     // /**
  88.     //  * Permet de modifier le mot de passe
  89.     //  * 
  90.     //  * @Route("/account/test", name="account_test")
  91.     //  * @IsGranted("ROLE_USER")
  92.     //  * 
  93.     //  * @return Response
  94.     //  */
  95.     // public function test()
  96.     // {
  97.     //     //return $this->render('facture/facture.html.twig', []);
  98.     //     $knpSnappyPdf = new Pdf('/usr/local/bin/wkhtmltopdf');
  99.     //     $html = $this->renderView('facture/facture.html.twig');
  100.     //     $header = $this->renderView('facture/_header.html.twig');
  101.     //     $footer = $this->renderView('facture/_footer.html.twig');
  102.     //     $options = [
  103.     //         'margin-top' => 30,
  104.     //         'margin-right' => 20,
  105.     //         'margin-bottom' => 40,
  106.     //         'margin-left' => 20,
  107.     //         'header-html' => $header,
  108.     //         'footer-html' => $footer
  109.     //     ];
  110.     //     header('Content-Type: application/pdf');
  111.     //     echo $knpSnappyPdf->getOutputFromHtml($html, $options);
  112.     // }
  113.     //     $passwordUpdate = new PasswordUpdate();
  114.     //     $user = $this->getUser();
  115.     //     $form = $this->createForm(PasswordUpdateType::class, $passwordUpdate);
  116.     //     $form->handleRequest($request);
  117.     //     if ($form->isSubmitted() && $form->isValid()) {
  118.     //         //Verifier que le old password est le bon
  119.     //         if (!password_verify($passwordUpdate->getOldPassword(), $user->getHash())) {
  120.     //             $form->get('oldPassword')->addError(new FormError("Le mot de passe saisie est différent du mot de passe actuel !"));
  121.     //         } else {
  122.     //             $newPassword = $passwordUpdate->getNewPassword();
  123.     //             $hash = $encoder->encodePassword($user, $newPassword);
  124.     //             $user->setHash($hash);
  125.     //             $manager->persist($user);
  126.     //             $manager->flush();
  127.     //             $this->addFlash(
  128.     //                 'success',
  129.     //                 "Votre mot de passe a bien été changé !"
  130.     //             );
  131.     //             return $this->redirectToRoute('home');
  132.     //         }
  133.     //     }
  134.     //     return $this->render('account/updatePassword.html.twig', [
  135.     //         'form' => $form->createView()
  136.     //     ]);
  137.     // }
  138.     /**
  139.      * Permet de modifier le mot de passe
  140.      * 
  141.      * @Route("/password-update", name="security_updatePassword")
  142.      * @IsGranted("ROLE_USER")
  143.      * 
  144.      * @return Response
  145.      */
  146.     public function updatePassword(Request $requestEntityManagerInterface $managerUserPasswordEncoderInterface $encoder)
  147.     {
  148.         $passwordUpdate = new PasswordUpdate();
  149.         $user $this->getUser();
  150.         $form $this->createForm(PasswordUpdateType::class, $passwordUpdate);
  151.         $form->handleRequest($request);
  152.         if ($form->isSubmitted() && $form->isValid()) {
  153.             //Verifier que le old password est le bon
  154.             if (!password_verify($passwordUpdate->getOldPassword(), $user->getPassword())) {
  155.                 $form->get('oldPassword')->addError(new FormError("Le mot de passe saisie est différent du mot de passe actuel !"));
  156.             } else {
  157.                 $newPassword $passwordUpdate->getNewPassword();
  158.                 $hash $encoder->encodePassword($user$newPassword);
  159.                 $user->setPassword($hash);
  160.                 $user->setNouveauMDP(false);
  161.                 $manager->persist($user);
  162.                 $manager->flush();
  163.                 $this->addFlash(
  164.                     'success',
  165.                     "Votre mot de passe a bien été changé !"
  166.                 );
  167.                 return $this->redirectToRoute('homepage');
  168.             }
  169.         }
  170.         return $this->render('account/updatePassword.html.twig', [
  171.             'form' => $form->createView()
  172.         ]);
  173.     }
  174.     /**
  175.      * @Route("/login_check", name="login_check")
  176.      */
  177.     public function check()
  178.     {
  179.         throw new \LogicException('This code should never be reached');
  180.     }
  181. }