src/App/Controller/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\SessionService;
  5. use FOS\UserBundle\Controller\SecurityController as FOSUserBundleSecurityController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. /**
  10. * The methods here are overrides of FOSUserBundle methods to extend their functionality as needed.
  11. */
  12. class SecurityController extends FOSUserBundleSecurityController
  13. {
  14. /** @var SessionService */
  15. private $sessionService;
  16. public function __construct(SessionService $sessionService, AuthenticationUtils $authenticationUtils, ?CsrfTokenManagerInterface $tokenManager = null)
  17. {
  18. $this->sessionService = $sessionService;
  19. parent::__construct($authenticationUtils, $tokenManager);
  20. }
  21. public function loginAction(): Response
  22. {
  23. /** @var User $user */
  24. $user = $this->getUser();
  25. if (!(null === $user) && $user instanceof User) {
  26. $response = new Response();
  27. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  28. return $this->render('errors/already_logged_in.html.twig', [], $response);
  29. } else {
  30. return parent::loginAction();
  31. }
  32. }
  33. protected function renderLogin(array $data): Response
  34. {
  35. $session = $this->get('session');
  36. $usernamePrefill = $this->sessionService->getUsernamePrefillForLogin($session);
  37. $data['last_username'] = is_null($usernamePrefill) ? $data['last_username'] : $usernamePrefill;
  38. return parent::renderLogin($data);
  39. }
  40. }