<?php
namespace App\Controller\Front;
use App\Entity\Profile;
use App\Entity\User;
use App\Security\JsonAuthenticator;
use App\Service\SerializeService;
use App\Service\Tools;
use Doctrine\ORM\EntityManagerInterface;
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\Guard\GuardAuthenticatorHandler;
/**
* Class AppController
* @package App\Controller\Front
* @Route(path="", name="app_")
*/
class AppController extends AbstractController
{
private SerializeService $serializeService;
private EntityManagerInterface $em;
public function __construct(
SerializeService $serializeService,
EntityManagerInterface $em
) {
$this->serializeService = $serializeService;
$this->em = $em;
}
/**
* @return Response
* @Route("/{vueRouting}", name="index", requirements={"vueRouting"="^(?!api|js|ipocam|mail|institution|workspace/menubuilder|matrix/menubuilder|login/to|mobile|pdfmenu|_(profiler|wdt)).*"}, defaults={"vueRouting"=null})
*/
public function index(Request $request)
{
/** @var User $user */
$user = $this->getUser();
if (!is_null($user)) {
if ($user->getProfile()->getRole() !== Profile::ROLE_CUSTOMER) {
return $this->redirectToRoute('admin_login');
}
$user = $this->serializeService->serializeUser($user);
}
$urlNotIndxeds = [
'/mobile',
'/pdfmenu',
];
$isIndexed = true;
foreach ($urlNotIndxeds as $urlNotIndxed) {
if (str_contains($request->getUri(), $urlNotIndxed)) {
$isIndexed = false;
}
}
return $this->render('front/app/mizogoo.html.twig', [
'isAuthenticated' => base64_encode(json_encode(!empty($user))),
'user' => base64_encode(json_encode($user)),
'isIndexed' => $isIndexed,
]);
}
/**
* @param $hash
* @param JsonAuthenticator $authenticator
* @param GuardAuthenticatorHandler $guardHandler
* @param Request $request
* @return Response
* @Route("/login/to/{hash}", name="login_to")
*/
public function loginTo($hash, JsonAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler, Request $request): Response
{
/** @var User $userTo */
$userTo = $this->em->getRepository(User::class)->find(Tools::decrypt($hash));
if ($userTo) {
if ($userTo->getProfile()->getRole() == Profile::ROLE_CUSTOMER) {
if (str_contains($request->headers->get('referer'), $this->generateUrl('admin_customer_view', ['customer' => $userTo->getId()])) ||
str_contains($request->headers->get('referer'), $this->generateUrl('admin_index'))) {
$guardHandler->authenticateUserAndHandleSuccess(
$userTo,
$request,
$authenticator,
'user_prodiver'
);
return $this->redirectToRoute('app_index');
}
}
}
return $this->redirectToRoute('admin_index');
}
}