src/Controller/Security/LoginController.php line 26

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Controller/Security/LoginController.php
  4. //----------------------------------------------------------------------
  5. namespace App\Controller\Security;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  13. use App\Entity\Access;
  14. use App\Entity\AccessClient\AccessClient;
  15. use App\Services\CommonTools;
  16. use App\Services\LogTools;
  17. use App\Services\Security\IpTools;
  18. use App\Services\Security\RedirectTools;
  19. use App\Services\Security\SecurityTools;
  20. class LoginController extends AbstractController
  21. {
  22.     public function __construct(ManagerRegistry $doctrineIpTools $ipToolsRedirectTools $redirectToolsLogTools $logToolsSecurityTools $securityToolsCommonTools $commonTools)
  23.     {
  24.         $this->em $doctrine->getManager();
  25.         $this->ipTools $ipTools;
  26.         $this->redirectTools $redirectTools;
  27.         $this->logTools $logTools;
  28.         $this->securityTools $securityTools;
  29.         $this->commonTools $commonTools;
  30.     }
  31.     public function login(Request $requestAuthenticationUtils $authenticationUtils): Response
  32.     {
  33.         // If the user is already logged in, redirect
  34.         if ($this->isGranted('IS_AUTHENTICATED_FULLY'))
  35.         {
  36.             return $this->redirectToRoute('login_redirect');
  37.         }
  38.         $today = new \DateTime();
  39.         $ip $request->getClientIp();
  40.         if ($this->ipTools->isBanned($ip))
  41.         {
  42.             // Don't throw AccessDeniedException to avoid infinite loop (Default firewall behavior : Redirect to login)
  43.             throw new AccessDeniedHttpException('');
  44.         }
  45.         // get the login error if there is one
  46.         $error $authenticationUtils->getLastAuthenticationError();
  47.         // last username entered by the user
  48.         $lastUsername $authenticationUtils->getLastUsername();
  49.         // Username in GET ?
  50.         $username null;
  51.         $activationMsg null;
  52.         if (!empty($request->query->get('username')))
  53.         {
  54.             $username $request->query->get('username');
  55.         }
  56.         if (!empty($request->query->get('activation_message')))
  57.         {
  58.             $activationMsg $request->query->get('activation_message');
  59.         }
  60.         // Get the redirect data if any
  61.         $redirectData $this->redirectTools->getRedirectData($request->cookies);
  62.         $render = array(
  63.             'activation_message'    =>    $activationMsg,
  64.             'last_username'         =>     $lastUsername,
  65.             'username'                =>    $username,
  66.             'error'                 =>     $error,
  67.             'today'                    =>    $today,
  68.         );
  69.         if ($redirectData !== null)
  70.         {
  71.             $render['icod_id'] = $redirectData['icod_id'];
  72.             $render['icod_route'] = $redirectData['icod_route'];
  73.             $render['icod_dont_redirect'] = $redirectData['icod_dont_redirect'];
  74.         }
  75.         // Plan.io Task #4327
  76.         $render['access_client_login'] = $this->commonTools->craftUrl('access_client_login');
  77.         return $this->render('security/login.html.twig'$render);
  78.     }
  79.     // Plan.io Task #4327
  80.     // Are we in the right place ?
  81.     public function areYouLost(Request $request): JsonResponse
  82.     {
  83.         if (!$request->isXmlHttpRequest())
  84.             return new JsonResponse(array('status' => 'Error'),400);
  85.         if (!isset($request->request))
  86.             return new JsonResponse(array('status' => 'Error'),400);
  87.         // Get data
  88.         $email $request->request->get('email');
  89.         $email preg_replace('/\s+/'''$email);
  90.         $email filter_var($emailFILTER_VALIDATE_EMAIL);
  91.         if (!$email)
  92.         {
  93.             // This is the case for admins and all data that is not an email
  94.             return new JsonResponse(array('status' => 'Stay''msg' => 'not.an.email'), 200);
  95.         }
  96.         // This is called from the Access Login page
  97.         // So check if the email address corresponds to an Access
  98.         // or an AccessClient who got lost
  99.         $access $this->em->getRepository(Access::class)->findOneByEmail($email);
  100.         if ($access !== null)
  101.         {
  102.             // All is good => Exit gracefully
  103.             return new JsonResponse(array('status' => 'Stay''msg' => 'you.are.in.the.right.place'), 200);
  104.         }
  105.         $accessClient $this->em->getRepository(AccessClient::class)->findOneByEmail($email);
  106.         if ($accessClient !== null)
  107.         {
  108.             // Redirect the poor guy on the AccessClient login page
  109.             return new JsonResponse(array('status' => 'Redirect''msg' => 'you.are.lost'), 200);
  110.         }
  111.         // If we are here it means that the email is just invalid
  112.         // Stay on the same page
  113.         return new JsonResponse(array('status' => 'Stay''msg' => 'who.are.you.stranger'), 200);
  114.     }
  115. }