<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Entity\User;
use App\Form\UserAccountConfirmType;
use App\Form\UserAccountForgotType;
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\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Controller\CommonController;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
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("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/account_forgot", name="app_account_forgot")
* @param Request $request
* @return Response
*/
public function accountForgot(Request $request, CommonController $commonController): Response
{
$form = $this->createForm(UserAccountForgotType::class, null);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(['email' => $request->request->all()['user_account_forgot']['email']]);
if(!$user) {
$this->addFlash('warning', 'account not found.');
}
else {
$token = $commonController->generateUniqueToken();
$user->setToken($token);
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
$commonController->sendMail($user, 'Popsisign - Forgot password', 'forgot_password');
$this->addFlash('success', 'A message to retrieve your password has been sent to your email box');;
return $this->redirectToRoute('app_login');
}
}
}
return $this->render('security/forgot.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/account_confirm/{token}", name="app_account_confirm")
* @param Request $request
* @param $token
* @return Response
*/
public function accountConfirm(Request $request, $token, UserPasswordEncoderInterface $encoder): Response
{
try {
$user = $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(['token'=>$token]);
if($user) {
$form = $this->createForm(UserAccountConfirmType::class, $user);
if($request->isMethod('POST')) {
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
if(preg_match("/^(?=.{8,}$)(?=.*?[A-Z])(?=.*?[0-9])(?=.*?\W).*$/", $data['user_account_confirm']['password']) == 1) {
if($data['user_account_confirm']['password'] === $data['user_account_confirm']['plain_password']) {
$user->setToken(null);
$user->setPassword($encoder->encodePassword($user, $form->get('password')->getData()));
$this->getDoctrine()->getManager()->persist($user);
$contact_is_exist = $this->getDoctrine()->getManager()->getRepository(Contact::class)->findOneBy(['email'=>$user->getEmail()]);
if(!$contact_is_exist) {
$contact = new Contact();
$contact->setFirstname($user->getFirstname());
$contact->setLastname($user->getLastname());
$contact->setEmail($user->getEmail());
$contact->setPhone($user->getPhone());
$contact->setCompany($user->getCompany());
$this->getDoctrine()->getManager()->persist($contact);
}
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'Your account is confirmed. Welcome to our platform !');
return $this->redirectToRoute('app_login');
}
else {
$this->addFlash('warning', 'Not similar enters.');
}
}
else {
$this->addFlash('warning', 'not valid password.');
}
}
}
return $this->render('security/confirm.html.twig', [
'form' => $form->createView(),
]);
}
else {
return new Response('user not found', 404);
}
}
catch (\Exception $e) {
return new Response($e->getMessage(), 404);
}
}
}