src/Controller/ResetPasswordController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use App\Service\ResetPasswordService;
  9. use App\Entity\User;
  10. use App\Service\UserService;
  11. class ResetPasswordController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/dealer/first-access-request", name="dealer_first_access_request", methods={"POST"})
  15.      */
  16.     public function dealerFirstAccessRequest(Request $requestResetPasswordService $resetPasswordServiceUserService $userService): Response
  17.     {
  18.         $data json_decode($request->getContent(), true);
  19.         if (!isset($data['id'])) {
  20.             return $this->JsonErrorResponse('Utente non trovato');
  21.         }
  22.         $user $resetPasswordService->getUserById($data['id']);
  23.         if(!$user){
  24.             return $this->JsonErrorResponse('Utente non trovato');
  25.         }
  26.         $userService->sendNewDealerUserEmail($user);
  27.         if($userService->hasErrors()){
  28.             return $this->JsonErrorResponse('Errore invio richiesta e-mail');
  29.         }
  30.         return $this->JsonOkResponse();
  31.     }
  32.     /**
  33.      * @Route("/dealer/reset-password-request", name="dealer_reset_password_request", methods={"POST"})
  34.      */
  35.     public function dealerResetPasswordRequest(Request $requestResetPasswordService $resetPasswordService): Response
  36.     {
  37.         $data json_decode($request->getContent(), true);
  38.         if (!isset($data['id'])) {
  39.             return $this->JsonErrorResponse('Utente non trovato');
  40.         }
  41.         $user $resetPasswordService->getUserById($data['id']);
  42.         if(!$user){
  43.             return $this->JsonErrorResponse('Utente non trovato');
  44.         }
  45.         $result $resetPasswordService->sendResetPasswordEmail($user);
  46.         if ($result != true) {
  47.             return $this->JsonErrorResponse('Errore invio richiesta e-mail');
  48.         }
  49.         return $this->JsonOkResponse();
  50.     }
  51.     /**
  52.      * @Route("/reset-password", name="reset_password", methods={"GET", "POST"})
  53.      */
  54.     public function resetPassword(Request $requestResetPasswordService $resetPasswordService): Response
  55.     {
  56.         //rotta per gestire il reset della password da parte dell'utente in autonomia
  57.         $user $this->get('security.token_storage')->getToken()->getUser();
  58.         if($user instanceof User){
  59.             //effettua il logout manuale
  60.             $this->get('security.token_storage')->setToken(null);
  61.             $request->getSession()->invalidate();
  62.         }
  63.         $email null;
  64.         $error null;
  65.         $showForm true;
  66.         if($request->getMethod() == 'POST'){
  67.             try {
  68.                 $authUsername $request->request->get('auth_username');
  69.                 if(!$authUsername){
  70.                     throw new \RuntimeException('Inserire un indirizzo email o un numero di partita IVA valido.');
  71.                 }
  72.                 $isEmail filter_var($authUsernameFILTER_VALIDATE_EMAIL);
  73.                 if ($isEmail) {
  74.                     $email $authUsername;
  75.                     $user $resetPasswordService->getUserByEmail($email);
  76.                 }else{
  77.                     $user $resetPasswordService->getUserByAuthUsername($authUsername);
  78.                 }
  79.                 if(!$user){
  80.                     throw new \RuntimeException('Utente non trovato.');
  81.                 }
  82.                 $emailSent $resetPasswordService->sendResetPasswordEmail($user$email);
  83.                 if(!$emailSent){
  84.                     throw new \RuntimeException('Errore invio richiesta e-mail');
  85.                 }
  86.                 $showForm false;
  87.             } catch (\Exception $e) {
  88.                 $error $e->getMessage();
  89.             }
  90.         }
  91.         //rotta pubblica per il cambio password dell'utente in autonomia
  92.         return $this->render('reset-password-email.html.twig', [
  93.             'email' => $email ?: '',
  94.             'error' => $error,
  95.             'showForm' => $showForm
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/reset-password-form", name="reset_password_form", methods={"GET"})
  100.      */
  101.     public function resetPasswordForm(Request $requestResetPasswordService $resetPasswordService): Response
  102.     {
  103.         $error null;
  104.         $showForm true;
  105.         $code $request->query->get('c');
  106.         if (!$code) {
  107.             $error 'La richiesta di ripristino password non è valida.<br>Controlla il link corretto nella email.';
  108.             $showForm false;
  109.         }
  110.         if(!$error){
  111.             $user $resetPasswordService->getUserByResetCode($code);
  112.             if (!$user) {
  113.                 //errore generico utente non trovato
  114.                 $error 'La richiesta di cambio password non è valida.<br>Effettuare una nuova richiesta.';
  115.                 $showForm false;
  116.             }
  117.         }
  118.         if(!$error){
  119.             //check scadenza richiesta password
  120.             $resetPasswordDate $user->getResetPasswordDate();
  121.             $now = new \DateTime();
  122.             $passwordDateValid $resetPasswordDate->diff($now)->days <= 1;
  123.             if(!$passwordDateValid){
  124.                 $error 'La richiesta di ripristino password è scaduta. Devi effettuare una nuova richiesta.';
  125.                 $showForm false;
  126.             }
  127.         }
  128.         return $this->render('reset-password-form.html.twig', [
  129.             'showForm' => $showForm,
  130.             'code' => $code,
  131.             'error' => $error,
  132.         ]);
  133.     }
  134.     /**
  135.      * @Route("/reset-password-submit", name="reset_password_submit",methods={"POST"})
  136.      */
  137.     public function resetPasswordSubmit(Request $requestResetPasswordService $resetPasswordService): Response
  138.     {
  139.         $code $request->request->get('code');
  140.         $password $request->request->get('password');
  141.         $passwordConfirm $request->request->get('password_confirm');
  142.         if(!$code){
  143.             throw $this->createNotFoundException('404');
  144.         }
  145.         $error null;
  146.         $showForm true;
  147.         if($password != $passwordConfirm){
  148.             $error 'La conferma password non corrisponde. Inserisci nuovamente la password.';
  149.         }
  150.         if(!$error){
  151.             $checkPassword $resetPasswordService->checkPassword($password);
  152.             if($checkPassword['valid'] == false){
  153.                 //$error = implode('<br>', $checkPassword['errors']);
  154.                 $error 'La password non rispetta i requisiti di sicurezza.';
  155.             }
  156.         }
  157.         if(!$error){
  158.             $resetPasswordService->saveNewPassword($code$password);
  159.             $showForm false;
  160.             //forza logout utente
  161.             $user $this->get('security.token_storage')->getToken()->getUser();
  162.             if($user instanceof User){
  163.                 //effettua il logout manuale
  164.                 $this->get('security.token_storage')->setToken(null);
  165.                 $request->getSession()->invalidate();
  166.             }
  167.         }
  168.         return $this->render('reset-password-form.html.twig', [
  169.             'showForm' => $showForm,
  170.             'code' => $code,
  171.             'error' => $error,
  172.         ]);
  173.     }
  174.     /**
  175.      * @Route("/partner/reset-password", name="partner_reset_password", methods={"POST"})
  176.      */
  177.     public function partnerResetPassword(Request $requestResetPasswordService $resetPasswordService): Response
  178.     {
  179.         //cambio password dal profilo dell'utente partner
  180.         try {
  181.             $user $this->get('security.token_storage')->getToken()->getUser();
  182.             if(!$user){
  183.                 throw new \RuntimeException('Utente non trovato.');
  184.             }
  185.             $sent $resetPasswordService->sendResetPasswordEmail($user);
  186.             if($sent != true){
  187.                 throw new \RuntimeException('Errore invio richiesta e-mail.');
  188.             }
  189.         } catch (\Exception $e) {
  190.             return $this->JsonErrorResponse($e->getMessage());
  191.         }
  192.         return $this->JsonOkResponse();
  193.     }
  194.     private function JsonOkResponse($data = [])
  195.     {
  196.         return new Response(json_encode([
  197.             'result' => 'OK',
  198.             'data' => $data,
  199.             'errmsg' => '',
  200.         ]));
  201.     }
  202.     private function JsonErrorResponse($errmsg)
  203.     {
  204.         $json json_encode([
  205.             'result' => 'ERROR',
  206.             'errmsg' => $errmsg,
  207.         ]);
  208.         $response = new Response($json);
  209.         $response->setStatusCode(500);
  210.         return $response;
  211.     }
  212. }