src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Assql\AssqlManager;
  4. use App\Security\GasettAuthenticator;
  5. use App\Service\AuthService;
  6. use LogicException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      * @param AuthenticationUtils $authenticationUtils
  18.      * @param AssqlManager $assqlManager
  19.      * @return Response
  20.      * @throws \Doctrine\DBAL\Exception
  21.      */
  22.     public function login(AuthenticationUtils $authenticationUtilsAssqlManager $assqlManager): Response
  23.     {
  24.         if ($this->getUser()) {
  25.             return $this->redirectToRoute('home_page');
  26.         }
  27.         // get the login error if there is one
  28.         $error $authenticationUtils->getLastAuthenticationError();
  29.         // last username entered by the user
  30.         $lastUsername $authenticationUtils->getLastUsername();
  31.         $query $assqlManager->getConnection()->createQueryBuilder();
  32.         $agencies $query
  33.             ->select('coge''rs')
  34.             ->from('fi_agen')
  35.             ->orderBy('coge''ASC')
  36.             ->where("coge != '99'")->fetchAllAssociative();
  37.         return $this->render('security/login.html.twig', [
  38.             'last_username' => $lastUsername,
  39.             'error' => $error,
  40.             'agencies' => $agencies
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/logout", name="app_logout")
  45.      */
  46.     public function logout()
  47.     {
  48.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  49.     }
  50.     /**
  51.      * @Route("/commercial/authentication/{tokenWithAgencyCodeAndCComer}", name="app_authentication_token")
  52.      * @param Request $request
  53.      * @param AuthService $authService
  54.      * @param UserAuthenticatorInterface $userAuthenticator
  55.      * @param GasettAuthenticator $authenticator
  56.      * @param string $tokenWithAgencyCodeAndCComer
  57.      * @return Response
  58.      */
  59.     public function authenticationToken(
  60.         Request $request,
  61.         AuthService $authService,
  62.         UserAuthenticatorInterface $userAuthenticator,
  63.         GasettAuthenticator $authenticator,
  64.         string $tokenWithAgencyCodeAndCComer
  65.     ): Response
  66.     {
  67.         if ($this->getUser()) {
  68.             return $this->redirectToRoute('home_page');
  69.         }
  70.         try {
  71.             $user $authService->authToken($tokenWithAgencyCodeAndCComer);
  72.             $userAuthenticator->authenticateUser($user$authenticator$request);
  73.             return $this->redirectToRoute('home_page');
  74.         } catch (\Throwable $e) {
  75.             $this->addFlash('error'$e->getMessage());
  76.         }
  77.         return $this->redirectToRoute('app_login');
  78.     }
  79. }