<?php
namespace App\Controller;
use App\Entity\Blocked;
use App\Repository\BlockedRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
/**
* @Route("/", name="app_login")
*/
public function login(Request $request,AuthenticationUtils $authenticationUtils, UserRepository $userRepository, BlockedRepository $blockedRepository): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if ($this->getUser()) {
$test = false;
$blocked = $blockedRepository->findOneBy(['User'=>$this->getUser()]);
if ($blocked != null) {
if($blocked->getStatut() == "bloquer"){
throw new CustomUserMessageAccountStatusException("Votre compte est bloqué merci de contacter votre administrateur.");
}else{
return $this->redirectToRoute('back_office');
}
}
else{
return $this->redirectToRoute('back_office');
}
}
else{
$test = true;
//dd($this->getParameter("login_throttling"));
}
$blocked = $blockedRepository->findOneBy(['mail'=>$lastUsername]);
if ($blocked != null) {
if($blocked->getStatut() == "bloquer"){
$error = new CustomUserMessageAccountStatusException("Votre compte est bloqué merci de contacter votre administrateur.");
}
}
//var_dump($lastUsername);
if ($error != null) {
if($error->getMessage() == ""){
$element = $blockedRepository->findBy(['mail' => $lastUsername]);
$blocked = new Blocked();
$user = $userRepository->findOneBy(['email'=>$lastUsername]);
//dd($element);
if ($element == []) {
$blocked->setMail($lastUsername);
$blocked->setStatut("bloquer");
$blocked->setUser($user);
$blocked->setIpAdress($request->server->get("REMOTE_ADDR"));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($blocked);
$entityManager->flush();
$error = new CustomUserMessageAccountStatusException("Votre compte est bloqué merci de contacter votre administrateur.");
}
};
}
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'test' => $test, 'notification'=> null,]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}