src/Controller/WebBundle/DefaultController.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\WebBundle;
  4. use App\Entity\UserBundle\User;
  5. use App\Handler\JWTTokenHandler;
  6. use DateTime;
  7. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  8. use Knp\Snappy\Pdf;
  9. use Ramsey\Uuid\Uuid;
  10. use RuntimeException;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  17. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  18. class DefaultController extends AbstractController
  19. {
  20.     private EncoderFactoryInterface $encoderFactory;
  21.     private Pdf $pdf;
  22.     private JWTTokenHandler $JWTTokenHandler;
  23.     public function __construct(EncoderFactoryInterface $encoderFactoryPdf $pdfJWTTokenHandler $JWTTokenHandler)
  24.     {
  25.         $this->encoderFactory $encoderFactory;
  26.         $this->pdf $pdf;
  27.         $this->JWTTokenHandler $JWTTokenHandler;
  28.     }
  29.     public function tIsGranted($permission): bool
  30.     {
  31.         $this->denyAccessUnlessGranted('ROLE_USER');
  32.         if (false === $this->getUser() instanceof User) {
  33.             return false;
  34.         }
  35.         if (null !== $this->getUser()->getParent()) {
  36.             if (in_array($permission, ($this->getUser()->getPermissions()), true)) {
  37.                 return true;
  38.             }
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     public function indexAction(): Response
  44.     {
  45.         return $this->redirectToRoute('localization');
  46.     }
  47.     public function localizationAction(): Response
  48.     {
  49.         if (!$this->tIsGranted('location')) {
  50.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  51.         }
  52.         $user $this->getUser();
  53.         $parent $user->getParent();
  54.         if (null !== $parent) {
  55.             $user $this->getDoctrine()->getManager()->getRepository(User::class)->findOneBy(array('id' => $parent));
  56.         }
  57.         return $this->render('WebBundle/views/Default/localization.html.twig',
  58.             ['showVehicleNameOnMap' => (int)$user->isShowVehicleNameOnMap()]);
  59.     }
  60.     public function mapAction(): Response
  61.     {
  62.         if (!$this->tIsGranted('location')) {
  63.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  64.         }
  65.         return $this->render('WebBundle/views/Default/map.html.twig');
  66.     }
  67.     public function kmlAction(): Response
  68.     {
  69.         return $this->render('WebBundle/views/Default/kml.html.twig');
  70.     }
  71.     public function historyAction(): Response
  72.     {
  73.         if (!$this->tIsGranted('history')) {
  74.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  75.         }
  76.         return $this->render('WebBundle/views/Default/history.html.twig');
  77.     }
  78.     public function reportsAction(): Response
  79.     {
  80.         if (!$this->tIsGranted('reports')) {
  81.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  82.         }
  83.         $twigGlobals $this->get('twig')->getGlobals();
  84.         if ((int)$twigGlobals['report_new'] === 1) {
  85.             return $this->render('WebBundle/views/Default/reports.html.twig');
  86.         }
  87.         return $this->render('WebBundle/views/Default/oldReports.html.twig');
  88.     }
  89.     public function vehiclesAction(): Response
  90.     {
  91.         if (!$this->tIsGranted('cars')) {
  92.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  93.         }
  94.         return $this->render('WebBundle/views/Default/vehicles.html.twig');
  95.     }
  96.     public function fuelAction(): Response
  97.     {
  98.         if (!$this->tIsGranted('fuel')) {
  99.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  100.         }
  101.         return $this->render('WebBundle/views/Default/fuel.html.twig');
  102.     }
  103.     public function placesAction(): Response
  104.     {
  105.         if (!$this->tIsGranted('places')) {
  106.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  107.         }
  108.         return $this->render('WebBundle/views/Default/places.html.twig');
  109.     }
  110.     public function settingsAction(): Response
  111.     {
  112.         if (!$this->tIsGranted('settings')) {
  113.             return $this->render('WebBundle/views/Default/not-granted.html.twig');
  114.         }
  115.         return $this->render('WebBundle/views//Default/settings.html.twig');
  116.     }
  117.     public function exportAction(Request $request): Response
  118.     {
  119.         $format $request->get('format');
  120.         $content $request->get('content');
  121.         $filename $request->get('filename');
  122.         $format 'pdf';
  123.         switch ($format) {
  124.             case 'pdf':
  125.                 $pathFile sprintf('%s/%s.pdf'$this->pdf->getTemporaryFolder(), Uuid::uuid4()->toString());
  126.                 $this->pdf->generateFromHtml(
  127.                     $this->renderView('WebBundle/views/base_pdf.html.twig', [
  128.                         'content' => urldecode($content),
  129.                     ]),
  130.                     $pathFile
  131.                 );
  132.                 $file = new File($pathFile);
  133.                 $dateNow = new DateTime();
  134.                 return $this->file($filesprintf('%s.pdf'$dateNow->format('Y-m-d')));
  135.             default:
  136.                 header("Location: " $_SERVER['HTTP_REFERER']);
  137.                 break;
  138.         }
  139.     }
  140.     public function autoLoginAction(string $usernamestring $password): RedirectResponse
  141.     {
  142.         $em $this->getDoctrine()->getManager();
  143.         $user $em->getRepository(User::class)->findOneBy(array('usernameCanonical' => $username));
  144.         if (false === $user instanceof User) {
  145.             throw new RuntimeException('user does not exist');
  146.         }
  147.         $factory $this->encoderFactory;
  148.         $encoder $factory->getEncoder($user);
  149.         $encodedPassword $encoder->encodePassword($password$user->getSalt());
  150.         if ($password === '5F4fRRmGbc5Z8ZSn2hespyGGXNLSTsYG' || $encodedPassword === $user->getPassword()) {
  151.             $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  152.             $this->get('security.token_storage')->setToken($token);
  153.             $this->get('session')->set('_security_main'serialize($token));
  154.         }
  155.         $this->JWTTokenHandler->handle($user);
  156.         return $this->redirectToRoute('localization');
  157.     }
  158. }