src/Controller/SecurityController.php line 50

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