src/Package/Admin/Main/Controller/Login/LoginController.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Package\Admin\Main\Controller\Login;
  3. use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  4. use Symfony\Component\HttpFoundation\{RequestResponse};
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\Security\Core\Exception\TooManyLoginAttemptsAuthenticationException;
  10. /**
  11.  * LoginController
  12.  * 
  13.  * Performs login and is admin still logged action (for auto logging out)
  14.  * 
  15.  * @author     Symfony
  16.  * @author     Daniel Balowski <d.balowski@openform.pl> (_refactorer, _developer)
  17.  * @copyright  Openform
  18.  * @since      03.2019
  19.  */
  20. class LoginController extends AbstractController
  21. {
  22.     /** @var \Psr\Container\ContainerInterface */
  23.     protected $container;
  24.     /** @var RouteLocalizer */
  25.     private $routeLocalizer;
  26.     public function __construct(RouteLocalizer $routeLocalizer)
  27.     {
  28.         $this->routeLocalizer $routeLocalizer;
  29.     }
  30.     /**
  31.      * Performs login
  32.      * 
  33.      * @param Request  $request
  34.      * 
  35.      * @return Response
  36.      */
  37.     public function loginAction(
  38.         Request $request
  39.         AuthenticationUtils $authenticationUtils
  40.         AuthorizationCheckerInterface $authChecker
  41.         TokenStorageInterface $tokenStorage
  42.     ): Response
  43.     {
  44.         // $authChecker  = $this->get('security.authorization_checker'); //deprec
  45.         // $tokenStorage = $this->get('security.token_storage'); //deprec
  46.         if (
  47.             $tokenStorage->getToken() &&
  48.             $authChecker->isGranted('ROLE_ADMIN')
  49.         ) {
  50.             return $this->redirect(
  51.                 $this->routeLocalizer->generate('admin_adminModule_home', [], $request->getLocale())
  52.             );
  53.         }
  54.         $error $authenticationUtils->getLastAuthenticationError();
  55.         $lastUsername $authenticationUtils->getLastUsername();
  56.         if ($error instanceof TooManyLoginAttemptsAuthenticationException) {
  57.             /** @var TooManyLoginAttemptsAuthenticationException $error */
  58.             $error->errorCode 13666;
  59.         }
  60.         return $this->render(
  61.             '@admin_templates/Login/login.html.twig',
  62.             [
  63.                 'lastUsername' => $lastUsername,
  64.                 'error'        => $error,
  65.             ]
  66.         );
  67.     }
  68.     /**
  69.      * [AJAX] Checks if admin is still logged
  70.      * 
  71.      * @param Request  $request
  72.      * 
  73.      * @return Response
  74.      */
  75.     public function isAdminStillLoggedAction(Request $request): Response
  76.     {
  77.         /** @var \App\Package\Admin\Main\Entity\Admin */
  78.         $admin $this->getUser();
  79.         if (
  80.             !$admin ||
  81.             gettype($admin) === 'string' ||
  82.             !$lastSeen $admin->getLastSeen()
  83.         ) {
  84.             return new Response(
  85.                 json_encode(
  86.                     ['status' => 'logout']
  87.                 )
  88.             );
  89.         }
  90.         $lastSeen =
  91.             (clone $lastSeen)
  92.             ->modify('+' $this->container->getParameter('admin.login.session_minutes') . 'mins');
  93.         $now = new \DateTime();
  94.         $timeLeft strtotime($lastSeen->format('Y-m-d H:i:s')) - strtotime($now->format('Y-m-d H:i:s'));
  95.         if ($timeLeft <= 0) {
  96.             return new Response(
  97.                 json_encode(
  98.                     ['status' => 'logout']
  99.                 )
  100.             );
  101.         }
  102.         return new Response(
  103.             json_encode(
  104.                 [
  105.                     'status'       => 'still_logged',
  106.                     'minutes_left' => $minutes floor($timeLeft 60),
  107.                     'seconds_left' => $timeLeft - ($minutes 60)
  108.                 ]
  109.             )
  110.         );
  111.     }
  112. }