src/Controller/BackEnd/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BackEnd;
  3. use App\Entity\Site;
  4. use App\Entity\User;
  5. use App\Entity\HistoriqueConnexion;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. class SecurityController extends AbstractController
  17. {
  18. // bloc login from crm
  19. /**
  20. * @Route("/login", name="app_login")
  21. */
  22. public function login(Request $request, AuthenticationUtils $authenticationUtils,EntityManagerInterface $em): Response
  23. {
  24. // Si déjà connecté
  25. if ($this->getUser()) {
  26. return $this->redirectToRoute('admin_dashbord');
  27. }
  28. // Vérifier token,email et host
  29. $host="";
  30. $token = $request->query->get('token');
  31. $email = $request->query->get('email');
  32. $referer = $request->headers->get('referer');
  33. if ($referer) {
  34. $host = parse_url($referer, PHP_URL_HOST);
  35. }
  36. if ($token === 'A7kP9xQ2Lm' && $host === 'crm.blue-web-agency.fr') {
  37. $user = $em->getRepository(User::class)->findOneBy([
  38. 'email' => $email
  39. ]);
  40. if ($user) {
  41. // connexion utilisateur
  42. $securityToken = new UsernamePasswordToken(
  43. $user,
  44. null,
  45. 'main', // firewall name
  46. $user->getRoles()
  47. );
  48. $this->container->get('security.token_storage')->setToken($securityToken);
  49. $request->getSession()->set('_security_main', serialize($securityToken));
  50. return $this->redirectToRoute('admin_dashbord');
  51. }
  52. }
  53. // login classique
  54. $error = $authenticationUtils->getLastAuthenticationError();
  55. $lastUsername = $authenticationUtils->getLastUsername();
  56. return $this->render('admin/authentification/login.html.twig', [
  57. 'last_username' => $lastUsername,
  58. 'error' => $error
  59. ]);
  60. }
  61. // end bloc login from crm
  62. /**
  63. * @Route("/logout", name="app_logout")
  64. */
  65. public function logout()
  66. {
  67. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  68. }
  69. }