<?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;
class SecurityController extends AbstractController
{
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* Portal principal con los 3 accesos (ManteVenues, ManteCatering, ManteAV)
*
* @Route("/login", name="login")
*/
public function loginAction(AuthenticationUtils $authenticationUtils, Request $request)
{
$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("/login/{type}", name="app_login_branded")
*/
public function loginBranded(string $type, Request $request, AuthenticationUtils $authenticationUtils)
{
$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("/switch-access/{type}", name="switch_access")
*/
public function switchAccess(string $type, Request $request)
{
// 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("/kickout", name="kickout")
*/
public function KickoutAction(Request $request)
{
$this->get('security.token_storage')->setToken(null);
$session = $request->getSession();
$session->invalidate();
return $this->render('kickout/kickout.html.twig');
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
// lo gestiona el LoginAuthenticator (Guard)
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
// gestionado por el firewall
}
public function getLicencias()
{
$repository = $this->em->getRepository(Configuration::class);
$configuration = $repository->findOneById(1);
return $configuration->getLicenses();
}
}