<?php
namespace App\Controller\BackEnd;
use App\Entity\Site;
use App\Entity\User;
use App\Entity\HistoriqueConnexion;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class SecurityController extends AbstractController
{
// bloc login from crm
/**
* @Route("/login", name="app_login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils,EntityManagerInterface $em): Response
{
// Si déjà connecté
if ($this->getUser()) {
return $this->redirectToRoute('admin_dashbord');
}
// Vérifier token,email et host
$host="";
$token = $request->query->get('token');
$email = $request->query->get('email');
$referer = $request->headers->get('referer');
if ($referer) {
$host = parse_url($referer, PHP_URL_HOST);
}
if ($token === 'A7kP9xQ2Lm' && $host === 'crm.blue-web-agency.fr') {
$user = $em->getRepository(User::class)->findOneBy([
'email' => $email
]);
if ($user) {
// connexion utilisateur
$securityToken = new UsernamePasswordToken(
$user,
null,
'main', // firewall name
$user->getRoles()
);
$this->container->get('security.token_storage')->setToken($securityToken);
$request->getSession()->set('_security_main', serialize($securityToken));
return $this->redirectToRoute('admin_dashbord');
}
}
// login classique
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('admin/authentification/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
// end bloc login from crm
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}