src/Controller/RestController.php line 173

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use FOS\RestBundle\Controller\AbstractFOSRestController;
  5. use FOS\RestBundle\Controller\Annotations as FOSRest;
  6. use OpenApi\Annotations as OA;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Entity\Bookmark;
  9. class RestController extends AbstractFOSRestController
  10. {
  11.     /**
  12.      * login
  13.      *
  14.      * @FOSRest\Get("/rest/login")
  15.      * @OA\Response(
  16.      *     response=200,
  17.      *     description="Permet de tester l'accès à ninegate"
  18.      *     )
  19.      * )
  20.      * @OA\Parameter(
  21.      *     name="key",
  22.      *     in="header",
  23.      *     required=true,
  24.      *     description="APIKey",
  25.      *     @OA\Schema(type="string")
  26.      * )
  27.      */
  28.     public function login(Request $request)
  29.     {
  30.         $key=$request->headers->get('key');
  31.         $output=[];
  32.         // Vérification de la clé
  33.         $realkey $this->getParameter("app_secret");
  34.         if($key!=$realkey) {
  35.             $view $this->view("API Key inconnue"403);
  36.             return $this->handleView($view);
  37.         }
  38.         $view $this->view("API Key OK"200);
  39.         return $this->handleView($view);
  40.     }
  41.     /**
  42.      * user
  43.      *
  44.      * @FOSRest\Get("/rest/user/{login}")
  45.      * @OA\Response(
  46.      *     response=200,
  47.      *     description="Récupération des informations d'un utilisateur"
  48.      *     )
  49.      * )
  50.      * @OA\Parameter(
  51.      *     name="key",
  52.      *     in="header",
  53.      *     required=true,
  54.      *     description="APIKey",
  55.      *     @OA\Schema(type="string")
  56.      * )
  57.      * @OA\Parameter(
  58.      *     name="only",
  59.      *     in="header",
  60.      *     required=false,
  61.      *     description="liste des informations désirés = user, items, bookmarks, groups, alerts, calendars\npar défaut tout",
  62.      *     @OA\Schema(type="string")
  63.      * )
  64.      * @OA\Parameter(
  65.      *     name="ssoitems",
  66.      *     in="header",
  67.      *     required=false,
  68.      *     description="liste des items issu du ssoitems",
  69.      *     @OA\Schema(type="string")
  70.      * )
  71.      */
  72.     public function user($loginRequest $requestManagerRegistry $em)
  73.     {
  74.         $key=$request->headers->get('key');
  75.         $only=($request->headers->get('only')?explode(",",$request->headers->get('only')):"");
  76.         $ssoitems=($request->headers->get('ssoitems')?explode(",",$request->headers->get('ssoitems')):[]);
  77.         // Vérification de la clé
  78.         $realkey $this->getParameter("app_secret");
  79.         if($key!=$realkey) {
  80.             $view $this->view("API Key inconnue"403);
  81.             return $this->handleView($view);
  82.         }
  83.         $output = [];
  84.         // Récupérer l'utilisateur
  85.         $user=$em->getRepository('App\Entity\User')->findOneBy(["username"=>$login]);
  86.         // Format de sortie
  87.         if($only==""||in_array("user",$only))           $output["user"]             = [];
  88.         if($only==""||in_array("bookmarks",$only))      $output["bookmarks"]        = [];
  89.         if($only==""||in_array("items",$only))          $output["items"]            = [];
  90.         if($only==""||in_array("items",$only))          $output["itemcategorys"]    = [];
  91.         if($only==""||in_array("groups",$only))         $output["groups"]           = [];
  92.         if($only==""||in_array("alerts",$only))         $output["alerts"]           = [];
  93.         if($only==""||in_array("alerts",$only))         $output["alertcategorys"]   = [];
  94.         if($only==""||in_array("calendars",$only))      $output["calendars"]        = [];
  95.         if($only==""||in_array("calendars",$only))      $output["calendarevents"]   = [];
  96.         
  97.         $bookmarks=null;
  98.         $items=null;
  99.         $itemcategorys=null;
  100.         $weburl=$this->getParameter("protocole")."://".$this->getParameter("weburl")."/".$this->getParameter("alias")."/";
  101.         $em->getRepository("App\Entity\Item")->getUserItems($user,$bookmarks,$items,$itemcategorys,null,$ssoitems,4);
  102.         //$this->getDoctrine()->getRepository("App\Entity\Page")->getPagesUser($user,null,$entity,$pagesuser,$pagesadmin,$groupsshared);
  103.         // Construction de la réponse
  104.         if($user&&($only==""||in_array("user",$only))) {
  105.             $output["user"]["firstname"]    = $user->getFirstname();
  106.             $output["user"]["lastname"]     = $user->getLastname();
  107.             $output["user"]["email"]        = $user->getEmail();
  108.             $output["user"]["avatar"]       = $weburl."uploads/avatar/".$user->getAvatar();
  109.             $output["user"]["niveau01"]     = $user->getNiveau01()->getLabel();
  110.             $output["user"]["niveau02"]     = ($user->getNiveau02()?$user->getNiveau02()->getLabel():null);
  111.             $output["user"]["role"]         = $user->getRole();
  112.             $output["user"]["usualname"]    = $user->getUsualname();
  113.         }
  114.         if($only==""||in_array("bookmarks",$only)) {
  115.             if($bookmarks) {
  116.                 foreach($bookmarks as $bookmark) {
  117.                     $tmp=[];
  118.                     $tmp["id"]          = $bookmark->getId();
  119.                     $tmp["title"]       = $bookmark->getTitle();
  120.                     $tmp["url"]         = $bookmark->getUrl();
  121.                     $tmp["target"]      = $bookmark->getTarget();
  122.                     $tmp["item"]        = ($bookmark->getItem()?$bookmark->getItem()->getId():null);
  123.                     $tmp["order"]       = 0;
  124.                     $tmp["color"]       = "#".($bookmark->getColor()?$bookmark->getColor():$this->get('session')->get('color')["main"]);
  125.                     $tmp["icon"]        = $weburl.($bookmark->getIcon()?$bookmark->getIcon()->getLabel():"uploads/icon/icon_pin.png");
  126.                     array_push($output["bookmarks"],$tmp);
  127.                 }
  128.             }
  129.         }
  130.         if($only==""||in_array("items",$only)) {
  131.             if($items) {
  132.                 foreach($items as $item) {
  133.                     $tmp=[];
  134.                     $tmp["id"]          = $item->getId();
  135.                     $tmp["title"]       = $item->getTitle();
  136.                     $tmp["url"]         = $item->getUrl();
  137.                     $tmp["target"]      = $item->getTarget();
  138.                     $tmp["order"]       = $item->getRoworder();
  139.                     $tmp["color"]       = "#".($item->getColor()?$item->getColor():$this->get('session')->get('color')["main"]);
  140.                     $tmp["icon"]        = $weburl.($item->getIcon()?$item->getIcon()->getLabel():"uploads/icon/icon_pin.png");
  141.                     $tmp["essential"]   = $item->getEssential();
  142.                     $tmp["category"]    = $item->getItemcategory()->getId();
  143.                     array_push($output["items"],$tmp);
  144.                 }
  145.             }
  146.             if($itemcategorys) {
  147.                 foreach($itemcategorys as $itemcategory) {
  148.                     $tmp=[];
  149.                     $tmp["id"]          = $itemcategory->getId();
  150.                     $tmp["title"]       = $itemcategory->getLabel();
  151.                     $tmp["order"]       = $itemcategory->getRoworder();
  152.                     $tmp["color"]       = "#".($itemcategory->getColor()?$itemcategory->getColor():$this->get('session')->get('color')["main"]);
  153.                     array_push($output["itemcategorys"],$tmp);
  154.                 }
  155.             }
  156.         }
  157.         if($user&&($only==""||in_array("groups",$only))) {
  158.             foreach($user->getGroups() as $usergroup) {
  159.                 $tmp=[];
  160.                 $tmp["id"]          = $usergroup->getGroup()->getId();
  161.                 $tmp["title"]       = $usergroup->getGroup()->getLabel();
  162.                 array_push($output["groups"],$tmp);
  163.             }
  164.         }
  165.         if($only==""||in_array("alerts",$only)) {
  166.             $alerts=$em->getRepository("App\Entity\Alert")->getUserAlerts($user,null,null,$ssoitems);
  167.             foreach($alerts as $alert) {
  168.                 $tmp=[];
  169.                 $tmp["id"]          = $alert->getId();
  170.                 $tmp["title"]       = $alert->getTitle();
  171.                 $tmp["order"]       = $alert->getRoworder();
  172.                 $tmp["category"]    = $alert->getAlertcategory()->getId();
  173.                 $tmp["description"] = $alert->getContent();
  174.                 $tmp["fghideable"]  = $alert->getFghideable();
  175.                 array_push($output["alerts"],$tmp);
  176.             }
  177.             $alertcategorys=$em->getRepository("App\Entity\Alertcategory")->findAll();
  178.             foreach($alertcategorys as $alertcategory) {
  179.                 $tmp=[];
  180.                 $tmp["id"]          = $alertcategory->getId();
  181.                 $tmp["title"]       = $alertcategory->getLabel();
  182.                 $tmp["color"]       = "#".($alertcategory->getColor()?$alertcategory->getColor():$this->get('session')->get('color')["main"]);
  183.                 $tmp["icon"]        = $weburl.($alertcategory->getIcon()?$alertcategory->getIcon()->getLabel():"uploads/icon/icon_pin.png");
  184.                 array_push($output["alertcategorys"],$tmp);
  185.             }
  186.         }
  187.         if($only==""||in_array("calendars",$only)) {
  188.             $events=$em->getRepository("App\Entity\Calendarevent")->getUserCalendarevents($user,$this->get('session')->get('color')["main"],null,null,$firstcalendar);
  189.             foreach($events as $event) {
  190.                 $tmp=[];
  191.                 $tmp["id"]          = $event["id"];
  192.                 $tmp["title"]       = $event["title"];
  193.                 $tmp["description"] = $event["description"];
  194.                 $tmp['start']       = $event["start"];
  195.                 $tmp['end']         = $event["end"];
  196.                 $tmp['allday']      = $event["allDay"];
  197.                 $tmp['calendar']    = $event["calendar"];
  198.                 array_push($output["calendarevents"],$tmp);
  199.                 $tmp=[];
  200.                 $tmp["id"]          = $event["calendar"];
  201.                 $tmp["title"]       = $event["name"];
  202.                 $tmp["color"]       = $event["color"];
  203.                 if(!in_array($tmp,$output["calendars"]))
  204.                     array_push($output["calendars"],$tmp);
  205.             }
  206.         }
  207.         
  208.         // Retour
  209.         $view $this->view($output200);
  210.         return $this->handleView($view);
  211.     }    
  212.     /**
  213.      * users
  214.      *
  215.      * @FOSRest\Get("/rest/users")
  216.      * @OA\Response(
  217.      *     response=200,
  218.      *     description="Liste l'ensemble des utilisateurs de ninegate et retourne ses attributs"
  219.      *     )
  220.      * )
  221.      * @OA\Parameter(
  222.      *     name="key",
  223.      *     in="header",
  224.      *     required=true,
  225.      *     description="APIKey",
  226.      *     @OA\Schema(type="string")
  227.      * )
  228.      */
  229.     public function users(Request $requestManagerRegistry $em)
  230.     {
  231.         $key=$request->headers->get('key');
  232.         // Vérification de la clé
  233.         $realkey $this->getParameter("app_secret");
  234.         if($key!=$realkey) {
  235.             $view $this->view("API Key inconnue"403);
  236.             return $this->handleView($view);
  237.         }
  238.         $weburl="https://".$this->getParameter("weburl")."/".$this->getParameter("alias");
  239.         $output = [];
  240.         $users=$em->getRepository('App\Entity\User')->findAll();
  241.         foreach($users as $user) {
  242.             // Construction de la réponse
  243.             $output[$user->getId()]["id"]          = $user->getId();
  244.             $output[$user->getId()]["username"]    = $user->getUsername();
  245.             $output[$user->getId()]["firstname"]   = $user->getFirstname();
  246.             $output[$user->getId()]["lastname"]    = $user->getLastname();
  247.             $output[$user->getId()]["email"]       = $user->getEmail();
  248.             $output[$user->getId()]["avatar"]      = $weburl."/uploads/avatar/".$user->getAvatar();
  249.             $output[$user->getId()]["role"]        = $user->getRole();
  250.         }
  251.         // Retour
  252.         $view $this->view($output200);
  253.         return $this->handleView($view);
  254.     }
  255.     /**
  256.      * groups
  257.      *
  258.      * @FOSRest\Get("/rest/groups")
  259.      * @OA\Response(
  260.      *     response=200,
  261.      *     description="Liste l'ensemble des groupes de ninegate et retourne ses attributs ainsi que les membres des groupes en question"
  262.      *     )
  263.      * )
  264.      * @OA\Parameter(
  265.      *     name="key",
  266.      *     in="header",
  267.      *     required=true,
  268.      *     description="APIKey",
  269.      *     @OA\Schema(type="string")
  270.      * )
  271.      */
  272.     public function groups(Request $requestManagerRegistry $em)
  273.     {
  274.         $key=$request->headers->get('key');
  275.         // Vérification de la clé
  276.         $realkey $this->getParameter("app_secret");
  277.         if($key!=$realkey) {
  278.             $view $this->view("API Key inconnue"403);
  279.             return $this->handleView($view);
  280.         }
  281.         $output = [];
  282.         $groups=$em->getRepository('App\Entity\Group')->findAll();
  283.         foreach($groups as $group) {
  284.             // Construction de la réponse
  285.             $output[$group->getId()]["id"]      = $group->getId();
  286.             $output[$group->getId()]["name"]    = $group->getLabel();
  287.             $output[$group->getId()]["users"]   = [];
  288.             $usergroups=$group->getUsers();
  289.             foreach($usergroups as $usergroup) {
  290.                 $user=$usergroup->getUser();
  291.                 $output[$group->getId()]["users"][$user->getId()]["id"]=$user->getId();
  292.                 $output[$group->getId()]["users"][$user->getId()]["username"]=$user->getUsername();
  293.             }
  294.         }
  295.         // Retour
  296.         $view $this->view($output200);
  297.         return $this->handleView($view);    
  298.     }
  299.     /**
  300.      * bookmarkadd
  301.      *
  302.      * @FOSRest\Post("/rest/bookmark/add")
  303.      * @OA\Response(
  304.      *     response=200,
  305.      *     description="Ajout d'un item ninegate existant comme bookmark d'un utilisateur"
  306.      *     )
  307.      * )
  308.      * @OA\Parameter(
  309.      *     name="key",
  310.      *     in="header",
  311.      *     required=true,
  312.      *     description="APIKey",
  313.      *     @OA\Schema(type="string")
  314.      * )
  315.      * @OA\Parameter(
  316.      *     name="login",
  317.      *     in="header",
  318.      *     required=true,
  319.      *     description="uid de l'utilisateur sur lequel on souhaite ajouter un bookmark",
  320.      *     @OA\Schema(type="string")
  321.      * )
  322.      * @OA\Parameter(
  323.      *     name="iditem",
  324.      *     in="header",
  325.      *     required=true,
  326.      *     description="id de l'item ninegate qui doit etre ajouté en tant que bookmark",
  327.      *     @OA\Schema(type="string")
  328.      * )
  329.      */
  330.     public function bookmarkadd(Request $requestManagerRegistry $em) {
  331.         $key=$request->headers->get('key');
  332.         $login=$request->headers->get('login');
  333.         $iditem=$request->headers->get('iditem');
  334.         
  335.         // Vérification de la clé
  336.         $realkey $this->getParameter("app_secret");
  337.         if($key!=$realkey) {
  338.             $view $this->view("API Key inconnue"403);
  339.             return $this->handleView($view);
  340.         }
  341.         // Tester l'existance du login
  342.         $user=$em->getRepository('App\Entity\User')->findOneBy(["username"=>$login]);
  343.         if(!$user) {
  344.             $view $this->view("user not exist"400);
  345.             return $this->handleView($view);            
  346.         }        
  347.         // Tester l'existance de l'item
  348.         $item=$em->getRepository('App\Entity\Item')->find($iditem);
  349.         if(!$item) {
  350.             $view $this->view("item not exist"400);
  351.             return $this->handleView($view); 
  352.         }    
  353.         
  354.         $bookmark $em->getRepository('App\Entity\Bookmark')->findOneBy(["user"=>$user,"item"=>$item]);
  355.         if(!$bookmark) {
  356.             $bookmark = new Bookmark();
  357.             $bookmark->setTitle($item->getTitle());
  358.             $bookmark->setSubtitle($item->getSubtitle());
  359.             $bookmark->setUrl($item->getUrl());
  360.             $bookmark->setIcon($item->getIcon());
  361.             $bookmark->setColor($item->getColor());
  362.             $bookmark->setTarget($item->getTarget());
  363.             $bookmark->setItem($item);
  364.             $bookmark->setUser($user);
  365.             $em->getManager()->persist($bookmark);
  366.             $em->getManager()->flush();            
  367.         }
  368.         $output=$bookmark->getId();   
  369.         $view $this->view($output200);
  370.         return $this->handleView($view); 
  371.     }
  372.     /**
  373.      * bookmarkdel
  374.      *
  375.      * @FOSRest\Delete("/rest/bookmark/del")
  376.      * @OA\Response(
  377.      *     response=200,
  378.      *     description="Suppression  d'un bookmark"
  379.      *     )
  380.      * )
  381.      * @OA\Parameter(
  382.      *     name="key",
  383.      *     in="header",
  384.      *     required=true,
  385.      *     description="APIKey",
  386.      *     @OA\Schema(type="string")
  387.      * )
  388.      * @OA\Parameter(
  389.      *     name="idbookmark",
  390.      *     in="header",
  391.      *     required=true,
  392.      *     description="id du bookmark à supprimer",
  393.      *     @OA\Schema(type="string")
  394.      * )
  395.      */    
  396.     public function bookmarkdel(Request $requestManagerRegistry $em) {
  397.       
  398.         $key=$request->headers->get('key');
  399.         $idbookmark=$request->headers->get('idbookmark');
  400.         
  401.         // Vérification de la clé
  402.         $realkey $this->getParameter("app_secret");
  403.         if($key!=$realkey) {
  404.             $view $this->view("API Key inconnue"403);
  405.             return $this->handleView($view);
  406.         }
  407.         // Tester l'existance du bookmark
  408.         $bookmark=$em->getRepository('App\Entity\Bookmark')->find($idbookmark);
  409.         if(!$bookmark) {
  410.             $view $this->view("bookmark not exist"400);
  411.             return $this->handleView($view);               
  412.         }    
  413.         $em->getManager()->remove($bookmark);
  414.         $em->getManager()->flush();        
  415.         
  416.         $output=[];
  417.         $view $this->view($output200);
  418.         return $this->handleView($view);  
  419.     }
  420.     /**
  421.      * alerthide
  422.      *
  423.      * @FOSRest\Post("/rest/alert/hide")
  424.      * @OA\Response(
  425.      *     response=200,
  426.      *     description="Cache une alert à un utilisateur"
  427.      *     )
  428.      * )
  429.      * @OA\Parameter(
  430.      *     name="key",
  431.      *     in="header",
  432.      *     required=true,
  433.      *     description="APIKey",
  434.      *     @OA\Schema(type="string")
  435.      * )
  436.      * @OA\Parameter(
  437.      *     name="login",
  438.      *     in="header",
  439.      *     required=true,
  440.      *     description="uid de l'utilisateur sur lequel on souhaite masquer une alerte",
  441.      *     @OA\Schema(type="string")
  442.      * )
  443.      * @OA\Parameter(
  444.      *     name="idalert",
  445.      *     in="header",
  446.      *     required=true,
  447.      *     description="id de l'alert à chacher",
  448.      *     @OA\Schema(type="string")
  449.      * )
  450.      */ 
  451.     public function alerthide(Request $requestManagerRegistry $em) {
  452.         $key=$request->headers->get('key');
  453.         $login=$request->headers->get('login');
  454.         $idalert=$request->headers->get('idalert');
  455.         
  456.         // Vérification de la clé
  457.         $realkey $this->getParameter("app_secret");
  458.         if($key!=$realkey) {
  459.             $view $this->view("API Key inconnue"403);
  460.             return $this->handleView($view);
  461.         }
  462.         // Tester l'existance du login
  463.         $user=$em->getRepository('App\Entity\User')->findOneBy(["username"=>$login]);
  464.         if(!$user) {
  465.             $view $this->view("user not exist"400);
  466.             return $this->handleView($view);   
  467.         }        
  468.         // Tester l'existance de l'alert
  469.         $alert=$em->getRepository('App\Entity\Alert')->find($idalert);
  470.         if(!$alert) {
  471.             $view $this->view("alert not exist"400);
  472.             return $this->handleView($view);   
  473.         }    
  474.         // Tester que l'alert est masquable
  475.         if(!$alert->getFghideable()) {
  476.             $view $this->view("alert not hideable"400);
  477.             return $this->handleView($view);           
  478.         }  
  479.         // Masquer l'alert
  480.         if(!$alert->getReaders()->contains($user)) {
  481.             $alert->addReader($user);
  482.             $em->getManager()->flush();
  483.         }  
  484.         
  485.         $output=[];
  486.         $view $this->view($output200);
  487.         return $this->handleView($view);  
  488.     }
  489. }