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 $authenticationUtils): 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.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  51.     }
  52.     
  53.     
  54.     /**
  55.      * @Route("/recover-password", name="app_recover_password")
  56.      */
  57.     public function recoverPassword(Request $requestAuthenticationUtils $authenticationUtilsEntityManagerInterface $entityManagerHelperService $helperService): Response
  58.     {
  59.         
  60.         if ($request->isMethod('POST')) {
  61.             if($request->get("email_recover")){
  62.                 
  63.                 $email $request->get("email_recover");
  64.                 $userObj $entityManager->getRepository(User::class)->findOneBy([
  65.                     'email' => $email
  66.                 ]);
  67.                 $systemConfiguration $entityManager->getRepository(SystemConfiguration::class)->findOneBy([
  68.                     'is_active' => 1
  69.                 ]);
  70.                 
  71.                 $rootWebUrl $systemConfiguration->getImgUrl();
  72.                 if($userObj){
  73.                     $pass $helperService->randomChars(8);
  74.                     $encoded  $this->passwordEncoder->encodePassword($userObj$pass);
  75.                     $userObj->setPassword($encoded);
  76.                     $entityManager->persist($userObj);
  77.                     $entityManager->flush();
  78.                     $email_receipt =  $email;
  79.                     $subject 'Su contraseña ha sido restablecida exitosamente';
  80.                     $text_email "Recientemente solicitó restablecer su contraseña para su cuenta de Disatel. La contraseña temporal se muestra a continuación:
  81.                        <br><b><h3>".$pass."</h3></b>
  82.                        ";
  83.                     
  84.                     $message $this->twig->render('_mail_template.html.twig', [
  85.                            'emailTitle' => $subject,
  86.                            'rootWebUrl' => $rootWebUrl."/public",
  87.                            'message' => $text_email
  88.                     ]);
  89.                     $email = (new Email())
  90.                       ->from(new Address("notificaciones@grupodisatel.com"'DISATEL'))
  91.                       ->to($email_receipt)
  92.                       ->subject($subject)
  93.                       ->html($message);
  94.                     $response_mail $this->mailer->send($email);
  95.                     $mailLog = new MailLog();
  96.                     $mailLog->setEmail($email_receipt);
  97.                     $mailLog->setResponse($response_mail);
  98.                     $mailLog->setContent($text_email);
  99.                     $mailLog->setCreatedAt(new \DateTime());
  100.                     $entityManager->persist($mailLog);
  101.                     $entityManager->flush();
  102.                     $this->addFlash('success'$this->getParameter('form_new_success'));
  103.                 }    
  104.                 return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  105.             }        
  106.         }
  107.     }
  108.     
  109.     /**
  110.      * @Route("/logout", name="app_logout")
  111.      */
  112.     public function logout(): void
  113.     {
  114.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  115.     }
  116. }