src/Controller/UserController.php line 214

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\Mailer\MailerInterface;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\Mime\Email;
  8. use Symfony\Component\HttpFoundation\{RequestResponse};
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Utils\Ticketing;
  11. use App\Entity\User;
  12. use App\Entity\Item;
  13. use App\Utils\Functions;
  14. class UserController extends AbstractController
  15. {
  16.     /**
  17.      * @var Ticketing
  18.      */
  19.     private $ticketing;
  20.     /**
  21.      * @var Functions
  22.      */
  23.     private $functions;
  24.     /**
  25.      * @var ManagerRegistry
  26.      */
  27.     private $doctrine;
  28.     /**
  29.      * @param Ticketing $ticketing
  30.      * @param Functions $functions
  31.      * @param ManagerRegistry $doctrine
  32.      */
  33.     public function __construct(Ticketing $ticketingFunctions $functionsManagerRegistry $doctrine)
  34.     {
  35.         date_default_timezone_set('UTC');
  36.         $this->ticketing $ticketing;
  37.         $this->functions $functions;
  38.         $this->doctrine  $doctrine;
  39.     }
  40.     /**
  41.      * @Route("/access", name="access")
  42.      */
  43.     public function access(Request $requestManagerRegistry $doctrineMailerInterface $mailer): Response
  44.     {
  45.         if($request->get("format")){
  46.             $format $request->get("format");
  47.         } else {
  48.             $format 1;
  49.         }
  50.         $session $request->getSession();
  51.         if($request->get("ref")){
  52.             $session->set('ref'$request->get("ref"));
  53.         }
  54.         $entityManager $doctrine->getManager();
  55.         $redirect="";
  56.         $statusLogin "";
  57.         $statusPreregister "";
  58.         $emailPreregister $request->get('email-preregister');
  59.         $emailLogin $request->get('email-login');
  60.         $password $request->get('password');
  61.         if($request->get("redirect")){
  62.             $redirect=$request->get("redirect");
  63.         }
  64.         if($emailLogin){
  65.             if($request->get('send-form-log')){
  66.                 $result $this->ticketing->curl("users/login""normal", array("userId"=>$emailLogin"password"=>$password), "POST");
  67.     
  68.                 if($result["code"] != 200){
  69.                     $statusLogin "error";
  70.                 } else{
  71.                     $session $request->getSession();
  72.                     $session->set('userId'$result["userId"]);
  73.                     if($redirect!=""){
  74.                         return $this->redirect($redirect);
  75.                     }
  76.                     return $this->redirectToRoute('home');
  77.                 }
  78.             }
  79.         }
  80.         if ($emailPreregister) {
  81.             if ($request->get('send-form-pre')) {
  82.                 $result $this->ticketing->curl("users""normal", array("userId" => $emailPreregister), "POST");
  83.                 if ($result["code"] != 201) {
  84.                     $statusPreregister "error";
  85.                 } else {
  86.                     $email = (new TemplatedEmail())
  87.                         ->from($this->getParameter('mailer_from'))
  88.                         ->to($result["userId"])
  89.                         ->subject('2º Congreso Iberoamericano de DBT - Registro')
  90.                         ->htmlTemplate('mail/register.html.twig')
  91.                         ->context([
  92.                             'userId' => $result["userId"],
  93.                             'registerCode' => $result["registerCode"],
  94.                         ]);
  95.                     try {
  96.                         $mailer->send($email);
  97.                         $statusPreregister "done";
  98.                     } catch (\Exception $e) {
  99.                         $statusPreregister 'sender_error';
  100.                     }
  101.                     $user = new User();
  102.                     $user->setUserId($result["userId"]);
  103.                     $user->setCreatedAt(new \DateTime());
  104.                     $entityManager->persist($user);
  105.                     $entityManager->flush();
  106.                 }
  107.             }
  108.         }
  109.         return $this->render('user/access.html.twig', [
  110.             'statusLogin' => $statusLogin,
  111.             'statusPreregister' => $statusPreregister,
  112.             'redirect' => $redirect,
  113.             'format' => $format
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("/register/{userId}/{registerCode}", name="register")
  118.      */
  119.     public function register(Request $requestManagerRegistry $doctrine$userId$registerCode): Response
  120.     {
  121.         $em $doctrine->getManager();
  122.         $status "";
  123.         $user $this->ticketing->curl("users/".$userId"normal", array(), "GET");
  124.         if($user["code"] == 200){
  125.             $status "error";
  126.         } else {
  127.             if($request->get('send-form')){
  128.                 $password $request->get('password');
  129.                 $name = !empty($request->get('name')) ? $request->get('name') : '';
  130.                 $surname = !empty($request->get('surname')) ? $request->get('surname') : '';
  131.                 $nationallity = !empty($request->get('nationallity')) ? $request->get('nationallity') : '';
  132.                 $document = !empty($request->get('document')) ? $request->get('document') : '';
  133.                 $country = !empty($request->get('country')) ? $request->get('country') : '';
  134.                 $city = !empty($request->get('city')) ? $request->get('city') : '';
  135.                 if ($request->get('birth_date')) {
  136.                     $birth_date $request->get('birth_date');
  137.                     $datetime = new \DateTime();
  138.                     $newDate $datetime->createFromFormat('Y-m-d'$birth_date);
  139.                 }
  140.                 $gender = !empty($request->get('gender')) ? $request->get('gender') : '';
  141.                 $phone = !empty($request->get('phone')) ? $request->get('phone') : '';
  142.                 $objUser $em->getRepository(User::class)->findOneBy(['user_id' => $userId]);
  143.                 $objUser->setName($name);
  144.                 $objUser->setSurname($surname);
  145.                 $objUser->setNationallity($nationallity);
  146.                 $objUser->setDocument($document);
  147.                 $objUser->setCountry($country);
  148.                 $objUser->setCity($city);
  149.                 if (isset($newDate)) $objUser->setBirthDate($newDate);
  150.                 $objUser->setGender($gender);
  151.                 $objUser->setPhone($phone);
  152.                 $em->persist($objUser);
  153.                 $em->flush();
  154.                 $result $this->ticketing->curl("users""normal", array("userId"=>$userId"password"=>$password"name"=>$name"surname"=>$surname"registerCode"=>$registerCode), "PATCH");
  155.                 if($result["code"] != 200){
  156.                     $status "error";
  157.                 } else {   
  158.                     $status "done";
  159.                 }
  160.             }
  161.         }
  162.         return $this->render('user/register.html.twig', [
  163.             'status' => $status,
  164.             'id' => $userId,
  165.             'code' => $registerCode
  166.         ]);
  167.     }
  168.     
  169.     /**
  170.      * @Route("/logout", name="logout")
  171.      */
  172.     public function logout(Request $request): Response
  173.     {
  174.         $session $request->getSession();
  175.         $session->clear();
  176.         return $this->redirectToRoute('home');
  177.     }
  178.     /**
  179.      * @Route("/user-profile", name="userProfile")
  180.      */
  181.     public function userProfile(Request $request): Response
  182.     {
  183.         $session $request->getSession();
  184.         if($request->get("ref")){
  185.             $session->set('ref'$request->get("ref"));
  186.         }
  187.         
  188.         $em $this->doctrine->getManager();
  189.         $userId=$this->functions->getUserLogged();
  190.         if(!$userId){
  191.             $this->addFlash(
  192.                 'notice',
  193.                 'Debe iniciar sesión como usuario'
  194.             );
  195.             return $this->redirectToRoute('access',array("redirect" => $this->generateUrl('userProfile')));
  196.         }
  197.         $tickets $this->ticketing->curl("tickets?onSaleByUser=false&user=".$userId."&limit=1000""normal", array(), "GET");
  198.         $result $this->ticketing->curl("users/".$userId"normal", array(), "GET");
  199.         $items_pending $em->getRepository(Item::class)->search("count",array("web_pending" => TRUE));
  200.         $sents $this->ticketing->curl("tickets/activity?origin=".$userId."&limit=1000&enabled=true""normal", array(), "GET");
  201.         $sells $this->ticketing->curl("tickets?onSaleByUser=true&user=".$userId."&limit=1000""normal", array(), "GET");
  202.         $userAlias $em->getRepository(User::class)->findOneBy(['user_id' => $userId])->getName();
  203.         
  204.         return $this->render('user/profile.html.twig', [
  205.             'user' => $result,
  206.             'tickets' => $tickets["tickets"],
  207.             "items_pending" => $items_pending,
  208.             "name" => $userAlias,
  209.             "sents" => $sents["activity"],
  210.             "sells" => $sells["tickets"]
  211.         ]);
  212.     }
  213.     /**
  214.      * @Route("/extra-content/{ticketId}/{source}", name="extraContent")
  215.      */
  216.     public function extraContent(Request $request$ticketId$source): Response
  217.     {
  218.         $session $request->getSession();
  219.         
  220.         $em $this->doctrine->getManager();
  221.         $userId=$this->functions->getUserLogged();
  222.         if(!$userId){
  223.             $this->addFlash(
  224.                 'notice',
  225.                 'Debe iniciar sesión como usuario'
  226.             );
  227.             return $this->redirectToRoute('access');
  228.         }
  229.         $ticket $this->ticketing->curl("tickets/".$ticketId"normal", array(), "GET");
  230.         if(!$ticket || $ticket["code"]!="200" || $ticket["user"]["userId"]!=$userId  || !$ticket["active"]){
  231.             $this->addFlash(
  232.                 'notice',
  233.                 'No se puede acceder al contenido extra'
  234.             );
  235.             return $this->redirectToRoute('home');
  236.         }
  237.         if(!array_key_exists("extraContent"$ticket) || !array_key_exists($source$ticket["extraContent"])){
  238.             $this->addFlash(
  239.                 'notice',
  240.                 'No se puede acceder al contenido extra'
  241.             );
  242.             return $this->redirectToRoute('home');
  243.         }
  244.         $source $ticket["extraContent"][$source];
  245.         // Path to your private key.  Be very careful that this file is not accessible
  246.         // from the web!
  247.         $private_key_filename = \dirname(__DIR__).'/../var/private_key.pem';
  248.         $key_pair_id 'K3HFP4XM97GS7X';
  249.         $expires time() + 86400// 1 hour
  250.         $canned_policy_stream_name $this->functions->get_canned_policy_stream_name($source$private_key_filename$key_pair_id$expires);
  251.         $client_ip $_SERVER['REMOTE_ADDR'];
  252.         $policy =
  253.         '{'.
  254.             '"Statement":['.
  255.                 '{'.
  256.                     '"Resource":"'$source '",'.
  257.                     '"Condition":{'.
  258.                         '"IpAddress":{"AWS:SourceIp":"' $client_ip '/32"},'.
  259.                         '"DateLessThan":{"AWS:EpochTime":' $expires '}'.
  260.                     '}'.
  261.                 '}'.
  262.             ']' .
  263.             '}';
  264.         $custom_policy_stream_name $this->functions->get_custom_policy_stream_name($source$private_key_filename$key_pair_id$policy);
  265.         //echo urldecode($canned_policy_stream_name);die();
  266.         return $this->redirect(urldecode($custom_policy_stream_name));
  267.         
  268.         return $this->render('user/extra-content.html.twig', [
  269.             'source' => $source,
  270.             'canned_policy_stream_name' => urldecode($canned_policy_stream_name),
  271.             'custom_policy_stream_name' => urldecode($custom_policy_stream_name)
  272.         ]);
  273.     }
  274.     /**
  275.      * @Route("/recover-password", name="recoverPassword")
  276.      */
  277.     public function recoverPassword(Request $requestMailerInterface $mailer): Response
  278.     {
  279.         $status "";
  280.         $email $request->get('email');
  281.         $result $this->ticketing->curl("users/".$email."/recover""normal", array(), "GET");
  282.         if($email && $result){
  283.             if($result["code"] != 200 || (!array_key_exists('recoverCode'$result) && !array_key_exists('registerCode'$result))){
  284.                 $status "error";
  285.             } else {
  286.                 if(array_key_exists('registerCode'$result)){
  287.                     $mail = (new TemplatedEmail())
  288.                     ->from($this->getParameter('mailer_from'))
  289.                     ->to($email)
  290.                     ->subject('2º Congreso Iberoamericano de DBT - Registro')
  291.                     ->htmlTemplate('mail/register.html.twig')
  292.                     ->context([
  293.                         'userId' => $email,
  294.                         'registerCode' => $result["registerCode"],
  295.                     ]);
  296.                 } else {
  297.                     $mail = (new TemplatedEmail())
  298.                     ->from($this->getParameter('mailer_from'))
  299.                     ->to($email)
  300.                     ->subject('2º Congreso Iberoamericano de DBT - Recuperar contraseña')
  301.                     ->htmlTemplate('mail/recover-password.html.twig')
  302.                     ->context([
  303.                         'userId' => $email,
  304.                         'recoverCode' => $result["recoverCode"],
  305.                     ]);
  306.                 }
  307.                 try {
  308.                     $mailer->send($mail);
  309.                     $status "done";
  310.                 } catch (\Exception $e) {
  311.                     $status 'sender_error';
  312.                 }
  313.             }
  314.         }
  315.         return $this->render('user/recover-password.html.twig', [
  316.             'status'=>$status
  317.         ]);
  318.     }
  319.     /**
  320.      * @Route("/change-password/{userId}/{recoverCode}", name="changePassword")
  321.      */
  322.     public function changePassword(Request $request$userId$recoverCode): Response
  323.     {
  324.         $status "";
  325.         $password $request->get('password');
  326.         $check $this->ticketing->curl("users/".$userId"normal", array(), "GET");
  327.         if($check["code"] != 200 || !array_key_exists('recoverCode'$check) || $check["recoverCode"]!=$recoverCode){
  328.             $status "error2";
  329.         }
  330.         else{
  331.             if($request->get('send-form')){
  332.                 $result $this->ticketing->curl("users/change-password""normal", array("userId"=>$userId"password"=>$password"recoverCode"=>$recoverCode), "PATCH");
  333.                 if($result["code"] != 200){
  334.                     $status "error";
  335.                 } else {
  336.                     $status "done";
  337.                 }
  338.             }
  339.         }
  340.         return $this->render('user/change-password.html.twig', [
  341.             'status' => $status,
  342.             'id' => $userId,
  343.             'code' => $recoverCode
  344.         ]);
  345.     }
  346. }