src/Controller/SecurityController.php line 45
<?php
namespace App\Controller;
use App\Form\ResetformpasseType;
use App\Form\ResetformnouvType;
use App\Repository\ParametreRepository;
use App\Repository\UserRepository;
use App\service\sendmailservice;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route(path: '/oblipass', name: 'obli_pass')]
public function oblippass(Request $request,
UserRepository $userRepository,
TokenGeneratorInterface $tokenGeneratorInterface,
EntityManagerInterface $entityManager,
sendmailservice $email,
ParametreRepository $parametreRepository
): Response
{
$form= $this->createForm(ResetformpasseType::class);
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$user = $userRepository->findOneByemail($form->get('email')->getData());
// dd($user);
if($user){
$token =$tokenGeneratorInterface->generateToken();
$user->setResetToken($token);
$entityManager->persist($user);
$entityManager->flush();
$url = $this->generateUrl('pas_oubli',['token'=>$token],UrlGeneratorInterface::ABSOLUTE_URL);
// dd($url);
$context= compact('url','user');
$email->send(
$parametreRepository->getparaIdvalue('emailrepli'),
$user->getEmail(),
'reinitialisation de mot de passe',
'oublipass',
$context
);
$this->addFlash('success',"email envoyé avec succés");
return $this->redirectToRoute('app_login');
}
$this->addFlash('danger',"un probleme est survenu");
return $this->redirectToRoute('app_login');
};
return $this->render('security/resetpass.html.twig',[
'resquetpassform'=> $form->createView()
]);
}
#[Route(path: '/oublipass/{token}', name: 'pas_oubli')]
public function resetpass(string $token,
Request $request,
UserRepository $userRepository,
EntityManagerInterface $entityManager,
UserPasswordHasherInterface $pass
): Response
{
$user = $userRepository->findOneByResetToken($token);
if($user){
$form= $this->createForm(ResetformnouvType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
// dd($form);
$user->setResetToken('');
$user->setPassword(
$pass->hashPassword(
$user,
$form->get('password')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success',"mot de passe changé avec succès");
return $this->redirectToRoute('app_login');
}
return $this->render('security/resetnouv.html.twig',[
'resquetnouvform'=> $form->createView()
]);
}
$this->addFlash('danger',"jeton invalide");
return $this->redirectToRoute('app_login');
}
}