src/Controller/SecurityController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Entity\User;
  5. use App\Form\UserAccountConfirmType;
  6. use App\Form\UserAccountForgotType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use App\Controller\CommonController;
  14. class SecurityController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/login", name="app_login")
  18.      * @param AuthenticationUtils $authenticationUtils
  19.      * @return Response
  20.      */
  21.     public function login(AuthenticationUtils $authenticationUtils): Response
  22.     {
  23.         // if ($this->getUser()) {
  24.         //     return $this->redirectToRoute('target_path');
  25.         // }
  26.         // get the login error if there is one
  27.         $error $authenticationUtils->getLastAuthenticationError();
  28.         // last username entered by the user
  29.         $lastUsername $authenticationUtils->getLastUsername();
  30.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  31.     }
  32.     /**
  33.      * @Route("/logout", name="app_logout")
  34.      */
  35.     public function logout()
  36.     {
  37.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  38.     }
  39.     /**
  40.      * @Route("/account_forgot", name="app_account_forgot")
  41.      * @param Request $request
  42.      * @return Response
  43.      */
  44.     public function accountForgot(Request $requestCommonController $commonController): Response
  45.     {
  46.         $form $this->createForm(UserAccountForgotType::class, null);
  47.         if ($request->isMethod('POST')) {
  48.             $form->handleRequest($request);
  49.             if ($form->isSubmitted() && $form->isValid()) {
  50.                 $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(['email' => $request->request->all()['user_account_forgot']['email']]);
  51.                 if(!$user) {
  52.                     $this->addFlash('warning''account not found.');
  53.                 }
  54.                 else {
  55.                     $token $commonController->generateUniqueToken();
  56.                     $user->setToken($token);
  57.                     $this->getDoctrine()->getManager()->persist($user);
  58.                     $this->getDoctrine()->getManager()->flush();
  59.                     $commonController->sendMail($user'Popsisign - Forgot password''forgot_password');
  60.                     $this->addFlash('success''A message to retrieve your password has been sent to your email box');;
  61.                     return $this->redirectToRoute('app_login');
  62.                 }
  63.             }
  64.         }
  65.         return $this->render('security/forgot.html.twig', [
  66.             'form' => $form->createView(),
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/account_confirm/{token}", name="app_account_confirm")
  71.      * @param Request $request
  72.      * @param $token
  73.      * @return Response
  74.      */
  75.     public function accountConfirm(Request $request$tokenUserPasswordEncoderInterface $encoder): Response
  76.     {
  77.         try {
  78.             $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(['token'=>$token]);
  79.             if($user) {
  80.                 $form $this->createForm(UserAccountConfirmType::class, $user);
  81.                 if($request->isMethod('POST')) {
  82.                     $form->handleRequest($request);
  83.                     if($form->isSubmitted() && $form->isValid()) {
  84.                         $data $request->request->all();
  85.                         if(preg_match("/^(?=.{8,}$)(?=.*?[A-Z])(?=.*?[0-9])(?=.*?\W).*$/"$data['user_account_confirm']['password']) == 1) {
  86.                             if($data['user_account_confirm']['password'] === $data['user_account_confirm']['plain_password']) {
  87.                                 $user->setToken(null);
  88.                                 $user->setPassword($encoder->encodePassword($user$form->get('password')->getData()));
  89.                                 $this->getDoctrine()->getManager()->persist($user);
  90.                                 $contact_is_exist $this->getDoctrine()->getManager()->getRepository(Contact::class)->findOneBy(['email'=>$user->getEmail()]);
  91.                                 if(!$contact_is_exist) {
  92.                                     $contact = new Contact();
  93.                                     $contact->setFirstname($user->getFirstname());
  94.                                     $contact->setLastname($user->getLastname());
  95.                                     $contact->setEmail($user->getEmail());
  96.                                     $contact->setPhone($user->getPhone());
  97.                                     $contact->setCompany($user->getCompany());
  98.                                     $this->getDoctrine()->getManager()->persist($contact);
  99.                                 }
  100.                                 $this->getDoctrine()->getManager()->flush();
  101.                                 $this->addFlash('success''Your account is confirmed. Welcome to our platform !');
  102.                                 return $this->redirectToRoute('app_login');
  103.                             }
  104.                             else {
  105.                                 $this->addFlash('warning''Not similar enters.');
  106.                             }
  107.                         }
  108.                         else {
  109.                             $this->addFlash('warning''not valid password.');
  110.                         }
  111.                     }
  112.                 }
  113.                 return $this->render('security/confirm.html.twig', [
  114.                     'form' => $form->createView(),
  115.                 ]);
  116.             }
  117.             else {
  118.                 return new Response('user not found'404);
  119.             }
  120.         }
  121.         catch (\Exception $e) {
  122.             return new Response($e->getMessage(), 404);
  123.         }
  124.     }
  125. }