<?php
/**
* Created by Mediterranean Develup Solutions.
* User: carlos.rojas@develup.solutions
* Date: 12/06/2017
* Time: 17:44
*/
namespace App\Controller;
use App\Entity\Configuration;
use App\Entity\SettingsBranding;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Response;
class SecurityController extends AbstractController
{
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
#[Route(path: '/inicio', name: 'ruta_inicio')]
public function inicioAction(AuthenticationUtils $authenticationUtils): Response
{
$em = $this->getDoctrine()->getManager();
$configuration = $em->getRepository(Configuration::class)->findOneBy([]);
return $this->render('login/login.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
'configuration' => $configuration,
]);
}
/**
* Portal principal con los 3 accesos (ManteVenues, ManteCatering, ManteAV)
*/
#[Route(path: '/login', name: 'login')]
public function loginAction(AuthenticationUtils $authenticationUtils): Response
{
$em = $this->getDoctrine()->getManager();
// Recupera la configuración general (solo debe haber 1)
$configuration = $em->getRepository(Configuration::class)->findOneBy([]);
return $this->render('inicio/inicio.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
'configuration' => $configuration,
]);
}
#[Route(path: '/login/{type}', name: 'app_login_branded')]
public function loginBranded(string $type, Request $request, AuthenticationUtils $authenticationUtils): Response
{
$em = $this->getDoctrine()->getManager();
// 1. Buscamos la configuración de marca para este tipo (venues, catering o av)
$branding = $em->getRepository(SettingsBranding::class)->findOneBy([
'accessType' => $type,
'isActive' => true
]);
// Si no existe esa configuración, volvemos al portal
if (!$branding) {
return $this->redirectToRoute('ruta_inicio');
}
// 2. Guardamos en sesión para que el Authenticator sepa a dónde volver si falla
$request->getSession()->set('current_access_type', $type);
return $this->render('login/login_unified.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
'branding' => $branding,
'type' => $type
]);
}
/**
* Cambiar el tipo de acceso (venues / av / catering) y volver al dashboard
*/
#[Route(path: '/switch-access/{type}', name: 'switch_access')]
public function switchAccess(string $type, Request $request): Response
{
// Solo aceptamos estos tipos. Ajusta si quieres añadir más.
$allowed = ['venues', 'av', 'catering'];
if (!in_array($type, $allowed, true)) {
throw $this->createNotFoundException();
}
// Guardar el nuevo tipo en sesión
$request->getSession()->set('current_access_type', $type);
// Redirigir al dashboard principal (pon aquí tu ruta real)
return $this->redirectToRoute('homepage');
// ^^^ cambia 'home' por la ruta que uses para tu panel principal
}
#[Route(path: '/kickout', name: 'kickout')]
public function KickoutAction(Request $request): Response
{
$this->get('security.token_storage')->setToken(null);
$session = $request->getSession();
$session->invalidate();
return $this->render('kickout/kickout.html.twig');
}
#[Route(path: '/login_check', name: 'login_check')]
public function loginCheckAction(): void
{
// lo gestiona el LoginAuthenticator (Guard)
}
#[Route(path: '/logout', name: 'logout')]
public function logoutAction(): void
{
// gestionado por el firewall
}
public function getLicencias(): ?int
{
$repository = $this->em->getRepository(Configuration::class);
$configuration = $repository->findOneById(1);
return $configuration->getLicenses();
}
}