src/Controller/RegistrationController.php line 92

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\Builder\ChangePasswordFormType;
  5. use App\Form\Builder\RegistrationFormType;
  6. use App\Security\EmailVerifier;
  7. use App\Service\UuidService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     private EntityManagerInterface $entityManager;
  20.     private EmailVerifier $emailVerifier;
  21.     public function __construct(EmailVerifier $emailVerifierEntityManagerInterface $entityManager)
  22.     {
  23.         $this->entityManager $entityManager;
  24.         $this->emailVerifier $emailVerifier;
  25.     }
  26.     #[Route('/{_locale}/register'name'app_register'requirements: ['_locale' => 'en|ru|hy'], defaults: ['_locale' => 'hy'])]
  27.     public function register(
  28.         Request $request,
  29.         UserPasswordHasherInterface $userPasswordHasher,
  30.         EntityManagerInterface $entityManager
  31.     ): Response {
  32.         if ($this->getUser()) {
  33.             return $this->redirectToRoute('page_home');
  34.         }
  35.         $user = new User();
  36.         $form $this->createForm(RegistrationFormType::class, $user);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             // encode the plain password
  40.             $user->setPassword(
  41.                 $userPasswordHasher->hashPassword(
  42.                     $user,
  43.                     $form->get('password')->getData()
  44.                 )
  45.             );
  46.             $user->setIsVerified(true);
  47.             $user->setRoles(["ROLE_USER"]);
  48.             $entityManager->persist($user);
  49.             $entityManager->flush();
  50.             // generate a signed url and email it to the user
  51. //            $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
  52. //                (new TemplatedEmail())
  53. //                    ->from(new Address('example@example.com', 'Deep sleep'))
  54. //                    ->to($user->getEmail())
  55. //                    ->subject('Please Confirm your Email')
  56. //                    ->htmlTemplate('password-reset.twig')
  57. //            );
  58.             // do anything else you need here, like send an email
  59.             return $this->redirectToRoute('sonata_admin_dashboard');
  60.         }
  61.         return $this->render('registration/register.html.twig', [
  62.             'registrationForm' => $form->createView(),
  63.         ]);
  64.     }
  65.     private function sendEmail($user): void
  66.     {
  67.         $this->emailVerifier->sendEmailConfirmation(
  68.             'page_password-verify',
  69.             $user,
  70.             (new TemplatedEmail())
  71.                 ->from(new Address('example@example.com''Deep sleep'))
  72.                 ->to($user->getemail())
  73.                 ->to('ashot.arzumanyan99@gmail.com')
  74.                 ->subject('Please Confirm your Email')
  75.                 ->context([
  76.                     'code' => $user->getVerificationCode(),
  77.                 ])
  78.                 ->htmlTemplate('registration/password-reset.twig')
  79.         );
  80.     }
  81.     #[Route('/{_locale}/password/reset'name'page_password-reset'requirements: ['_locale' => 'en|ru|hy'], defaults: ['_locale' => 'hy'])]
  82.     public function verifyUserEmail(Request $requestUuidService $uuid_service): Response
  83.     {
  84.         $email $request->get('email');
  85.         $user $this->entityManager->getRepository(User::class)->findOneBy(["email" => $email]);
  86.         $digits 5;
  87.         $uuid $uuid_service->generate();
  88.         $code rand(pow(10$digits 1), pow(10$digits) - 1);
  89.         $code 12345;
  90.         if (!$user) {
  91.             return $this->render('registration/password-reset.twig');
  92.         } else {
  93.             $user->setVerificationCode($code);
  94.             $user->setUuid($uuid);
  95.             $this->entityManager->persist($user);
  96.             $this->entityManager->flush();
  97.             $this->sendEmail($user);
  98.             return $this->redirectToRoute('page_password-verify');
  99.         }
  100.     }
  101.     #[Route('/{_locale}/password/verify'name'page_password-verify'requirements: ['_locale' => 'en|ru|hy'], defaults: ['_locale' => 'hy'])]
  102.     public function verifyCode(Request $request): Response
  103.     {
  104.         $session $request->getSession();
  105.         $uuid $session->get('uuid');
  106. //        if ( ! $uuid) {
  107. //            return $this->redirectToRoute('page_home');
  108. //        }
  109.         $user $this->entityManager->getRepository(User::class)->findOneBy(["uuid" => $uuid]);
  110.         if ($request->get('submit')) {
  111.             $code $request->get('code');
  112.             if ($user->getVerificationCode() == (int)$code) {
  113.                 $session->set('code', (int)$code);
  114.                 return $this->redirectToRoute('page_password-change');
  115.             } else {
  116.                 return $this->redirectToRoute('page_password-verify');
  117.             }
  118.         }
  119.         return $this->render('registration/receive-code.html.twig');
  120.     }
  121.     #[Route('/password/resend'name'page_password-resend')]
  122.     public function resendEmail(Request $request): Response
  123.     {
  124.         $session $request->getSession();
  125.         $uuid $session->get('uuid');
  126.         $user $this->entityManager->getRepository(User::class)->findOneBy(["uuid" => $uuid]);
  127.         $this->sendEmail($user);
  128.         return $this->redirectToRoute('page_password-verify');
  129.     }
  130.     #[Route('/{_locale}/password/change'name'page_password-change'requirements: ['_locale' => 'en|ru|hy'], defaults: ['_locale' => 'hy'])]
  131.     public function changePassword(
  132.         Request $request,
  133.         UserPasswordHasherInterface $userPasswordHasher,
  134.         EntityManagerInterface $entity_manager
  135.     ): Response {
  136.         $session $request->getSession();
  137.         $uuid $session->get('uuid');
  138.         if ( ! $uuid) {
  139.             return $this->redirectToRoute('page_home');
  140.         }
  141.         $code $session->get('code');
  142.         $user $this->entityManager->getRepository(User::class)->findOneBy(["uuid" => $uuid]);
  143.         if ( ! $user->getVerificationCode() == $code) {
  144.             return $this->redirectToRoute('page_password-verify');
  145.         }
  146.         $form $this->createForm(ChangePasswordFormType::class, $user);
  147.         $form->handleRequest($request);
  148.         if ($form->isSubmitted() && $form->isValid()) {
  149.             // encode the plain password
  150.             $user->setPassword(
  151.                 $userPasswordHasher->hashPassword(
  152.                     $user,
  153.                     $form->get('password')->getData()
  154.                 )
  155.             );
  156.             $entity_manager->flush();
  157.             return $this->redirectToRoute('app_login');
  158.         }
  159.         return $this->render('registration/password-change.html.twig', [
  160.             'registrationForm' => $form->createView(),
  161.         ]);
  162.     }
  163.     #[Route('{_locale}/change-user-info'name'change_user_info'requirements: ['_locale' => 'en|ru|hy'], defaults: ['_locale' => 'hy'], methods'POST')]
  164.     public function changeUserInfo(Request $requestUserPasswordHasherInterface $passwordHasherEntityManagerInterface $entityManager): Response
  165.     {
  166.         $user $this->getUser();
  167.         $data $request->request->all();
  168.         $oldPassword $request->request->get('passwordOld');
  169.         $newPassword $request->request->get('passwordNew');
  170.         $repeatPassword $request->request->get('passwordRepeat');
  171.         if ( ! empty($oldPassword) || ! empty($newPassword) || ! empty($repeatPassword)) {
  172.             if ( ! $passwordHasher->isPasswordValid($user$oldPassword)) {
  173.                 $this->addFlash('error''incorrect_old_password');
  174.                 return $this->redirectToRoute('page_profile');
  175.             }
  176.             // Check if the new passwords match
  177.             if ($newPassword !== $repeatPassword) {
  178.                 $this->addFlash('error''passwords_do_not_match');
  179.                 return $this->redirectToRoute('page_profile');
  180.             }
  181.             $hashedPassword $passwordHasher->hashPassword($user$newPassword);
  182.             $user->setPassword($hashedPassword);
  183.         }
  184.         if (($user->getCountry() !== $data['country']) && ! empty($data['country'])) {
  185.             $user->setCountry($data['country']);
  186.         }
  187.         if (($user->getPhone() !== $data['phone']) && ! empty($data['phone'])) {
  188.             $user->setPhone($data['phone']);
  189.         }
  190.         $userEmail $entityManager->getRepository(User::class)->findOneBy(["email" => $data['email']]);
  191.         if (!empty($userEmail) and $user->getEmail() !== $data['email']){
  192.             $this->addFlash('error''user_exist');
  193.             return $this->redirectToRoute('page_profile');
  194.         }
  195.         if (($user->getEmail() !== $data['email']) && !empty($data['email']) && empty($userEmail)) {
  196.             $user->setEmail($data['email']);
  197.         }
  198.         if (($user->getCity() !== $data['city']) && ! empty($data['city'])) {
  199.             $user->setCity($data['city']);
  200.         }
  201.         $entityManager->persist($user);
  202.         $entityManager->flush();
  203.         $this->addFlash('success''user_changed_successfully');
  204.         return $this->redirectToRoute('page_profile');
  205.     }
  206. }