<?php
//----------------------------------------------------------------------
// src/Controller/Security/LoginController.php
//----------------------------------------------------------------------
namespace App\Controller\Security;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Entity\Access;
use App\Entity\AccessClient\AccessClient;
use App\Services\CommonTools;
use App\Services\LogTools;
use App\Services\Security\IpTools;
use App\Services\Security\RedirectTools;
use App\Services\Security\SecurityTools;
class LoginController extends AbstractController
{
public function __construct(ManagerRegistry $doctrine, IpTools $ipTools, RedirectTools $redirectTools, LogTools $logTools, SecurityTools $securityTools, CommonTools $commonTools)
{
$this->em = $doctrine->getManager();
$this->ipTools = $ipTools;
$this->redirectTools = $redirectTools;
$this->logTools = $logTools;
$this->securityTools = $securityTools;
$this->commonTools = $commonTools;
}
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
// If the user is already logged in, redirect
if ($this->isGranted('IS_AUTHENTICATED_FULLY'))
{
return $this->redirectToRoute('login_redirect');
}
$today = new \DateTime();
$ip = $request->getClientIp();
if ($this->ipTools->isBanned($ip))
{
// Don't throw AccessDeniedException to avoid infinite loop (Default firewall behavior : Redirect to login)
throw new AccessDeniedHttpException('');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// Username in GET ?
$username = null;
$activationMsg = null;
if (!empty($request->query->get('username')))
{
$username = $request->query->get('username');
}
if (!empty($request->query->get('activation_message')))
{
$activationMsg = $request->query->get('activation_message');
}
// Get the redirect data if any
$redirectData = $this->redirectTools->getRedirectData($request->cookies);
$render = array(
'activation_message' => $activationMsg,
'last_username' => $lastUsername,
'username' => $username,
'error' => $error,
'today' => $today,
);
if ($redirectData !== null)
{
$render['icod_id'] = $redirectData['icod_id'];
$render['icod_route'] = $redirectData['icod_route'];
$render['icod_dont_redirect'] = $redirectData['icod_dont_redirect'];
}
// Plan.io Task #4327
$render['access_client_login'] = $this->commonTools->craftUrl('access_client_login');
return $this->render('security/login.html.twig', $render);
}
// Plan.io Task #4327
// Are we in the right place ?
public function areYouLost(Request $request): JsonResponse
{
if (!$request->isXmlHttpRequest())
return new JsonResponse(array('status' => 'Error'),400);
if (!isset($request->request))
return new JsonResponse(array('status' => 'Error'),400);
// Get data
$email = $request->request->get('email');
$email = preg_replace('/\s+/', '', $email);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email)
{
// This is the case for admins and all data that is not an email
return new JsonResponse(array('status' => 'Stay', 'msg' => 'not.an.email'), 200);
}
// This is called from the Access Login page
// So check if the email address corresponds to an Access
// or an AccessClient who got lost
$access = $this->em->getRepository(Access::class)->findOneByEmail($email);
if ($access !== null)
{
// All is good => Exit gracefully
return new JsonResponse(array('status' => 'Stay', 'msg' => 'you.are.in.the.right.place'), 200);
}
$accessClient = $this->em->getRepository(AccessClient::class)->findOneByEmail($email);
if ($accessClient !== null)
{
// Redirect the poor guy on the AccessClient login page
return new JsonResponse(array('status' => 'Redirect', 'msg' => 'you.are.lost'), 200);
}
// If we are here it means that the email is just invalid
// Stay on the same page
return new JsonResponse(array('status' => 'Stay', 'msg' => 'who.are.you.stranger'), 200);
}
}