<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use App\Repository\UserRepository;
use App\Form\MotDePasseType;
class MdpController extends AbstractController
{
/**
* @Route("/reset-password", name="reset_password")
*/
public function resetpassword(UserRepository $userRepository, Request $request, EntityManagerInterface $em, MailerInterface $mailer): Response
{
$motDePasseOublieForm = $this->createForm(MotDePasseType::class);
$motDePasseOublieForm->handleRequest($request);
if ($motDePasseOublieForm->isSubmitted() && $motDePasseOublieForm->isValid()) {
if (($user = $userRepository->findOneByEmail($motDePasseOublieForm->get('email')->getData())) != null) {
$alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',
'W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'];
$password = "";
for ($i = 0; $i < 12; $i++) {
$n = Rand(0, count($alphabet)-1);
$password .= $alphabet[$n];
}
$user->setPassword($password);
$em->persist($user);
$em->flush($user);
$email = (new Email())
->from(new Address('noreply@lfpl.fr', 'Déclaration LFPL'))
->to($user->getEmail())
->subject('[LFPL Déclaration Frais Déplacement] Nouveau Mot de Passe')
->embedFromPath($this->getParameter('img_directory').'signature.jpg', 'signature')
->html('<p>Bonjour,<br/><br/>
Vos identifiants de connexion à la <a href="https://app.lfpl.fr/lfpldeclaration/public/">plateforme de déclaration de frais de déplacement</a> sont les suivants :<br/>
Identifiant : '.$user->getEmail().'<br/>
Mot de passe : '.$password.'<br/>
<br/>Sportivement,<br/>
<img src="cid:signature"/><br/>
Mail: <a href="mailto:comptabilite@lfpl.fff.fr">comptabilite@lfpl.fff.fr</a></p>
<p><span style="color: #d8171c">Ce mail a été généré automatiquement, merci de ne pas répondre.</span></p>');
$mailer->send($email);
}
return $this->redirectToRoute('check_mail');
}
return $this->render('mdp/resetpassword.html.twig', [
'motDePasseOublieForm' => $motDePasseOublieForm->createView(),
]);
}
/**
* @Route("/check-mail", name="check_mail")
*/
public function checkmail()
{
return $this->render('mdp/checkmail.html.twig');
}
}