src/Controller/SecurityController.php line 162

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Locale;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Service\LocalList;
  12. use App\Entity\ProjectUsers;
  13. class SecurityController extends AbstractController
  14. {
  15.     private $translator;
  16.     private $localList;
  17.     protected $entityManager;
  18.     public function __construct(TranslatorInterface $translatorEntityManagerInterface $entityManagerLocalList $localList)
  19.     {
  20.         $this->translator $translator;
  21.         $this->entityManager $entityManager;
  22.         $this->localList $localList;
  23.     }
  24.     /**
  25.      * @Route("/compte/{_locale}", name="dashboard")
  26.      */
  27.     public function index(string $_locale): Response
  28.     {
  29.         $locale $this->entityManager->getRepository(Locale::class)->findBy(['iso_code' => $_locale]);
  30.         if (!$locale) {
  31.             $_locale Locale::DEFAULT_LANG;
  32.             return $this->redirectToRoute('dashboard', ["_locale" => $_locale]);
  33.         }
  34.         switch ($this->localList->RoleUser()) {
  35.             case "ROLE_ADMIN":
  36.                 $users $this->entityManager->getRepository(ProjectUsers::class)->getActiveUsers();
  37.                 return $this->render('admin/dashboard/index.html.twig', [
  38.                     'controller_name' => 'DashboardController',
  39.                     'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
  40.                     'users' => $users
  41.                 ]);
  42.                 break;
  43.             case "ROLE_CLIENT_USER":
  44.                 return $this->render('security/client_dashboard.html.twig', [
  45.                     'controller_name' => 'SecurityController',
  46.                     'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
  47.                 ]);
  48.                 break;
  49.             case "ROLE_USER":
  50.                 $projects $this->entityManager->getRepository(ProjectUsers::class)->getResponsableProjects($this->getUser(), true);
  51.                 if (empty($projects)) {
  52.                     $user_data = [
  53.                         "id" => $this->getUser()->getId(),
  54.                         "label" => $this->getUser()->getNom() . " " $this->getUser()->getPrenom(),
  55.                         'color' => "#ff0000",
  56.                     ];
  57.                     return $this->render('security/user_dashboard.html.twig', [
  58.                         'controller_name' => 'SecurityController',
  59.                         'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
  60.                         'user_data' => $user_data
  61.                     ]);
  62.                 } else {
  63.                     $users $this->entityManager->getRepository(ProjectUsers::class)->getActiveUsers(nullnullnull$projects);
  64.                     return $this->render('admin/dashboard/index.html.twig', [
  65.                         'controller_name' => 'DashboardController',
  66.                         'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
  67.                         'users' => $users,
  68.                         'isResponsable' => true,
  69.                     ]);
  70.                 }
  71.                 break;
  72.             default:
  73.                 break;
  74.         }
  75.         return $this->render('security/index.html.twig', [
  76.             'controller_name' => 'SecurityController',
  77.             'titre_page' => $this->translator->trans('securite.title_page.dashboard'),
  78.         ]);
  79.     }
  80.     /**
  81.      * @Route("/", name="primal_view")
  82.      */
  83.     public function primal_view(): Response
  84.     {
  85.         return $this->redirectToRoute('connexion');
  86.     }
  87.     /**
  88.      * @Route("/connexion/{_locale}", name="connexion")
  89.      */
  90.     public function connexion(AuthenticationUtils $authenticationUtilsRequest $requeststring $_locale): Response
  91.     {
  92.         $error $authenticationUtils->getLastAuthenticationError();
  93.         $lastUsername $authenticationUtils->getLastUsername();
  94.         $locale $this->entityManager->getRepository(Locale::class)->findBy(array('iso_code' => $_locale));
  95.         if (!$locale)
  96.             return $this->redirectToRoute('connexion', ['_locale' => Locale::DEFAULT_LANG]);
  97.         if ($this->getUser()) {
  98.             if (in_array('ROLE_ADMIN'$this->getUser()->getRoles())) {
  99.                 return $this->redirectToRoute('admin_projects', [
  100.                     '_locale' => $_locale,
  101.                 ]);
  102.             } else {
  103.                 return $this->redirectToRoute('front_projects', [
  104.                     '_locale' => $_locale,
  105.                 ]);
  106.             }
  107.         }
  108.         return $this->render('security/connexion.html.twig', [
  109.             'controller_name' => 'SecurityController',
  110.             'titre_page' => $this->translator->trans('securite.title_page.connexion'),
  111.             'last_username' => $lastUsername,
  112.             'error' => $error,
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/demande_acces/{_locale}", name="demande_acces")
  117.      */
  118.     public function demande_acces(AuthenticationUtils $authenticationUtilsRequest $requeststring $_locale): Response
  119.     {
  120.         $error $authenticationUtils->getLastAuthenticationError();
  121.         $lastUsername $authenticationUtils->getLastUsername();
  122.         return $this->render('security/demande_acces.html.twig', [
  123.             'controller_name' => 'UserController',
  124.             'titre_page' => $this->translator->trans('securite.title_page.connexion'),
  125.             'last_username' => $lastUsername,
  126.             'error' => $error,
  127.         ]);
  128.     }
  129.     /**
  130.      * @Route("/logout", name="logout")
  131.      */
  132.     public function logout(Request $request): void
  133.     {
  134.         $session $request->getSession();
  135.         $session->remove('userRole');
  136.         if ($session->has('clients_user'))
  137.             $session->remove('clients_user');
  138.         if ($session->has('is_client_prestataire'))
  139.             $session->remove('is_client_prestataire');
  140.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  141.     }
  142. }