<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\SessionService;
use FOS\UserBundle\Controller\SecurityController as FOSUserBundleSecurityController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* The methods here are overrides of FOSUserBundle methods to extend their functionality as needed.
*/
class SecurityController extends FOSUserBundleSecurityController
{
/** @var SessionService */
private $sessionService;
public function __construct(SessionService $sessionService, AuthenticationUtils $authenticationUtils, ?CsrfTokenManagerInterface $tokenManager = null)
{
$this->sessionService = $sessionService;
parent::__construct($authenticationUtils, $tokenManager);
}
public function loginAction(): Response
{
/** @var User $user */
$user = $this->getUser();
if (!(null === $user) && $user instanceof User) {
$response = new Response();
$response->setStatusCode(Response::HTTP_FORBIDDEN);
return $this->render('errors/already_logged_in.html.twig', [], $response);
} else {
return parent::loginAction();
}
}
protected function renderLogin(array $data): Response
{
$session = $this->get('session');
$usernamePrefill = $this->sessionService->getUsernamePrefillForLogin($session);
$data['last_username'] = is_null($usernamePrefill) ? $data['last_username'] : $usernamePrefill;
return parent::renderLogin($data);
}
}