src/Controller/SecurityController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\Notifications;
  5. use Doctrine\ORM\EntityManager;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  7. use Symfony\Bridge\Doctrine\ManagerRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Twig\Environment;
  15. class SecurityController extends AbstractController
  16. {
  17.     private $notifications;
  18.     private $twig;
  19.     public function __construct(Notifications $notificationsEnvironment $twig)
  20.     {
  21.         $this->notifications $notifications;
  22.         $this->twig $twig;
  23.     }
  24.     /**
  25.      * @Route("/login", name="app_login")
  26.      */
  27.     public function login(Request $requestAuthenticationUtils $authenticationUtils): Response
  28.     {
  29.         // get the login error iff there is one
  30.         $error $authenticationUtils->getLastAuthenticationError();
  31.         // last username entered by the user
  32.         $lastUsername $authenticationUtils->getLastUsername();
  33.         return $this->render('security/index.html.twig',
  34.             ['last_username' => $lastUsername'error' => $error]);
  35.     }
  36.     /**
  37.      * @Route("/logout", name="app_logout", methods={"GET"})
  38.      */
  39.     public function logout()
  40.     {
  41.         // throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  42.     }
  43.     /**
  44.      * @Route("public/restore/password", name="app_restore_password")
  45.      */
  46.     public function restore(  Request $requestUserPasswordHasherInterface $passwordEncoder  ) {
  47.         $em $this->getDoctrine()->getManager();
  48.         $email $request->get('emailR');
  49.         $userD $em->getRepository(User::class)->findOneBy(['email' => $email]);
  50.         if ( ! empty($userD)) {
  51.             $passNew rand(1000099999999);
  52.             $this->changePass($userD->getId(), $passNew$passwordEncoder);
  53.             $this->addFlash(
  54.                 'success',
  55.                 'Te enviamos un correo con tu nueva contraseña.'
  56.             );
  57.             $subject 'Nueva Contraseña';
  58.             $message "Estimado Usuario, hemos restablecido tu contraseña para el acceso a nuestra plataforma. <br> <br>
  59.      
  60.             Tu nueva contraseña es: <strong>" $passNew " </strong> <br><br>
  61.  
  62.   Dentro del sistema podrás visualizar el estado de las licitaciones. Solo debes ingresar con tu correo y la nueva contraseña. <br><br> ¡Te esperamos! <br>  Saludos Cordiales";
  63.             $htmlContents $this->twig->render('security/email.html.twig', [
  64.                 'title'    => $subject,
  65.                 'messages' => $message,
  66.             ]);
  67.             $this->notifications->newNotificationSystem(
  68.                 $email,
  69.                 $subject,
  70.                 $htmlContents
  71.             );
  72.         } else {
  73.             $this->addFlash(
  74.                 'error',
  75.                 'El correo ingresado no esta disponible en la plataforma'
  76.             );
  77.         }
  78.         return $this->redirectToRoute('app_login');
  79.     }
  80.     function changePass($id$pass$passwordEncoder)
  81.     {
  82.         $em $this->getDoctrine()->getManager();
  83.         $userD $em->getRepository(User::class)->find($id);
  84.         $userD->setPassword($passwordEncoder->hashPassword($userD$pass));
  85.         $em->flush();
  86.         return true;
  87.     }
  88. }