src/Controller/SecurityController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Clinics;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Security\AuthorizationChecker;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     private $em;
  14.     public function __construct(EntityManagerInterface $em)
  15.     {
  16.         $this->em $em;
  17.     }
  18.     #[Route(path'/distributors/login'name'distributor_login')]
  19.     public function distributorLogin(AuthenticationUtils $authenticationUtilsRequest $requestAuthorizationChecker $checker): Response
  20.     {
  21.         if (true === $checker->isGranted('ROLE_DISTRIBUTOR')) {
  22.             $distributor_id $this->getUser()->getDistributor()->getId();
  23.             return $this->redirectToRoute('distributor_order_list',[
  24.                 'distributor_id' => $distributor_id
  25.             ]);
  26.         }
  27.         $uri explode('/'$request->getPathInfo());
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         return $this->render('security/login.html.twig', [
  33.             'last_username' => $lastUsername,
  34.             'error' => $error,
  35.             'csrf_token_intention' => 'authenticate',
  36.             'user_type' => $uri[1],
  37.         ]);
  38.     }
  39.     #[Route(path'/clinics/login'name'clinics_login')]
  40.     public function clinicLogin(AuthenticationUtils $authenticationUtilsRequest $requestAuthorizationChecker $checker): Response
  41.     {
  42.         if (true === $checker->isGranted('ROLE_CLINIC'))
  43.         {
  44.             $clinicId $this->getUser()->getClinic()->getId();
  45.             return $this->redirectToRoute('clinic_orders_list',[
  46.                 'clinic_id' => $clinicId
  47.             ]);
  48.         }
  49.         $uri explode('/'$request->getPathInfo());
  50.         // get the login error if there is one
  51.         $error $authenticationUtils->getLastAuthenticationError();
  52.         // last username entered by the user
  53.         $lastUsername $authenticationUtils->getLastUsername();
  54.         return $this->render('security/login.html.twig', [
  55.             'last_username' => $lastUsername,
  56.             'error' => $error,
  57.             'csrf_token_intention' => 'authenticate',
  58.             'user_type' => $uri[1],
  59.         ]);
  60.     }
  61.     #[Route(path'/manufacturers/login'name'manufacturer_login')]
  62.     public function manufacturerLogin(AuthenticationUtils $authenticationUtilsRequest $requestAuthorizationChecker $checker): Response
  63.     {
  64.         if (true === $checker->isGranted('ROLE_MANUFACTURER')) {
  65.             header('Location: '$this->getParameter('app.base_url') . '/manufacturers/analytics');
  66.             die();
  67.         }
  68.         $uri explode('/'$request->getPathInfo());
  69.         // get the login error if there is one
  70.         $error $authenticationUtils->getLastAuthenticationError();
  71.         // last username entered by the user
  72.         $lastUsername $authenticationUtils->getLastUsername();
  73.         return $this->render('security/login.html.twig', [
  74.             'last_username' => $lastUsername,
  75.             'error' => $error,
  76.             'csrf_token_intention' => 'authenticate',
  77.             'user_type' => $uri[1],
  78.         ]);
  79.     }
  80.     #[Route(path'/admin/login'name'admin_login')]
  81.     public function adminLogin(AuthenticationUtils $authenticationUtilsRequest $requestAuthorizationChecker $checker): Response
  82.     {
  83.         if (true === $checker->isGranted('ROLE_ADMIN'))
  84.         {
  85.             return $this->redirectToRoute('products_list',[
  86.                 'page_id' => 1
  87.             ]);
  88.         }
  89.         $uri explode('/'$request->getPathInfo());
  90.         // get the login error if there is one
  91.         $error $authenticationUtils->getLastAuthenticationError();
  92.         // last username entered by the user
  93.         $lastUsername $authenticationUtils->getLastUsername();
  94.         return $this->render('security/login.html.twig', [
  95.             'last_username' => $lastUsername,
  96.             'error' => $error,
  97.             'csrf_token_intention' => 'authenticate',
  98.             'user_type' => '',
  99.         ]);
  100.     }
  101.     #[Route(path'/admin/logout'name'admin_logout')]
  102.     public function logout(): void
  103.     {
  104.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  105.     }
  106.     #[Route(path'/clinics/logout'name'clinics_logout')]
  107.     public function clinicLogout(): void
  108.     {
  109.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  110.     }
  111.     #[Route(path'/distributors/logout'name'distributors_logout')]
  112.     public function distributorLogout(): void
  113.     {
  114.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  115.     }
  116.     #[Route(path'/manufacturers/logout'name'manufacturers_logout')]
  117.     public function manufacturerLogout(): void
  118.     {
  119.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  120.     }
  121. }