src/Controller/PasswordController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Exception;
  4. use App\Security\User;
  5. use App\Service\TokenService;
  6. use App\Form\UserPasswordType;
  7. use App\Form\RecoveryPasswordType;
  8. use App\Service\Api\ApiServicePortalService;
  9. use App\Service\Interfaces\ApiInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. /**
  16.  * @Route("/password", name="password_")
  17.  */
  18. class PasswordController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/recovery", name="recovery")
  22.      * @param Request $request
  23.      * @param UserRepository $userRepository
  24.      * @param TranslatorInterface $translator
  25.      * @return RedirectResponse|Response
  26.      */
  27.     public function recovery(
  28.         Request $request,
  29.         TranslatorInterface $translator,
  30.         ApiServicePortalService $apiServicePortalService
  31.     ) {
  32.         /* Check if some user is logged */
  33.         if ($this->getUser()) {
  34.             return $this->redirectToRoute('login');
  35.         }
  36.         $form $this->createForm(RecoveryPasswordType::class);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             try {
  40.                 $username $form->get('username')->getData();
  41.                 $resource sprintf('user/generate-recovery-token/%s/%s'$usernameUser::ROLE_PROVIDER);
  42.                 $response $apiServicePortalService->apiRequest(ApiInterface::PATCH$resource);
  43.                 if (ApiInterface::API_SUCCESS_CODE_200 == $response[ApiInterface::RESPONSE_CODE]) {
  44.                     return $this->redirectToRoute('password_recovery_confirm');
  45.                 }
  46.             } catch (Exception $exception) {
  47.                 $this->addFlash('danger'$translator->trans('password.recovery.error', [], 'password'));
  48.             }
  49.         }
  50.         return $this->render('password/recovery.html.twig', [
  51.             'form' => $form->createView()
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/recovery-confirm", name="recovery_confirm")
  56.      * @return Response
  57.      */
  58.     public function recoveryPasswordConfirm(): Response
  59.     {
  60.         /* Check if some user is logged */
  61.         if ($this->getUser()) {
  62.             return $this->redirectToRoute('login');
  63.         }
  64.         return $this->render('password/recovery_confirm.html.twig');
  65.     }
  66.     /**
  67.      * @Route("/recovery-reset", name="recovery_reset")
  68.      * @param Request $request
  69.      * @param TranslatorInterface $translator
  70.      * @param ApiServicePortalService $apiServicePortalService
  71.      * @return Response
  72.      */
  73.     public function recoveryReset(
  74.         Request $request,
  75.         TranslatorInterface $translator,
  76.         ApiServicePortalService $apiServicePortalService
  77.     ) {
  78.         /* Check if some user is logged */
  79.         if ($this->getUser()) {
  80.             return $this->redirectToRoute('logout');
  81.         }
  82.         $token $request->get('token');
  83.         if (!$token) {
  84.             $this->addFlash('error'$translator->trans('password.recovery_reset.token_not_valid', [], 'password'));
  85.             return $this->redirectToRoute('login');
  86.         }
  87.         $resource sprintf('user/check-recovery-token/%s/%s'$tokenUser::ROLE_PROVIDER);
  88.         $response $apiServicePortalService->apiRequest(ApiInterface::GET$resource);
  89.         if (ApiInterface::API_SUCCESS_CODE_200 != $response[ApiInterface::RESPONSE_CODE]) {
  90.             $this->addFlash('danger'$translator->trans('password.recovery_reset.user_not_found', [], 'password'));
  91.             return $this->redirectToRoute('login');
  92.         }
  93.         $uuid $response[ApiInterface::RESPONSE_DATA]['user']['uuid'];
  94.         $email $response[ApiInterface::RESPONSE_DATA]['user']['email'];
  95.         $form $this->createForm(UserPasswordType::class);
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             $resource sprintf('user/change-password/%s'$uuid);
  99.             $newPassword $form->getData()['password'];
  100.             $data = ["op" => "replace""path" => "password""value" => $newPassword];
  101.             $response $apiServicePortalService->apiRequest(ApiInterface::PATCH$resource$data);
  102.             $badPass false;
  103.             if (ApiInterface::API_ERROR_CODE_401 == $response[ApiInterface::RESPONSE_CODE]) {
  104.                 $badPass true;
  105.                 $this->addFlash(
  106.                     'warning',
  107.                     $response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE]
  108.                 );
  109.                 return $this->redirectToRoute('password_recovery_reset', [
  110.                     'token' => $token
  111.                 ]);
  112.             }
  113.             if (ApiInterface::API_SUCCESS_CODE_200 != $response[ApiInterface::RESPONSE_CODE] && !$badPass) {
  114.                 $this->addFlash(
  115.                     'danger',
  116.                     $translator->trans('password.recovery.error', [], 'password')
  117.                 );
  118.                 return $this->redirectToRoute('password_recovery_reset', [
  119.                     'token' => $token
  120.                 ]);
  121.             }
  122.             $this->addFlash('success'$translator->trans('password.recovery_reset.success', [], 'password'));
  123.             return $this->redirectToRoute('login');
  124.         }
  125.         return $this->render('password/recovery_reset.html.twig', [
  126.             'user' => $email,
  127.             'form' => $form->createView()
  128.         ]);
  129.     }
  130. }