<?php
namespace App\Controller;
use App\Service\LoggerService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* @Route(
* {
* "es": "/{_locale}",
* "ca": "/{_locale}",
* "eu": "/{_locale}",
* },
* requirements={"_locale": "%app.supported_locales%"}
* )
*/
class SecurityController extends AbstractController
{
public function __construct(
private LoggerService $logger,
private RequestStack $request_stack,
private SessionInterface $session
) {
}
/**
* @Route("", name="app_index")
*/
public function index(AuthenticationUtils $authenticationUtils, Request $request): Response
{
/** @var \App\Entity\User $logged_user */
$logged_user = $this->getUser();
if ($logged_user) {
$institutionId = $logged_user->getInstitution()->getId();
$locale = ($institutionId == 2) ? 'ca' : 'es';
if ($institutionId == 2) {
$locale = 'ca';
$this->request_stack->getCurrentRequest()->setLocale($locale);
$this->session->set('_locale', $locale);
$redirectUrl = $this->generateUrl(
'app_actAs',
['_locale' => $locale],
UrlGeneratorInterface::ABSOLUTE_URL
);
return $this->redirect($redirectUrl);
}
return $this->redirectToRoute('app_actAs');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
//dd($logged_user);
return $this->redirectToRoute('app_login');
// return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
/** @var \App\Entity\User $logged_user */
$logged_user = $this->getUser();
if ($logged_user) {
$this->logger->info('Iniciado sesion', ['User' => $logged_user->getId()]);
return $this->redirectToRoute('app_index');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'security/login.html.twig',
['last_username' => $lastUsername, 'error' => $error]
);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
/** @var \App\Entity\User $logged_user */
$logged_user = $this->getUser();
$this->logger->info(
'Cerrado sesion',
['User' => $logged_user->getId()]
);
throw new \LogicException(
'This method can be blank - it will be intercepted by the logout key on your firewall.'
);
}
/**
* @Route("/waiting_verify", name="app_waiting_verify")
*/
public function waitingVerify(): Response
{
/** @var \App\Entity\User $logged_user */
$logged_user = $this->getUser();
if ($logged_user) {
if ($logged_user->isVerified()) {
return $this->redirectToRoute('app_index');
}
}
return $this->render('security/waiting_verify.html.twig');
}
/**
* @Route("/banned", name="app_banned")
*/
public function banned(): Response
{
/** @var \App\Entity\User $logged_user */
$logged_user = $this->getUser();
if (!$logged_user->isBanned()) {
return $this->redirectToRoute('app_index');
}
return $this->render('security/banned.html.twig', [
'user' => $logged_user,
]);
}
}