src/Controller/SecurityController.php line 21
<?php
namespace App\Controller;
use App\Entity\ExpeditorTemplate;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
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\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route('/api/get-user', name: 'app_user', methods: 'GET')]
#[Route('/api/check_user_role_access', name: 'app_check_user_role_access', methods: 'GET')]
public function index(#[CurrentUser] ?User $user, Request $request, EntityManagerInterface $entityManager, JWTTokenManagerInterface $JWTManager): Response
{
if ($request->get('_route') === 'app_check_user_role_access' && $request->getSession()->has('admin_switch_user'))
{
$user = $entityManager->getRepository(User::class)->findUserByIdAndRole(
id: $request->getSession()->get('admin_switch_user'),
role: 'ROLE_ADMIN'
);
$token = $JWTManager->create($user);
$request->getSession()->remove('admin_switch_user');
return $this->json([
'token' => $token,
'role' => 'ROLE_ADMIN'
]);
}
/* @var $user User */
if (null === $user)
{
return $this->json(['message' => 'USER NOT FOUND'], Response::HTTP_UNAUTHORIZED);
}
$expTemplates = $user->getExpeditorAccount()->getExpeditorTemplates()->map(function (ExpeditorTemplate $template){
return ['name' => $template->getName(), 'code' => 'Exp' . $template->getFilename()];
});
$userInfo = [
'id' => $user->getId(),
'email' => $user->getUserIdentifier(),
'expId' => $user->getExpeditorAccount()->getId(),
'expAccount' => $user->getExpeditorAccount()->getLibrestAccountNumber(),
'expPrefix' => $user->getExpeditorAccount()->getPrefix(),
'expCompany' => $user->getExpeditorAccount()->getCompany(),
'expBaseURL' => $user->getExpeditorAccount()->getUrlContent(),
'expTemplates' => $expTemplates,
'isGrantedRoleAdmin' => $this->isGranted('ROLE_ADMIN')
];
return $this->json($userInfo);
}
#[Route('/admin/logout', name: 'app_admin_logout')]
public function logout(): Response
{
return $this->redirectToRoute('app_admin_login');
}
#[Route('/admin/login', name: 'app_admin_login')]
public function adminLogin(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('admin/security/login.html.twig', [
'controller_name' => 'SecurityController',
'page_name' => 'Identification',
'last_username' => $lastUsername,
'error' => $error,
]);
}
}