src/Controller/SecurityController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\VisitorEmailType;
  5. use App\Repository\UserRepository;
  6. use App\Service\Encryption;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Notifier\NotifierInterface;
  12. use Symfony\Component\Notifier\Recipient\Recipient;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  16. use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
  17. use \Symfony\Component\HttpFoundation\Cookie;
  18. class SecurityController extends AbstractController
  19. {
  20. private Encryption $encryption;
  21. public function __construct(Encryption $encryption)
  22. {
  23. $this->encryption = $encryption;
  24. }
  25. /**
  26. * @Route("/connexion", name="app_login")
  27. */
  28. public function login(AuthenticationUtils $authenticationUtils): Response
  29. {
  30. // if ($this->getUser()) {
  31. // return $this->redirectToRoute('target_path');
  32. // }
  33. // if ($request->isMethod('POST')) {
  34. // $email = $request->request->get('email');
  35. // $user = $userRepository->findOneBy(['email' => $email]);
  36. // $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
  37. // // create a notification based on the login link details
  38. // $notification = new LoginLinkNotification(
  39. // $loginLinkDetails,
  40. // 'Welcome to MY WEBSITE!' // email subject
  41. // );
  42. // // create a recipient for this user
  43. // // $recipient = new Recipient($user->getEmail());
  44. // $recipient = new Recipient("doumbiayacouba055@gmail.com");
  45. // // send the notification to the user
  46. // $notifier->send($notification, $recipient);
  47. // // render a "Login link is sent!" page
  48. // return $this->render('security/login_link_sent.html.twig');
  49. // }
  50. // get the login error if there is one
  51. $error = $authenticationUtils->getLastAuthenticationError();
  52. // last username entered by the user
  53. $lastUsername = $authenticationUtils->getLastUsername();
  54. return $this->render('security/login.html.twig', [
  55. 'last_username' => $lastUsername,
  56. 'error' => $error,
  57. ]);
  58. }
  59. // /**
  60. // * @Route("/visiteur", name="app_front_visitor_registration_page")
  61. // */
  62. // public function requestLoginLink(NotifierInterface $notifier, LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request)
  63. // {
  64. // //$entityManager = $doctrine->getManager();
  65. // $user = new User();
  66. // $form = $this->createForm(VisitorEmailType::class, $user);
  67. // $form->handleRequest($request);
  68. // if ($form->isSubmitted() && $form->isValid()) {
  69. // // load the user in some way (e.g. using the form input)
  70. // $email = $request->request->get('email');
  71. // $user = $userRepository->findOneBy(['email' => $email]);
  72. // // create a login link for $user this returns an instance
  73. // // of LoginLinkDetails
  74. // $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
  75. // //$loginLink = $loginLinkDetails->getUrl();
  76. // // create a notification based on the login link details
  77. // $notification = new LoginLinkNotification(
  78. // $loginLinkDetails,
  79. // 'Welcome to MY WEBSITE!' // email subject
  80. // );
  81. // // create a recipient for this user
  82. // $recipient = new Recipient($user->getEmail());
  83. // // send the notification to the user
  84. // $notifier->send($notification, $recipient);
  85. // // render a "Login link is sent!" page
  86. // return $this->redirectToRoute('app_front_visitor_details_page');
  87. // }
  88. // return $this->render('front/pages/authentication/visitor_registration_details_page.html.twig', [
  89. // 'form' => $form->createView(),
  90. // ]);
  91. // }
  92. /**
  93. * @Route("/logout", name="app_logout")
  94. */
  95. public function logout(): void
  96. {
  97. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  98. }
  99. /**
  100. * @Route("/login_check", name="login_check")
  101. */
  102. public function check()
  103. {
  104. throw new \LogicException('This code should never be reached');
  105. }
  106. /**
  107. * @Route("/config/firebase", name="firebase_config")
  108. */
  109. public function firebaseConfig(Request $request)
  110. {
  111. $encryptedData = [
  112. 'apiKey' => $_ENV['FIREBASE_API_KEY'],
  113. 'authDomain' => $_ENV['FIREBASE_AUTH_DOMAIN'],
  114. 'projectId' => $_ENV['FIREBASE_PROJECT_ID'],
  115. 'storageBucket' => $_ENV['FIREBASE_STORAGE_BUCKET'],
  116. 'messagingSenderId' => $_ENV['FIREBASE_MESSAGING_SENDER_ID'],
  117. 'appId' => $_ENV['FIREBASE_APP_ID'],
  118. 'measurementId' => $_ENV['FIREBASE_MEASUREMENT_ID'],
  119. 'vapidKey' => $_ENV['FIREBASE_VAPID_KEY'],
  120. ];
  121. if ($request->query->get('sw') != "1") {
  122. $dataToEncrypt = json_encode($encryptedData);
  123. $encryptedData = $this->encryption->encrypt($dataToEncrypt);
  124. $session = $request->getSession();
  125. setcookie('firebase_config_iv', $encryptedData['iv'], time() + (86400 * 30), "/");
  126. setcookie('firebase_config_data', $encryptedData['data'], time() + (86400 * 30), "/");
  127. $session->set('firebase_config_iv', $encryptedData['iv']);
  128. $session->set('firebase_config_data', $encryptedData['data']);
  129. }
  130. return new JsonResponse($encryptedData);
  131. }
  132. }