<?php
namespace App\Controller;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class IndexController extends AbstractController
{
public function __construct(
EntityManagerInterface $em,
TokenStorageInterface $tokenStorage,
SessionInterface $session
) {
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->session = $session;
}
/**
* @Route("/", name="aaa")
*/
public function index(): Response
{
$user = $this->getUser();
if($user){
$roles = $user->getRoles();
if (in_array("ROLE_SUPER_ADMINISTRATOR", $roles) or in_array("ROLE_ADMIN", $roles)){
return $this->redirectToRoute('admin_project_list_home');
}else if (in_array("ROLE_DEV", $roles) or in_array("ROLE_EPC", $roles)){
return $this->redirectToRoute('user_project_list_home');
}
}else{
return $this->redirectToRoute('app_login');
}
return $this->render('admin/index.html.twig');
}
/**
* @Route("/integration/{page}", name="app_integration")
*/
public function integration($page): Response
{
return $this->render("intergration/$page.html.twig");
}
/**
* @Route("/integration/equipement/{page}", name="app_equipement_integration")
*/
public function integrationEquipement($page): Response
{
return $this->render("intergration/equipement/$page.html.twig");
}
/**
* @Route("/switch-user/{id}", name="switch_user_admin")
* @IsGranted("ROLE_SUPER_ADMINISTRATOR")
*/
public function switchUser(Request $request,$id) : Response
{
$user = $this->em->getRepository(User::class)->find($id);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->tokenStorage->setToken($token);
$this->session->set('_security_main', serialize($token));
return $this->redirectToRoute('intervenant_project_view');
}
/**
* @Route("/altarea", name="app_altarea_login_redirection")
*/
public function redirectAltarea(): Response
{
return $this->redirect('https://launcher.myapps.microsoft.com/api/signin/e458aa6e-633e-4c9a-a6eb-e60e14212070?tenantId=a3fa8e74-b14d-4758-a49c-931f83606b14');
}
}