src/Controller/SecurityController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\User\UserForgottenPasswordType;
  4. use App\Form\User\UserResetPasswordType;
  5. use App\Manager\TokenManager;
  6. use App\Model\Operation\Draw;
  7. use App\Model\Token;
  8. use App\Service\APIKiwi;
  9. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  10. use KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class SecurityController extends APIController
  19. {
  20.     /**
  21.      * @Route("/connexion", name="app_login")
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtils): Response
  24.     {
  25.         $this->redirectionDraw();
  26.         if ($this->getUser()) {
  27.             return $this->redirectToRoute('kiwi_homepage');
  28.         }
  29.         // get the login error if there is one
  30.         $error $authenticationUtils->getLastAuthenticationError();
  31.         if ($error) {
  32.             $error 'Email ou mot de passe incorrect';
  33.         }
  34.         // last username entered by the user
  35.         $lastUsername $authenticationUtils->getLastUsername();
  36.         return $this->render('security/login.html.twig', [
  37.             'last_username' => $lastUsername,
  38.             'error' => $error,
  39.             'target_path' => $this->requestStack->getSession()->get('_target_path'),
  40.             'target_path_params' => $this->requestStack->getSession()->get('_target_path_params'),
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/logout", name="app_logout")
  45.      */
  46.     public function logout()
  47.     {
  48.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  49.     }
  50.     /**
  51.      * @Route("/mot-de-passe-oublie", name="kiwi_forgotten_password_user")
  52.      */
  53.     public function forgottenPassword(Request $requestAPIKiwi $APIKiwi)
  54.     {
  55.         $form $this->createForm(UserForgottenPasswordType::class);
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             $data $form->getData();
  59.             $response $APIKiwi->postPublicKiwi([
  60.                 'path' => '/public/request-reset-password',
  61.                 'data' => [
  62.                     'templateId' => $_ENV['TEMPLATE_FORGOTTEN_PASSWORD_ID'],
  63.                     'email' => $data->getEmail(),
  64.                     'url' => $request->getSchemeAndHttpHost().'/reinitialiser-mot-de-passe/'
  65.                 ]
  66.             ]);
  67.             $error null;
  68.             if ($response->getStatusCode() === 200) {
  69.                 $error json_decode($response->getContent(), true)['error'];
  70.             }
  71.             return $this->render('user/success_forgotten_password.html.twig', ['error' => $error]);
  72.         }
  73.         return $this->render('user/forgotten_password.html.twig', [
  74.             'form' => $form->createView(),
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/reinitialiser-mot-de-passe/{token}", name="kiwi_reinit_password_user")
  79.      */
  80.     public function resetPassword(Request $requestAPIKiwi $APIKiwi$tokenTokenManager $tokenManagerTranslatorInterface $translator)
  81.     {
  82.         $data $APIKiwi->getPublicKiwi('tokens/'.$token);
  83.         $token $this->deserialize($data->getContent(), Token::class, 'get_token');
  84.         if (!$tokenManager->checkValidityToken($token)) {
  85.             return $this->render('user/expired_token.html.twig');
  86.         }
  87.         $form $this->createForm(UserResetPasswordType::class);
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             $data $form->getData();
  91.             $url sprintf(
  92.                 '/users/%s/tokens/%s',
  93.                 $token->getUser()->getId(),
  94.                 $token->getId()
  95.             );
  96.             $APIKiwi->putPublicKiwi(
  97.                 $url,
  98.                 [
  99.                     'data' => [
  100.                         'plainPassword' => $data->getPlainPassword(),
  101.                 ]
  102.             ]);
  103.             $this->addFlash('success'$translator->trans('default.forgotten_password.success.reset_password'));
  104.             return $this->redirectToRoute('app_login');
  105.         }
  106.         return $this->render('user/reset_password.html.twig', [
  107.             'form' => $form->createView(),
  108.         ]);
  109.     }
  110.     private function redirectionDraw()
  111.     {
  112.         if ('kiwi_operation_draw_show' ===  $this->requestStack->getSession()->get('_target_path')) {
  113.             $drawResp $this->APIKiwi->getPublicKiwi('draw/'$this->requestStack->getSession()->get('kiwi_operation_draw_id'));
  114.             $draw $this->deserialize($drawResp->getContent(), Draw::class, 'get_draw');
  115.             if (Draw::DRAW_TYPE_DEMAT === $draw->getDrawType()) {
  116.                  $this->requestStack->getSession()->set('_target_path''kiwi_participation_confirm_register_participation_draw');
  117.             } else {
  118.                  $this->requestStack->getSession()->set('_target_path''kiwi_operation_draw_address_delivery_user');
  119.             }
  120.         }
  121.     }
  122.     /**
  123.      * @param ClientRegistry $clientRegistry
  124.      * @Route("/connect/facebook", name="facebook_connect")
  125.      */
  126.     public function connect(ClientRegistry $clientRegistry): RedirectResponse
  127.     {
  128.         /** @var FacebookClient $client */
  129.         $client $clientRegistry->getClient('facebook');
  130.         return $client->redirect([
  131.             'public_profile''email''hometown'
  132.         ]);
  133.     }
  134.     /**
  135.      * @Route("/supprimer-compte/", name="kiwi_soft_delete_user")
  136.      */
  137.     public function softDelete()
  138.     {
  139.         $userId $this->getUser()->getId();
  140.         $templateId $_ENV['TEMPLATE_DELETE_ACCOUNT_ID'];
  141.         try {
  142.             $this->APIKiwi->postKiwi([
  143.                 'path' => sprintf('/users/softdelete/%s/%s'$userId$templateId),
  144.                 'token' => $this->getUser()->getToken(),
  145.                 'templateId' => $templateId,
  146.             ]);
  147.         } catch (\Exception $exception) {
  148.             throw new BadRequestHttpException($exception->getMessage());
  149.         }
  150.         return $this->redirectToRoute('app_logout');
  151.     }
  152.     /**
  153.      * @Route("/telecharger-mes-donnees/", name="kiwi_download_personnal_data_user")
  154.      */
  155.     public function downloadpersonalData()
  156.     {
  157.         $userId $this->getUser()->getId();
  158.         try {
  159.             $response $this->APIKiwi->getKiwi([
  160.                 'path' => sprintf('/users/download-personal-data/%s'$userId),
  161.                 'token' => $this->getUser()->getToken(),
  162.             ]);
  163.         } catch (\Exception $exception) {
  164.             throw new BadRequestHttpException($exception->getMessage());
  165.         }
  166.         return $this->redirect(json_decode($response->getContent(), true)['url']);
  167.     }
  168. }