src/Controller/Security/SecurityController.php line 143

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Controller\Abstract\MainController;
  4. use App\Entity\User;
  5. use App\Form\ChangePasswordForm;
  6. use App\Form\SignUpForm;
  7. use App\Service\Email\AccountActivate;
  8. use App\Service\Email\ResetPassword;
  9. use App\Service\Utils\PasswordChecker;
  10. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. use Symfony\Component\Validator\Constraints\Email;
  19. use Symfony\Component\Validator\Validator\ValidatorInterface;
  20. class SecurityController extends MainController
  21. {
  22.     /**
  23.      * @Route("/login", name="app_login")
  24.      */
  25.     public function login(
  26.         Request $request,
  27.         AuthenticationUtils $authenticationUtils
  28.     ): Response {
  29.         if ($this->isLogged()) {
  30.             return $this->redirectToRoute('index');
  31.         }
  32.         return $this->response($request'main\\website\\login.html.twig', [
  33.             'last_username' => $authenticationUtils->getLastUsername(),
  34.             'error'         => $authenticationUtils->getLastAuthenticationError(),
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/reset-password", name="reset_password")
  39.      */
  40.     public function resetPassword(
  41.         Request $request,
  42.         ResetPassword $resetPassword,
  43.         ValidatorInterface $validator
  44.     ): Response {
  45.         if ($this->isLogged()) {
  46.             return $this->redirectToRoute('index');
  47.         }
  48.         $form $this->createFormBuilder(null)
  49.              ->add(
  50.                  'email',
  51.                  EmailType::class,
  52.                  [
  53.                      'label'       => 'Adresa de email',
  54.                      'attr'        => [
  55.                          'autofocus' => true,
  56.                      ],
  57.                      'constraints' => [
  58.                          new Email(['message' => '"{{ value }}" nu este o adresa de email valida']),
  59.                      ],
  60.                  ]
  61.              )
  62.              ->add(
  63.                  'send',
  64.                  SubmitType::class,
  65.                  [
  66.                      'label' => 'Trimite link-ul de resetare parola',
  67.                  ]
  68.              )
  69.              ->getForm();
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted()) {
  72.             $errors $validator->validate($form);
  73.             if ($errors) {
  74.                 foreach ($errors as $error) {
  75.                     return new JsonResponse(
  76.                         [
  77.                             'success' => false,
  78.                             'message' => $error->getMessage()
  79.                         ]
  80.                     );
  81.                 }
  82.             }
  83.             if ($form->isValid() === false) {
  84.                 return new JsonResponse(
  85.                     [
  86.                         'success' => false,
  87.                         'message' => 'Eroare neasteptata!'
  88.                     ]
  89.                 );
  90.             }
  91.             $data $form->getData();
  92.             $userRep $this->em->getRepository(User::CLASS);
  93.             $user $userRep->findOneBy(
  94.                 [
  95.                     'email' => $data['email']
  96.                 ]
  97.             );
  98.             if ($user) {
  99.                 $resetPassword->send($user);
  100.                 return new JsonResponse(
  101.                     [
  102.                         'success' => true,
  103.                         'message' => sprintf('Ok! Am trimis un link pentru schimbarea parolei pe "%s"'$user->getEmail())
  104.                     ]
  105.                 );
  106.             } else {
  107.                 return new JsonResponse(
  108.                     [
  109.                         'success' => false,
  110.                         'message' => sprintf('Nu am gasit un cont cu adresa de email "%s"'$data['email'])
  111.                     ]
  112.                 );
  113.             }
  114.         }
  115.         return $this->response($request'main\\website\\resetPassword.html.twig', [
  116.             'message' => false,
  117.             'form'    => $form->createView(),
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("/resend-activation-code", name="resend_activation_code")
  122.      */
  123.     public function resendActivationCode(
  124.         Request $request,
  125.         AccountActivate $accountActivate,
  126.         ValidatorInterface $validator
  127.     ): Response {
  128.         if ($this->isLogged()) {
  129.             return $this->redirectToRoute('index');
  130.         }
  131.         $form $this->createFormBuilder(null)
  132.              ->add(
  133.                  'email',
  134.                  EmailType::class,
  135.                  [
  136.                      'label'       => 'Adresa de email',
  137.                      'attr'        => [
  138.                          'autofocus' => true,
  139.                      ],
  140.                      'constraints' => [
  141.                          new Email(['message' => '{{ value }} nu este o adresa de email valida'])
  142.                      ]
  143.                  ]
  144.              )
  145.              ->add(
  146.                  'send',
  147.                  SubmitType::class,
  148.                  [
  149.                      'label' => 'Trimite link-ul de activare a contului',
  150.                  ]
  151.              )
  152.              ->getForm();
  153.         $form->handleRequest($request);
  154.         if ($form->isSubmitted()) {
  155.             $errors $validator->validate($form);
  156.             if ($errors) {
  157.                 foreach ($errors as $error) {
  158.                     return new JsonResponse(
  159.                         [
  160.                             'success' => false,
  161.                             'message' => $error->getMessage()
  162.                         ]
  163.                     );
  164.                 }
  165.             }
  166.             if ($form->isValid() === false) {
  167.                 return new JsonResponse(
  168.                     [
  169.                         'success' => false,
  170.                         'message' => 'Eroare neasteptata!'
  171.                     ]
  172.                 );
  173.             }
  174.             $data $form->getData();
  175.             $userRep $this->em->getRepository(User::CLASS);
  176.             $user $userRep->findOneBy(
  177.                 [
  178.                     'email' => $data['email']
  179.                 ]
  180.             );
  181.             if ($user && $user->getStatusId() === User::STATUS_NOT_ACTIVATED) {
  182.                 $accountActivate->send($user);
  183.                 return new JsonResponse(
  184.                     [
  185.                         'success' => true,
  186.                         'message' => sprintf('Ok! Am trimis un link de activare pe adresa de email "%s"'$user->getEmail())
  187.                     ]
  188.                 );
  189.             } else {
  190.                 return new JsonResponse(
  191.                     [
  192.                         'success' => false,
  193.                         'message' => sprintf('Nu am gasit un cont neactivat cu adresa de email "%s"'$data['email'])
  194.                     ]
  195.                 );
  196.             }
  197.         }
  198.         return $this->response($request'main\\website\\resendActivationCode.html.twig', [
  199.             'message' => false,
  200.             'form'    => $form->createView(),
  201.         ]);
  202.     }
  203.     /**
  204.      * @Route("/logout", name="app_logout")
  205.      */
  206.     public function logout(): void
  207.     {
  208.     }
  209.     /**
  210.      * @Route("/change-password", name="change_password", methods={"POST"})
  211.      */
  212.     public function changePassword(
  213.         Request $request,
  214.         UserPasswordHasherInterface $hasher,
  215.         ValidatorInterface $validator,
  216.         PasswordChecker $passwordChecker
  217.     ): Response {
  218.         $form $this->createForm(ChangePasswordForm::class);
  219.         $form->handleRequest($request);
  220.         if ($form->isSubmitted()) {
  221.             $data $form->getData();
  222.             $errors $validator->validate($form);
  223.             if ($errors) {
  224.                 foreach ($errors as $error) {
  225.                     return new JsonResponse(
  226.                         [
  227.                             'success' => false,
  228.                             'message' => $error->getMessage()
  229.                         ]
  230.                     );
  231.                 }
  232.             }
  233.             if ($form->isValid() === false) {
  234.                 return new JsonResponse(
  235.                     [
  236.                         'success' => false,
  237.                         'message' => 'Eroare neasteptata!'
  238.                     ]
  239.                 );
  240.             }
  241.             if (!$hasher->isPasswordValid($this->getUser(), $data['oldPassword'])) {
  242.                 return new JsonResponse(['success' => false'message' => 'Parola curenta nu este valida!']);
  243.             } elseif (($lastError $passwordChecker->check($data['newPassword'], $data['newPassword2'])) !== true) {
  244.                 return new JsonResponse(['success' => false'message' => $lastError]);
  245.             }
  246.             $user $this->getUser();
  247.             $user->setPassword($hasher->hashPassword($user$data['newPassword']));
  248.             $this->em->persist($user);
  249.             $this->em->flush();
  250.             return new JsonResponse(['success' => true'message' => 'Parola a fost schimbata!']);
  251.         }
  252.         return $this->response(
  253.             $request,
  254.             'main\\user\\account\\changePassword.html.twig',
  255.             [
  256.                 'form' => $form->createView(),
  257.             ],
  258.             [
  259.                 'swalClass' => 'smallSize',
  260.             ]
  261.         );
  262.     }
  263.     /**
  264.      * @Route("/inrolare", name="sign_up")
  265.      */
  266.     public function signUp(
  267.         Request $request,
  268.         ValidatorInterface $validator
  269.     ): Response {
  270.         $form $this->createForm(SignUpForm::class);
  271.         $form->handleRequest($request);
  272.         if ($form->isSubmitted()) {
  273.             $data $form->getData();
  274.             $errors $validator->validate($form);
  275.             if ($errors) {
  276.                 foreach ($errors as $error) {
  277.                     return new JsonResponse(
  278.                         [
  279.                             'success' => false,
  280.                             'message' => $error->getMessage()
  281.                         ]
  282.                     );
  283.                 }
  284.             }
  285.             if ($form->isValid() === false) {
  286.                 return new JsonResponse(
  287.                     [
  288.                         'success' => false,
  289.                         'message' => 'Eroare neasteptata!'
  290.                     ]
  291.                 );
  292.             }
  293.             return new JsonResponse(['success' => true'message' => 'Parola a fost schimbata!']);
  294.         }
  295.         return $this->response(
  296.             $request,
  297.             'main\\website\\signUp.html.twig',
  298.             [
  299.                 'form' => $form->createView(),
  300.             ],
  301.             [
  302.                 'swalClass' => 'smallSize',
  303.             ]
  304.         );
  305.     }
  306. }