src/Controller/SecurityController.php line 45

  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ResetformpasseType;
  4. use App\Form\ResetformnouvType;
  5. use App\Repository\ParametreRepository;
  6. use App\Repository\UserRepository;
  7. use App\service\sendmailservice;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class SecurityController extends AbstractController
  18. {
  19.     #[Route(path'/login'name'app_login')]
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         // if ($this->getUser()) {
  23.         //     return $this->redirectToRoute('target_path');
  24.         // }
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  30.     }
  31.     #[Route(path'/logout'name'app_logout')]
  32.     public function logout(): void
  33.     {
  34.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  35.     }
  36.     #[Route(path'/oblipass'name'obli_pass')]
  37.     public function oblippass(Request $request,
  38.     UserRepository $userRepository,
  39.     TokenGeneratorInterface $tokenGeneratorInterface,
  40.     EntityManagerInterface $entityManager,
  41.     sendmailservice $email,
  42.     ParametreRepository $parametreRepository
  43.     ): Response
  44.     {
  45.         $form$this->createForm(ResetformpasseType::class);
  46.         $form->handleRequest($request);
  47.            // dd($form);
  48.            if($form->isSubmitted() && $form->isValid()){
  49.                     $user $userRepository->findOneByemail($form->get('email')->getData());
  50.                   //  dd($user);
  51.                   if($user){
  52.                         $token =$tokenGeneratorInterface->generateToken();
  53.                         $user->setResetToken($token);
  54.                     $entityManager->persist($user);
  55.                     $entityManager->flush();
  56.                     $url $this->generateUrl('pas_oubli',['token'=>$token],UrlGeneratorInterface::ABSOLUTE_URL);
  57.                    // dd($url);
  58.                     $contextcompact('url','user');
  59.                     $email->send(
  60.                         $parametreRepository->getparaIdvalue('emailrepli'),
  61.                         $user->getEmail(),
  62.                         'reinitialisation de mot de passe',
  63.                         'oublipass',
  64.                         $context
  65.                     );
  66.                     $this->addFlash('success',"email envoyé avec succés");
  67.                     return $this->redirectToRoute('app_login');
  68.                   }
  69.                   $this->addFlash('danger',"un probleme est survenu");
  70.                   return $this->redirectToRoute('app_login');
  71.            };
  72.            
  73.        return $this->render('security/resetpass.html.twig',[
  74.             'resquetpassform'=> $form->createView()
  75.         ]);
  76.     }
  77.     
  78.     
  79.     #[Route(path'/oublipass/{token}'name'pas_oubli')]
  80.     public function resetpass(string $token,
  81.     Request $request,
  82.     UserRepository $userRepository,
  83.     EntityManagerInterface $entityManager,
  84.     UserPasswordHasherInterface $pass
  85.     ): Response
  86.     {
  87.         $user $userRepository->findOneByResetToken($token);
  88.         if($user){
  89.             $form$this->createForm(ResetformnouvType::class);
  90.            
  91.                  $form->handleRequest($request);
  92.          
  93.            if($form->isSubmitted() && $form->isValid()){
  94.               // dd($form);
  95.                $user->setResetToken('');
  96.                $user->setPassword(
  97.                 $pass->hashPassword(
  98.                     $user,
  99.                     $form->get('password')->getData()
  100.                 )
  101.                );
  102.                $entityManager->persist($user);
  103.                $entityManager->flush();
  104.                $this->addFlash('success',"mot de passe changé avec succès");
  105.                return $this->redirectToRoute('app_login');
  106.            } 
  107.            return $this->render('security/resetnouv.html.twig',[
  108.                 'resquetnouvform'=> $form->createView()
  109.             ]);
  110.         }
  111.         $this->addFlash('danger',"jeton invalide");
  112.                   return $this->redirectToRoute('app_login');
  113.     }
  114. }