src/Controller/SecurityController.php line 21

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ExpeditorTemplate;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Http\Attribute\CurrentUser;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. class SecurityController extends AbstractController
  15. {
  16.     #[Route('/api/get-user'name'app_user'methods'GET')]
  17.     #[Route('/api/check_user_role_access'name'app_check_user_role_access'methods'GET')]
  18.     public function index(#[CurrentUser] ?User $userRequest $requestEntityManagerInterface $entityManagerJWTTokenManagerInterface $JWTManager): Response
  19.     {
  20.         if ($request->get('_route') === 'app_check_user_role_access' && $request->getSession()->has('admin_switch_user'))
  21.         {
  22.             $user $entityManager->getRepository(User::class)->findUserByIdAndRole(
  23.                 id$request->getSession()->get('admin_switch_user'),
  24.                 role'ROLE_ADMIN'
  25.             );
  26.             $token $JWTManager->create($user);
  27.             $request->getSession()->remove('admin_switch_user');
  28.             return $this->json([
  29.                 'token' => $token,
  30.                 'role' => 'ROLE_ADMIN'
  31.             ]);
  32.         }
  33.         /* @var $user User */
  34.         if (null === $user)
  35.         {
  36.             return $this->json(['message' => 'USER NOT FOUND'], Response::HTTP_UNAUTHORIZED);
  37.         }
  38.         $expTemplates $user->getExpeditorAccount()->getExpeditorTemplates()->map(function (ExpeditorTemplate $template){
  39.             return ['name' => $template->getName(), 'code' => 'Exp' $template->getFilename()];
  40.         });
  41.         $userInfo = [
  42.             'id' => $user->getId(),
  43.             'email' => $user->getUserIdentifier(),
  44.             'expId' => $user->getExpeditorAccount()->getId(),
  45.             'expAccount' => $user->getExpeditorAccount()->getLibrestAccountNumber(),
  46.             'expPrefix' => $user->getExpeditorAccount()->getPrefix(),
  47.             'expCompany' => $user->getExpeditorAccount()->getCompany(),
  48.             'expBaseURL' => $user->getExpeditorAccount()->getUrlContent(),
  49.             'expTemplates' => $expTemplates,
  50.             'isGrantedRoleAdmin' => $this->isGranted('ROLE_ADMIN')
  51.         ];
  52.         return $this->json($userInfo);
  53.     }
  54.     #[Route('/admin/logout'name'app_admin_logout')]
  55.     public function logout(): Response
  56.     {
  57.         return $this->redirectToRoute('app_admin_login');
  58.     }
  59.     #[Route('/admin/login'name'app_admin_login')]
  60.     public function adminLogin(AuthenticationUtils $authenticationUtils): Response
  61.     {
  62.         // get the login error if there is one
  63.         $error $authenticationUtils->getLastAuthenticationError();
  64.         // last username entered by the user
  65.         $lastUsername $authenticationUtils->getLastUsername();
  66.         return $this->render('admin/security/login.html.twig', [
  67.             'controller_name' => 'SecurityController',
  68.             'page_name' => 'Identification',
  69.             'last_username' => $lastUsername,
  70.             'error'         => $error,
  71.         ]);
  72.     }
  73. }