<?php
namespace App\Controller;
use Exception;
use App\Security\User;
use App\Service\TokenService;
use App\Form\UserPasswordType;
use App\Form\RecoveryPasswordType;
use App\Service\Api\ApiServicePortalService;
use App\Service\Interfaces\ApiInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/password", name="password_")
*/
class PasswordController extends AbstractController
{
/**
* @Route("/recovery", name="recovery")
* @param Request $request
* @param UserRepository $userRepository
* @param TranslatorInterface $translator
* @return RedirectResponse|Response
*/
public function recovery(
Request $request,
TranslatorInterface $translator,
ApiServicePortalService $apiServicePortalService
) {
/* Check if some user is logged */
if ($this->getUser()) {
return $this->redirectToRoute('login');
}
$form = $this->createForm(RecoveryPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$username = $form->get('username')->getData();
$resource = sprintf('user/generate-recovery-token/%s/%s', $username, User::ROLE_PROVIDER);
$response = $apiServicePortalService->apiRequest(ApiInterface::PATCH, $resource);
if (ApiInterface::API_SUCCESS_CODE_200 == $response[ApiInterface::RESPONSE_CODE]) {
return $this->redirectToRoute('password_recovery_confirm');
}
} catch (Exception $exception) {
$this->addFlash('danger', $translator->trans('password.recovery.error', [], 'password'));
}
}
return $this->render('password/recovery.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/recovery-confirm", name="recovery_confirm")
* @return Response
*/
public function recoveryPasswordConfirm(): Response
{
/* Check if some user is logged */
if ($this->getUser()) {
return $this->redirectToRoute('login');
}
return $this->render('password/recovery_confirm.html.twig');
}
/**
* @Route("/recovery-reset", name="recovery_reset")
* @param Request $request
* @param TranslatorInterface $translator
* @param ApiServicePortalService $apiServicePortalService
* @return Response
*/
public function recoveryReset(
Request $request,
TranslatorInterface $translator,
ApiServicePortalService $apiServicePortalService
) {
/* Check if some user is logged */
if ($this->getUser()) {
return $this->redirectToRoute('logout');
}
$token = $request->get('token');
if (!$token) {
$this->addFlash('error', $translator->trans('password.recovery_reset.token_not_valid', [], 'password'));
return $this->redirectToRoute('login');
}
$resource = sprintf('user/check-recovery-token/%s/%s', $token, User::ROLE_PROVIDER);
$response = $apiServicePortalService->apiRequest(ApiInterface::GET, $resource);
if (ApiInterface::API_SUCCESS_CODE_200 != $response[ApiInterface::RESPONSE_CODE]) {
$this->addFlash('danger', $translator->trans('password.recovery_reset.user_not_found', [], 'password'));
return $this->redirectToRoute('login');
}
$uuid = $response[ApiInterface::RESPONSE_DATA]['user']['uuid'];
$email = $response[ApiInterface::RESPONSE_DATA]['user']['email'];
$form = $this->createForm(UserPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$resource = sprintf('user/change-password/%s', $uuid);
$newPassword = $form->getData()['password'];
$data = ["op" => "replace", "path" => "password", "value" => $newPassword];
$response = $apiServicePortalService->apiRequest(ApiInterface::PATCH, $resource, $data);
$badPass = false;
if (ApiInterface::API_ERROR_CODE_401 == $response[ApiInterface::RESPONSE_CODE]) {
$badPass = true;
$this->addFlash(
'warning',
$response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE]
);
return $this->redirectToRoute('password_recovery_reset', [
'token' => $token
]);
}
if (ApiInterface::API_SUCCESS_CODE_200 != $response[ApiInterface::RESPONSE_CODE] && !$badPass) {
$this->addFlash(
'danger',
$translator->trans('password.recovery.error', [], 'password')
);
return $this->redirectToRoute('password_recovery_reset', [
'token' => $token
]);
}
$this->addFlash('success', $translator->trans('password.recovery_reset.success', [], 'password'));
return $this->redirectToRoute('login');
}
return $this->render('password/recovery_reset.html.twig', [
'user' => $email,
'form' => $form->createView()
]);
}
}