<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use SendinBlue\Client\Model\SendEmail;
use DateTime;
use SendinBlue\Client\Api\SMTPApi;
class SecurityController extends AbstractController
{
/**
* @Route("/", 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();
// if($lastUsername){
// return $this->redirectToRoute("app_coupons");
// }
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
}
/**
* @Route("/forgot", name="forgot_password")
*/
public function forgotPassword(Request $request, RouterInterface $router) {
$email = $request->get('email');
$manager = $this->getDoctrine()->getManager();
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $email]);
if (!$email) {
return $this->render( 'security/forgot-pass.html.twig');
}
if (!$user) {
$this->addFlash('danger', 'Email could not be found.');
return $this->render( 'security/forgot-pass.html.twig');
}
$token = md5(random_bytes(20));
$user->setToken($token);
$date = new DateTime('+24 hours');
$user->setTokenExpire($date->getTimestamp());
$manager->flush();
$url = $router->generate('reset_password', [
'token' => $token
], RouterInterface::ABSOLUTE_URL);
$sendEmail = new SendEmail();
$sendEmail->setEmailTo([$email])
->setAttributes(["RESET_LINK" => $url]);
$api_instance = new SMTPApi();
$api_instance->getConfig()->setApiKey("api-key","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");
$api_instance->sendTemplate($_ENV["TEMPLATE_RESET_PASSWORD"], $sendEmail);
$this->addFlash('success', 'Email sent.');
return $this->render( 'security/forgot-pass.html.twig' );
}
/**
* @Route("/reset/{token}", name="reset_password")
*/
public function resetPassword(Request $request, $token, UserPasswordEncoderInterface $passwordEncoder) {
$user = $this->getDoctrine()->getRepository(User::class)->findOneByToken($token);
if (!$user) {
$this->addFlash('danger', 'Invalid token');
return $this->redirectToRoute('app_coupons');
}
$manager = $this->getDoctrine()->getManager();
$newPassword = $request->get('new_password');
$repeatPassword = $request->get('repeat_password');
if ($newPassword) {
if ($newPassword != $repeatPassword) {
$this->addFlash('danger', 'Les mots de passe ne sont pas identiques');
return $this->render( 'security/reset-pass.html.twig', [ 'email' => $user->getEmail()] );
}
if (strlen($newPassword) < 8) {
$this->addFlash('danger', 'Password must have at least 8 characters');
return $this->render( 'security/reset-pass.html.twig', [ 'email' => $user->getEmail()] );
}
$user->setPassword($passwordEncoder->encodePassword($user, $newPassword));
$user->setToken(NULL);
$user->setTokenExpire(NULL);
$manager->flush();
$this->addFlash('success', 'Password reseted');
return $this->redirectToRoute('app_login');
}
return $this->render( 'security/reset-pass.html.twig', [ 'email' => $user->getEmail() ] );
}
}