src/Controller/FrontController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Service\SJira;
  6. use App\Service\SFonction;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  17. class FrontController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="front_index")
  21.      */
  22.     public function index()
  23.     {
  24.         return $this->redirectToRoute('app_login');
  25.         
  26.         /*return $this->render('front/index.html.twig', [
  27.         ]);*/
  28.     }
  29.         
  30.     /**
  31.      * @Route("/inscription", name="front_connexion_inscription", methods={"GET","POST"})
  32.      */
  33.     public function new(SFonction $sFonctionMailerInterface $mailerRequest $requestUserPasswordEncoderInterface $passwordEncoderSJira $SJira): Response
  34.     {
  35.         $utilisateur = new User();
  36.         $form $this->createForm(UserType::class, $utilisateur);
  37.         $form->handleRequest($request);
  38.         $error '';
  39.         if ($form->isSubmitted()) {
  40.             $formData       $form->getData();
  41.             $email $formData->getEmail();
  42.             $nom $formData->getName();
  43.             $prenom $formData->getLastName();
  44.             $em $this->getDoctrine()->getManager();
  45.             $mailClient     $em->getRepository(User::class)->findByEmail($email);
  46.             if (count($mailClient) == && $form->isValid()) {
  47.                 $customerId $SJira->setCustomer($nom ' ' $prenom$email);    //Création du customer ID pour gerer les tickets support sur Jira
  48.                 $utilisateur->setPassword($passwordEncoder->encodePassword($utilisateur$utilisateur->getPassword()));
  49.                 $utilisateur->setRoles(['ROLE_CLIENT']);
  50.                 $utilisateur->setEmail($email);
  51.                 $utilisateur->setName($nom);
  52.                 $utilisateur->setLastname($prenom);
  53.                 $utilisateur->setCustomerId($customerId['customerId']);
  54.                 $utilisateur->setNotificationActive(1);
  55.                 $utilisateur->setErrorJira($customerId['errorJira']);
  56.                 $em->persist($utilisateur);
  57.                 $em->flush();
  58.                 //Récupération des informations
  59.                 $client     $em->getRepository(User::class)->findOneById($utilisateur->getId());
  60.                 //Génération du mail
  61.                 $email = (new TemplatedEmail())         //https://symfony.com/doc/4.4/mailer.html
  62.                 ->from(new Address('support@voxdia.com''Voxdia'))
  63.                 ->to($client->getEmail())
  64.                 //->cc('cc@example.com')
  65.                 //->bcc('bcc@example.com')
  66.                 //->replyTo('support@voxdia.com')
  67.                 ->priority(Email::PRIORITY_HIGH)
  68.                 ->subject('Your account has been successfully created')
  69.                 ->htmlTemplate('__email/client_creation_compte.html.twig')
  70.                 ->context([
  71.                     'prenomClient'      => $client->getName(),
  72.                 ]);
  73.                 //->html('<p>Votre podcast est disponible</p>Lien ici : ');
  74.                 $mailer->send($email);
  75.                 return $this->redirectToRoute('admin_index');
  76.             } else {
  77.                 $error 'An account already exists with this email<br><br>';
  78.             }
  79.         }
  80.         return $this->render('front/connexion_inscription.html.twig', [
  81.             'utilisateur' => $utilisateur,
  82.             'form' => $form->createView(),
  83.             'error' => $error,
  84.         ]);
  85.     }   
  86.     
  87.     /**
  88.      * @Route("/reset", name="front_connexion_reset", methods={"GET","POST"})
  89.      */
  90.     public function reset(Request $requestUserPasswordEncoderInterface $passwordEncoderMailerInterface $mailer): Response
  91.     {
  92.         $request $request->request;
  93.         $resetEmail $request->get('resetEmail');
  94.         
  95.         if (!is_null($resetEmail)) {
  96.             
  97.             $em $this->getDoctrine()->getManager();
  98.             $utilisateur $em->getRepository(User::class)->findOneBy(['email' => $resetEmail]);
  99.     
  100.             if (!is_null($utilisateur)) {
  101.                 $password md5(uniqid());
  102.                 
  103.                 $utilisateur->setPassword($passwordEncoder->encodePassword($utilisateur$password));
  104.                 $em->persist($utilisateur);
  105.                 $em->flush();
  106.                 
  107.                 $email = (new TemplatedEmail())         //https://symfony.com/doc/4.4/mailer.html
  108.                     ->from(new Address('support@voxdia.com''Voxdia'))
  109.                     ->to($resetEmail)
  110.                     //->cc('cc@example.com')
  111.                     //->bcc('bcc@example.com')
  112.                     //->replyTo('support@voxdia.com')
  113.                     //->priority(Email::PRIORITY_HIGH)
  114.                     ->priority(Email::PRIORITY_HIGH)
  115.                     ->subject('Nouveau mot de passe')
  116.                     ->htmlTemplate('__email/nouveau_mdp.html.twig')
  117.                     ->context([
  118.                         'password'          => $password,
  119.                     ]);
  120.                 $mailer->send($email);
  121.                 
  122.                 $retour = [ 'success'   => true,
  123.                             'message'   => 'Password reset email sent. Please check your inbox.',
  124.                             ];
  125.             } else {
  126.                 $retour = [ 'success'   => false,
  127.                             'message'   => 'Wrong email',
  128.                             ];
  129.             }
  130.             
  131.             return new JsonResponse($retourResponse::HTTP_OK);
  132.         }
  133.         
  134.         return $this->render('front/connexion_reset.html.twig', [
  135.         ]);
  136.     }   
  137. }