src/Controller/Frontend/SecurityController.php line 21

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\User\User;
  4. use App\Exception\User\PasswordRecovery\InvalidLink;
  5. use App\Forms\Types\User\RegistrationType;
  6. use App\Service\User\Event\UserCreatedEvent;
  7. use App\Service\User\UserManager;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. class SecurityController extends AbstractController
  16. {
  17.     #[Route(path'/login'name'login'priority50)]
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // if ($this->getUser()) {
  21.         //     return $this->redirectToRoute('target_path');
  22.         // }
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  28.     }
  29.     #[Route(path'/password-recover/{token}'name'password_restore')]
  30.     public function passwordRestore(
  31.         \App\Entity\User\PasswordRecovery  $passwordRecovery,
  32.         \App\Service\User\PasswordRecovery $prService,
  33.         EntityManagerInterface             $em
  34.     ) {
  35.         if ($this->getUser()) {
  36.             return $this->redirectToRoute('main_olymp');
  37.         }
  38.         try {
  39.             $result $prService->createNewPassword($passwordRecovery);
  40.             $em->flush();
  41.         } catch (InvalidLink $e) {
  42.             return $this->render(
  43.                 'security/password-restore-invalid-link.html.twig',
  44.                 []
  45.             );
  46.         }
  47.         return $this->render(
  48.             'security/password-restore-send.html.twig',
  49.             []
  50.         );
  51.     }
  52.     /**
  53.      * @return Response
  54.      */
  55.     #[Route(path'/password-recover'name'password_recovery')]
  56.     public function passwordRecoveryForm(
  57.         \App\Service\User\PasswordRecovery $prService,
  58.         EntityManagerInterface             $em,
  59.         Request                            $request
  60.     ) {
  61.         $data = new \App\Model\User\PasswordRecovery();
  62.         $form $this->createForm(\App\Forms\Types\User\PasswordRecoveryType::class, $data);
  63.         $form->handleRequest($request);
  64. //        $form->addError(new FormError('test'));
  65.         $send false;
  66.         $haveError false;
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             $prService->initPasswordRecovery($data);
  69.             $em->flush();
  70.             $send true;
  71. //            if ($form->isValid()) {
  72. //                $em->flush();
  73. //                return $this->json(['ok' => true]);
  74. //            }
  75.         }
  76.         return $this->render(
  77.             'security/password-recovery-form.html.twig',
  78.             [
  79.                 'send' => $send,
  80.                 'form' => $form->createView(),
  81.             ]
  82.         );
  83.     }
  84.     #[Route(path'/logout'name'app_logout')]
  85.     public function logout()
  86.     {
  87.         throw new \LogicException(
  88.             'This method can be blank - it will be intercepted by the logout key on your firewall.'
  89.         );
  90.     }
  91.     /**
  92.      * @return Response
  93.      */
  94.     #[Route(path'/registration'name'registration')]
  95.     public function registration(
  96.         Request                  $request,
  97.         EntityManagerInterface   $em,
  98.         UserManager              $userManager,
  99.         EventDispatcherInterface $ed
  100.     ): Response {
  101.         $user = new User();
  102. //        $user->setEmail("kulebyakin@gmail.com");
  103. //        $user->setPhone("+79266970986");
  104. //        $user->setFirstName("Миша");
  105.         $send false;
  106.         $form $this->createForm(RegistrationType::class, $user);
  107.         $form->handleRequest($request);
  108.         if ($form->isSubmitted() && $form->isValid()) {
  109.             $userManager->create($user);
  110.             $event = new UserCreatedEvent(
  111.                 $user/*, [
  112.                          UserCreatedEvent::SUBSCRIBE => $form->get('subscribe')->getData(),
  113.                      ]
  114.            */
  115.             );
  116.             $ed->dispatch($event$event::NAME);
  117.             $em->persist($user);
  118. //            if ($form->get('subscribe')->getData()) {
  119. //                try {
  120. //                    $subscribe->subscribe($user->getEmail());
  121. //                } catch (DuplicateException $e) {
  122. //                }
  123. //            }
  124.             $em->flush();
  125.             $send true;
  126.         }
  127.         return $this->render(
  128.             'security/registration-form.html.twig',
  129.             [
  130.                 'send' => $send,
  131.                 'form' => $form->createView(),
  132.             ]
  133.         );
  134.     }
  135.     /**
  136.      * @param Request $request
  137.      * @param EntityManagerInterface $em
  138.      * @param UserManager $userManager
  139.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  140.      */
  141.     #[Route(path'/reg/ajax'name'registration_ajax'methods: ['post'])]
  142.     public function ajaxRegistration(
  143.         Request                  $request,
  144.         EntityManagerInterface   $em,
  145.         UserManager              $userManager,
  146.         EventDispatcherInterface $ed
  147.     ) {
  148.         $form $this->createForm(RegistrationType::class);
  149.         $form->handleRequest($request);
  150.         if ($form->isSubmitted() && $form->isValid()) {
  151.             $user $form->getData();
  152.             $userManager->create($user);
  153.             $event = new UserCreatedEvent(
  154.                 $user,
  155.                 [
  156.                     UserCreatedEvent::SUBSCRIBE => $form->get('subscribe')->getData(),
  157.                 ]
  158.             );
  159.             $ed->dispatch($event$event::NAME);
  160.             $em->persist($user);
  161.             if ($form->get('subscribe')->getData()) {
  162.             }
  163.             $em->flush();
  164.             return $this->json(['ok' => true]);
  165.         }
  166.         $errors = [];
  167. //        $form->get('phone')->addError(new FormError('sdf'));
  168.         foreach ($form->getErrors(true) as $error) {
  169.             $name $error->getOrigin()->getName();
  170.             if (empty($errors[$name])) {
  171.                 $errors[$name] = [];
  172.             }
  173.             $errors[$name][] = $error->getMessage();
  174.         }
  175.         return $this->json(
  176.             [
  177.                 'ok' => false,
  178.                 "errors" => $errors,
  179.             ]
  180.         );
  181.     }
  182. }