src/Controller/SecSecurityController.php line 32

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\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  9. use App\Repository\SecUserRepository;
  10. use App\Services\MailerCore;
  11. class SecSecurityController extends AbstractController {
  12.     #[Route('/login'name'app_login')]
  13.     public function index(AuthenticationUtils $authenticationUtilsUserPasswordHasherInterface $passwordHasher): Response {
  14.         $error $authenticationUtils->getLastAuthenticationError();
  15.         $lastUsername $authenticationUtils->getLastUsername();
  16.         return $this->render('sec_security/login.html.twig', [
  17.                     'controller_name' => 'LoginController',
  18.                     'last_username' => $lastUsername,
  19.                     'error' => $error,
  20.         ]);
  21.     }
  22.     #[Route('/password_request'name'app_password_request'methods: ['GET''POST'])]
  23.     public function passwordRequest(Request $requestSecUserRepository $secUserRepositoryMailerCore $mailerCore): Response {
  24.         if ($request->getMethod() == "POST") {
  25.             $username trim($request->request->get('_username'));
  26.             $user $secUserRepository->findOneBy(['username' => $username]);
  27.             if (!$user) {
  28.                 $this->addFlash('warning'"Valide la información suministrada, el usuario no existe.");
  29.                 return $this->redirectToRoute('app_password_request');
  30.             }
  31.             if ($user->getRequestTokenChange() === null) {
  32.                 $tokenChange random_int(100000999999);
  33.                 $user->setRequestTokenChange($tokenChange);
  34.                 $user->setLastRequestToken(new \DateTime('now'));
  35.                 $em $this->getDoctrine()->getManager();
  36.                 $em->persist($user);
  37.                 $em->flush();
  38.                 $mailerCore->notificarUsuario('requestPassword', [$user]);
  39.                 return $this->redirectToRoute('app_confirm_token_password');
  40.             } else {
  41.                 $this->addFlash('warning'"Valide su bandeja de entrada, el usuario ya cuenta con un token generado.");
  42.                 return $this->redirectToRoute('app_confirm_token_password');
  43.             }
  44.         }
  45.         // get the login error if there is one
  46. //        $user = new \App\Entity\SecUser();
  47. //        $user->setUsername('123456789');
  48. //        $user->setEmail('ccastaneda@it-systems.com.co');
  49. //        $plaintextPassword = "123456";
  50. //        $user->setEnabled(1);
  51. //        $user->setRoles(['ROLE_ADMIN','ROLE_USUARIO']);
  52. //        // hash the password (based on the security.yaml config for the $user class)
  53. //        $hashedPassword = $passwordHasher->hashPassword(
  54. //            $user,
  55. //            $plaintextPassword
  56. //        );
  57. //        $user->setPassword($hashedPassword);
  58. //        $em = $this->getDoctrine()->getManager();
  59. //        $em->persist($user);
  60. //        $em->flush();
  61.         return $this->render('sec_security/passwordRequest.html.twig', [
  62.         ]);
  63.     }
  64.     #[Route('/confirm_token_password'name'app_confirm_token_password'methods: ['GET''POST'])]
  65.     public function confirmTokenPassword(Request $requestSecUserRepository $secUserRepositoryMailerCore $mailerCore): Response {
  66.         if ($request->getMethod() == "POST") {
  67.             $token trim($request->request->get('_token'));
  68.             $user $secUserRepository->findOneBy(['requestTokenChange' => $token]);
  69.             if (!$user) {
  70.                 $this->addFlash('warning'"Valide la información suministrada, el usuario no existe.");
  71.                 return $this->redirectToRoute('app_login');
  72.             }
  73.             $sesion $request->getSession();
  74.             $sesion->set('username'$user->getUsername());
  75.             return $this->redirectToRoute('app_password_change');
  76.         }
  77.         // get the login error if there is one
  78. //        $user = new \App\Entity\SecUser();
  79. //        $user->setUsername('123456789');
  80. //        $user->setEmail('ccastaneda@it-systems.com.co');
  81. //        $plaintextPassword = "123456";
  82. //        $user->setEnabled(1);
  83. //        $user->setRoles(['ROLE_ADMIN','ROLE_USUARIO']);
  84. //        // hash the password (based on the security.yaml config for the $user class)
  85. //        $hashedPassword = $passwordHasher->hashPassword(
  86. //            $user,
  87. //            $plaintextPassword
  88. //        );
  89. //        $user->setPassword($hashedPassword);
  90. //        $em = $this->getDoctrine()->getManager();
  91. //        $em->persist($user);
  92. //        $em->flush();
  93.         return $this->render('sec_security/confirmTokenPassword.html.twig', [
  94.         ]);
  95.     }
  96.     #[Route('/password_change'name'app_password_change'methods: ['GET''POST'])]
  97.     public function passwordChange(Request $requestSecUserRepository $secUserRepositoryMailerCore $mailerCoreUserPasswordHasherInterface $passwordHasher): Response {
  98.         if ($request->getMethod() == "POST") {
  99.             $sesion $request->getSession();
  100.             $username trim($sesion->get('username'));
  101.             $user $secUserRepository->findOneBy(['username' => $username]);
  102.             if (!$user) {
  103.                 $this->addFlash('warning'"Valide la información suministrada, el usuario no existe.");
  104.                 return $this->redirectToRoute('app_password_request');
  105.             }
  106.             if ($user->getRequestTokenChange() !== null) {
  107.                 $password trim($request->request->get('_password'));
  108.                 $hashedPassword $passwordHasher->hashPassword(
  109.                         $user,
  110.                         $password
  111.                 );
  112.                 $user->setPassword($hashedPassword);
  113.                 $user->setRequestTokenChange(null);
  114.                 $user->setLastRequestToken(new \DateTime('now'));
  115.                 $em $this->getDoctrine()->getManager();
  116.                 $em->persist($user);
  117.                 $em->flush();
  118.                 $this->addFlash('success'"Su contraseña fue actualizada con exito.");
  119.                 return $this->redirectToRoute('app_login');
  120.             }
  121.         }
  122.         // get the login error if there is one
  123. //        $user = new \App\Entity\SecUser();
  124. //        $user->setUsername('123456789');
  125. //        $user->setEmail('ccastaneda@it-systems.com.co');
  126. //        $plaintextPassword = "123456";
  127. //        $user->setEnabled(1);
  128. //        $user->setRoles(['ROLE_ADMIN','ROLE_USUARIO']);
  129. //        // hash the password (based on the security.yaml config for the $user class)
  130. //        $hashedPassword = $passwordHasher->hashPassword(
  131. //            $user,
  132. //            $plaintextPassword
  133. //        );
  134. //        $user->setPassword($hashedPassword);
  135. //        $em = $this->getDoctrine()->getManager();
  136. //        $em->persist($user);
  137. //        $em->flush();
  138.         return $this->render('sec_security/passwordChange.html.twig', [
  139.         ]);
  140.     }
  141.     /**
  142.      * @Route("/logout", name="app_logout", methods={"POST"})
  143.      */
  144.     public function logout(): void {
  145.         // controller can be blank: it will never be called!
  146.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  147.     }
  148. }