src/Controller/SecurityController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Twig\Environment;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\Mime\Address;
  14. use App\Service\HelperService;
  15. use App\Entity\MailLog;
  16. use App\Entity\User;
  17. use App\Repository\UserRepository;
  18. class SecurityController extends AbstractController
  19. {
  20.     
  21.     private $passwordEncoder;
  22.     private $twig;
  23.     private $mailer;
  24.     
  25.     public function __construct(
  26.                     UserPasswordEncoderInterface $passwordEncoder,
  27.                     Environment $twig,
  28.                     MailerInterface $mailer
  29.                     ) {
  30.         $this->passwordEncoder $passwordEncoder;
  31.         $this->twig $twig;
  32.         $this->mailer $mailer;
  33.     
  34.     }
  35.     /**
  36.      * @Route("/login", name="app_login")
  37.      */
  38.     public function login(Request $requestAuthenticationUtils $authenticationUtilsEntityManagerInterface $entityManagerHelperService $helperService): Response
  39.     {
  40.         
  41.         // if ($this->getUser()) {
  42.         //     return $this->redirectToRoute('target_path');
  43.         // }
  44.         // get the login error if there is one
  45.         $error $authenticationUtils->getLastAuthenticationError();
  46.         // last username entered by the user
  47.         $lastUsername $authenticationUtils->getLastUsername();
  48.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  49.     }
  50.     
  51.     
  52.     /**
  53.      * @Route("/recover-password", name="app_recover_password")
  54.      */
  55.     public function recoverPassword(Request $requestAuthenticationUtils $authenticationUtilsEntityManagerInterface $entityManagerHelperService $helperService): Response
  56.     {
  57.         
  58.         if ($request->isMethod('POST')) {
  59.             if($request->get("email_recover")){
  60.                 
  61.                 $email $request->get("email_recover");
  62.                 $userObj $entityManager->getRepository(User::class)->findOneBy([
  63.                     'email' => $email
  64.                 ]);
  65.                 
  66.                 if($userObj){
  67.                     $pass $helperService->randomChars(8);
  68.                     $encoded  $this->passwordEncoder->encodePassword($userObj$pass);
  69.                     $userObj->setPassword($encoded);
  70.                     $entityManager->persist($userObj);
  71.                     $entityManager->flush();
  72.                     $email_receipt =  $email;
  73.                     $subject 'Su contraseña ha sido restablecida exitosamente';
  74.                     $text_email "Recientemente solicitó restablecer su contraseña para su cuenta de Disatel. La contraseña temporal se muestra a continuación:
  75.                        <br><b><h3>".$pass."</h3></b>
  76.                        ";
  77.                     $rootWebUrlOriginal $request->getSchemeAndHttpHost();
  78.                     $rootWebUrl $request->getSchemeAndHttpHost();
  79.                     if($rootWebUrl == "https://dev2.datoscontinuos.net"){
  80.                               $rootWebUrl $rootWebUrl."/2308/app_logistic_backend";
  81.                     }
  82.                     $message $this->twig->render('_mail_template.html.twig', [
  83.                            'emailTitle' => $subject,
  84.                            'rootWebUrl' => $rootWebUrl."/public",
  85.                            'message' => $text_email
  86.                     ]);
  87.                     $email = (new Email())
  88.                       ->from(new Address("notificaciones@grupodisatel.com"'DISATEL'))
  89.                       ->to($email_receipt)
  90.                       ->subject($subject)
  91.                       ->html($message);
  92.                     $response_mail $this->mailer->send($email);
  93.                     $mailLog = new MailLog();
  94.                     $mailLog->setEmail($email_receipt);
  95.                     $mailLog->setResponse($response_mail);
  96.                     $mailLog->setContent($text_email);
  97.                     $mailLog->setCreatedAt(new \DateTime());
  98.                     $entityManager->persist($mailLog);
  99.                     $entityManager->flush();
  100.                     $this->addFlash('success'$this->getParameter('form_new_success'));
  101.                 }    
  102.                 return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  103.             }        
  104.         }
  105.     }
  106.     /**
  107.      * @Route("/logout", name="app_logout")
  108.      */
  109.     public function logout(): void
  110.     {
  111.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  112.     }
  113. }