src/Controller/SecurityController.php line 273

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Resource;
  4. use App\Form\AskRecoveryType;
  5. use App\Form\RecoverType;
  6. use App\Form\ResourceRegisterType;
  7. use App\Service\CaptchaService;
  8. use App\Service\FormService;
  9. use App\Service\ResourceService;
  10. use App\Service\VoiceService;
  11. use DateTime;
  12. use Exception;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\File\UploadedFile;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. use Symfony\Component\String\ByteString;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class SecurityController extends AbstractController
  23. {
  24.     const DEFAULT_ROUTE 'home';
  25.     private $translator;
  26.     public function __construct(TranslatorInterface $translator)
  27.     {
  28.         $this->translator $translator;
  29.     }
  30.     public function nolocale(Request $request): RedirectResponse
  31.     {
  32.         $prefered $request->getPreferredLanguage(explode('|'$this->getParameter('supported_locales')));
  33.         if ($prefered == null) {
  34.             $prefered 'en';
  35.         }
  36.         return $this->redirectToRoute('login', ['_locale' => $prefered]);
  37.     }
  38.     public function registernolocale(Request $request): RedirectResponse
  39.     {
  40.         $prefered $request->getPreferredLanguage(explode('|'$this->getParameter('supported_locales')));
  41.         if ($prefered == null) {
  42.             $prefered 'en';
  43.         }
  44.         return $this->redirectToRoute('register', ['_locale' => $prefered]);
  45.     }
  46.     public function askRecoverynolocale(Request $request): RedirectResponse
  47.     {
  48.         $prefered $request->getPreferredLanguage(explode('|'$this->getParameter('supported_locales')));
  49.         if ($prefered == null) {
  50.             $prefered 'en';
  51.         }
  52.         return $this->redirectToRoute('ask_recovery', ['_locale' => $prefered]);
  53.     }
  54.     public function recovernolocale(Request $requeststring $token): RedirectResponse
  55.     {
  56.         $prefered $request->getPreferredLanguage(explode('|'$this->getParameter('supported_locales')));
  57.         if ($prefered == null) {
  58.             $prefered 'en';
  59.         }
  60.         return $this->redirectToRoute('recover', [
  61.             'token' => $token,
  62.             '_locale' => $prefered,
  63.         ]);
  64.     }
  65.     public function createPasswordnolocale(Request $requeststring $token): RedirectResponse
  66.     {
  67.         $prefered $request->getPreferredLanguage(explode('|'$this->getParameter('supported_locales')));
  68.         if ($prefered == null) {
  69.             $prefered 'en';
  70.         }
  71.         return $this->redirectToRoute('create_password', [
  72.             'token' => $token,
  73.             '_locale' => $prefered,
  74.         ]);
  75.     }
  76.     public function login(AuthenticationUtils $authUtils): Response
  77.     {
  78.         if ($this->getUser() instanceof Resource) {
  79.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  80.         }
  81.         $error $authUtils->getLastAuthenticationError();
  82.         $lastUsername $authUtils->getLastUsername();
  83.         return $this->render('security/login.html.twig', [
  84.             'last_username' => $lastUsername,
  85.             'error' => $error,
  86.         ]);
  87.     }
  88.     public function register(CaptchaService $captchaService): Response
  89.     {
  90.         if ($this->getUser() instanceof Resource) {
  91.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  92.         }
  93.         $form $this->createForm(ResourceRegisterType::class);
  94.         return $this->render('security/register.html.twig', [
  95.             'form' => $form->createView(),
  96.             'sitekey' => $captchaService->getSitekey(),
  97.         ]);
  98.     }
  99.     /**
  100.      * @throws Exception
  101.      */
  102.     public function registerSave(Request $requestResourceService $utilFormService $fUtilVoiceService $voiceServiceCaptchaService $captchaService): Response
  103.     {
  104.         $resource = new Resource();
  105.         $form $this->createForm(ResourceRegisterType::class, $resource);
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             /**
  109.              * @var UploadedFile $sample
  110.              */
  111.             $sample $form->get('sample_file')->getData();
  112.             $recaptchaToken $request->request->get('g-recaptcha-response');
  113.             if (!$captchaService->verifyCaptcha($recaptchaToken)) {
  114.                 try {
  115.                     unlink($sample->getRealPath());
  116.                 } catch (Exception $e) {
  117.                 }
  118.                 return new JsonResponse([
  119.                     'errors' => [
  120.                         'recaptcha' => $this->translator->trans('The form is invalid, please refresh the page and try again'),
  121.                     ],
  122.                 ]);
  123.             }
  124.             $sponsorCode $form->get('sponsor_code')->getData();
  125.             $sponsor $util->getResourceBySponsorCode($sponsorCode);
  126.             $resource->setRegisteredAt(new DateTime());
  127.             $resource->setPassword('');
  128.             $resource->setAccentHidden($resource->getAccent());
  129.             $resource->setRole('ROLE_RESOURCE');
  130.             $resource->setStatus(Resource::STATUS_VALIDABLE);
  131.             $resource->setComptaStatus(Resource::STATUS_COMPTA_NONE);
  132.             $resource->setSponsorCode(ByteString::fromRandom(10'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'));
  133.             $resource->setSponsor($sponsor);
  134.             $resource->setPhoneLookup(false);
  135.             $resource->setOptOut(false);
  136.             $resource->setIsAgent(false);
  137.             $em $this->getDoctrine()->getManager();
  138.             $em->persist($resource);
  139.             $em->flush();
  140.             $resource->setTra(null);
  141.             $voiceFile $voiceService->saveSample($sample$resource->getId());
  142.             $resource->setSample($voiceFile);
  143.             $em->flush();
  144.             $util->sendPasswordCreation($resource);
  145.             return $this->redirectToRoute('register_success', [
  146.                 'resource' => $resource->getId(),
  147.             ]);
  148.         }
  149.         return new JsonResponse([
  150.             'errors' => $fUtil->getErrorMessages($form),
  151.         ]);
  152.     }
  153.     public function registerSuccess(Resource $resource): Response
  154.     {
  155.         return $this->render('security/register_success.html.twig', [
  156.             'email' => $resource->getEmail(),
  157.         ]);
  158.     }
  159.     public function askRecovery(Request $requestResourceService $util): Response
  160.     {
  161.         if ($this->getUser()) {
  162.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  163.         }
  164.         $form $this->createForm(AskRecoveryType::class);
  165.         $form->handleRequest($request);
  166.         if ($form->isSubmitted() && $form->isValid()) {
  167.             $data $form->getData();
  168.             $user $util->getResourceByEmail($data['email']);
  169.             if ($user !== null) {
  170.                 $util->sendRecovery($user);
  171.             }
  172.             return $this->render('security/recover_sent.html.twig');
  173.         }
  174.         return $this->render('security/ask_recovery.html.twig', [
  175.             'form' => $form->createView(),
  176.             'error' => false,
  177.         ]);
  178.     }
  179.     public function recover(Request $requeststring $tokenResourceService $util): Response
  180.     {
  181.         if ($this->getUser()) {
  182.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  183.         }
  184.         if ($token === null) {
  185.             return $this->render('security/recover_invalid_token.html.twig');
  186.         }
  187.         $user $util->getRecoverableResource($token);
  188.         if ($user === null) {
  189.             return $this->render('security/recover_invalid_token.html.twig');
  190.         }
  191.         $form $this->createForm(RecoverType::class);
  192.         $form->handleRequest($request);
  193.         if ($form->isSubmitted() && $form->isValid()) {
  194.             $data $form->getData();
  195.             $util->changePassword($user$data['password']);
  196.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  197.         }
  198.         return $this->render('security/recover.html.twig', [
  199.             'form' => $form->createView(),
  200.         ]);
  201.     }
  202.     public function createPassword(Request $requeststring $tokenResourceService $util): Response
  203.     {
  204.         if ($this->getUser()) {
  205.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  206.         }
  207.         if ($token === null) {
  208.             return $this->render('security/create_password_invalid_token.html.twig');
  209.         }
  210.         $user $util->getRecoverableResource($token);
  211.         if ($user === null) {
  212.             return $this->render('security/create_password_invalid_token.html.twig');
  213.         }
  214.         $form $this->createForm(RecoverType::class);
  215.         $form->handleRequest($request);
  216.         if ($form->isSubmitted() && $form->isValid()) {
  217.             $data $form->getData();
  218.             $util->changePassword($user$data['password']);
  219.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  220.         }
  221.         return $this->render('security/create_password.html.twig', [
  222.             'form' => $form->createView(),
  223.         ]);
  224.     }
  225.     public function tra(): Response
  226.     {
  227.         return $this->render('security/tra.html.twig');
  228.     }
  229. }