<?php
namespace App\Controller;
use App\Entity\Locale;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\LocalList;
use App\Entity\ProjectUsers;
class SecurityController extends AbstractController
{
private $translator;
private $localList;
protected $entityManager;
public function __construct(TranslatorInterface $translator, EntityManagerInterface $entityManager, LocalList $localList)
{
$this->translator = $translator;
$this->entityManager = $entityManager;
$this->localList = $localList;
}
/**
* @Route("/compte/{_locale}", name="dashboard")
*/
public function index(string $_locale): Response
{
$locale = $this->entityManager->getRepository(Locale::class)->findBy(['iso_code' => $_locale]);
if (!$locale) {
$_locale = Locale::DEFAULT_LANG;
return $this->redirectToRoute('dashboard', ["_locale" => $_locale]);
}
switch ($this->localList->RoleUser()) {
case "ROLE_ADMIN":
$users = $this->entityManager->getRepository(ProjectUsers::class)->getActiveUsers();
return $this->render('admin/dashboard/index.html.twig', [
'controller_name' => 'DashboardController',
'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
'users' => $users
]);
break;
case "ROLE_CLIENT_USER":
return $this->render('security/client_dashboard.html.twig', [
'controller_name' => 'SecurityController',
'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
]);
break;
case "ROLE_USER":
$projects = $this->entityManager->getRepository(ProjectUsers::class)->getResponsableProjects($this->getUser(), true);
if (empty($projects)) {
$user_data = [
"id" => $this->getUser()->getId(),
"label" => $this->getUser()->getNom() . " " . $this->getUser()->getPrenom(),
'color' => "#ff0000",
];
return $this->render('security/user_dashboard.html.twig', [
'controller_name' => 'SecurityController',
'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
'user_data' => $user_data
]);
} else {
$users = $this->entityManager->getRepository(ProjectUsers::class)->getActiveUsers(null, null, null, $projects);
return $this->render('admin/dashboard/index.html.twig', [
'controller_name' => 'DashboardController',
'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
'users' => $users,
'isResponsable' => true,
]);
}
break;
default:
break;
}
return $this->render('security/index.html.twig', [
'controller_name' => 'SecurityController',
'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
]);
}
/**
* @Route("/", name="primal_view")
*/
public function primal_view(): Response
{
return $this->redirectToRoute('connexion');
}
/**
* @Route("/connexion/{_locale}", name="connexion")
*/
public function connexion(AuthenticationUtils $authenticationUtils, Request $request, string $_locale): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
$locale = $this->entityManager->getRepository(Locale::class)->findBy(array('iso_code' => $_locale));
if (!$locale)
return $this->redirectToRoute('connexion', ['_locale' => Locale::DEFAULT_LANG]);
if ($this->getUser()) {
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles())) {
return $this->redirectToRoute('admin_projects', [
'_locale' => $_locale,
]);
} else {
return $this->redirectToRoute('front_projects', [
'_locale' => $_locale,
]);
}
}
return $this->render('security/connexion.html.twig', [
'controller_name' => 'SecurityController',
'titre_page' => $this->translator->trans('securite.title_page.connexion'),
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/demande_acces/{_locale}", name="demande_acces")
*/
public function demande_acces(AuthenticationUtils $authenticationUtils, Request $request, string $_locale): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/demande_acces.html.twig', [
'controller_name' => 'UserController',
'titre_page' => $this->translator->trans('securite.title_page.connexion'),
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout(Request $request): void
{
$session = $request->getSession();
$session->remove('userRole');
if ($session->has('clients_user'))
$session->remove('clients_user');
if ($session->has('is_client_prestataire'))
$session->remove('is_client_prestataire');
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}