<?php
namespace App\Controller;
use App\Assql\AssqlManager;
use App\Security\GasettAuthenticator;
use App\Service\AuthService;
use LogicException;
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\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @param AssqlManager $assqlManager
* @return Response
* @throws \Doctrine\DBAL\Exception
*/
public function login(AuthenticationUtils $authenticationUtils, AssqlManager $assqlManager): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('home_page');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$query = $assqlManager->getConnection()->createQueryBuilder();
$agencies = $query
->select('coge', 'rs')
->from('fi_agen')
->orderBy('coge', 'ASC')
->where("coge != '99'")->fetchAllAssociative();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'agencies' => $agencies
]);
}
/**
* @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.');
}
/**
* @Route("/commercial/authentication/{tokenWithAgencyCodeAndCComer}", name="app_authentication_token")
* @param Request $request
* @param AuthService $authService
* @param UserAuthenticatorInterface $userAuthenticator
* @param GasettAuthenticator $authenticator
* @param string $tokenWithAgencyCodeAndCComer
* @return Response
*/
public function authenticationToken(
Request $request,
AuthService $authService,
UserAuthenticatorInterface $userAuthenticator,
GasettAuthenticator $authenticator,
string $tokenWithAgencyCodeAndCComer
): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('home_page');
}
try {
$user = $authService->authToken($tokenWithAgencyCodeAndCComer);
$userAuthenticator->authenticateUser($user, $authenticator, $request);
return $this->redirectToRoute('home_page');
} catch (\Throwable $e) {
$this->addFlash('error', $e->getMessage());
}
return $this->redirectToRoute('app_login');
}
}