<?php
namespace Pec\Bundle\UserBundle\Controller;
use Pec\Bundle\UserBundle\DependencyInjection\Configuration;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class LoginController extends AbstractController {
use TargetPathTrait;
public const ALLOW_REGISTRATION = Configuration::PREFIX . '.' . 'allow_registration';
public const ALLOW_PASSWORD_RESET = Configuration::PREFIX . '.' . 'allow_password_reset';
public const ADDITIONAL_LOGINS = Configuration::PREFIX . '.' . 'additional_logins';
/**
* @param AuthenticationUtils $authenticationUtils
* @param ParameterBagInterface $parameterBag
* @return Response
*/
#[Route("/login", name: "pec_user_login")]
public function index(AuthenticationUtils $authenticationUtils, ParameterBagInterface $parameterBag): Response {
if($this->getUser()) {
return $this->redirectToRoute('pec_dashboard_homepage');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('@PecUser/login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'allowPasswordReset' => $parameterBag->has(self::ALLOW_PASSWORD_RESET) ? $parameterBag->get(self::ALLOW_PASSWORD_RESET) : false,
'allowRegistration' => $parameterBag->has(self::ALLOW_REGISTRATION) ? $parameterBag->get(self::ALLOW_REGISTRATION) : false,
'additionalLogins' => $parameterBag->has(self::ADDITIONAL_LOGINS) ? $parameterBag->get(self::ADDITIONAL_LOGINS) : [],
'templates' => [],
'backgroundImages' => [],
]);
}
#[Route('/logout', name: 'pec_user_logout', methods: ['GET'])]
public function logout(): Response {
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
}