nineskeletor/src/Controller/RestController.php

468 lines
13 KiB
PHP

<?php
namespace App\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as FOSRest;
use OpenApi\Annotations as OA;
use Doctrine\Persistence\ManagerRegistry;
class RestController extends AbstractFOSRestController
{
private $output=[];
private $cpt;
/**
* getAllUsers
*
*
* @FOSRest\Get("/rest/getAllUsers")
* @OA\Response(
* response=200,
* description="get all users"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllUsers(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$users=$em->getRepository("App\Entity\User")->findAll();
foreach($users as $user) {
array_push($output,$this->userFormat($user));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneUser
*
*
* @FOSRest\Get("/rest/getOneUser")
* @OA\Response(
* response=200,
* description="get one user by login"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
* @OA\Parameter(
* name="login",
* in="header",
* required=true,
* description="Login",
* @OA\Schema(type="string")
* )
*/
public function getOneUser(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$user=$em->getRepository("App\Entity\User")->findOneBy(["username"=>$request->headers->get("login")]);
if(!$user) {
$view = $this->view("Utilisateur inconnue", 403);
return $this->handleView($view);
}
$output=$this->userFormat($user);
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getAllNiveau01s
*
*
* @FOSRest\Get("/rest/getAllNiveau01s")
* @OA\Response(
* response=200,
* description="get all niveau01"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllNiveau01s(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$niveau01s=$em->getRepository("App\Entity\Niveau01")->findAll();
foreach($niveau01s as $niveau01) {
array_push($output,$this->niveau01Format($niveau01,true));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneNiveau01
*
*
* @FOSRest\Get("/rest/getOneNiveau01")
* @OA\Response(
* response=200,
* description="get one niveau01 by label"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
* @OA\Parameter(
* name="label",
* in="header",
* required=true,
* description="Label",
* @OA\Schema(type="string")
* )
*/
public function getOneNiveau01(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$niveau01=$em->getRepository("App\Entity\Niveau01")->findOneBy(["label"=>$request->headers->get("label")]);
if(!$niveau01) {
$view = $this->view("Niveau01 inconnu", 403);
return $this->handleView($view);
}
$output=$this->niveau01Format($niveau01,true);
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getAllNiveau02s
*
*
* @FOSRest\Get("/rest/getAllNiveau02s")
* @OA\Response(
* response=200,
* description="get all niveau02"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllNiveau02s(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$niveau02s=$em->getRepository("App\Entity\Niveau02")->findAll();
foreach($niveau02s as $niveau02) {
array_push($output,$this->niveau02Format($niveau02,true));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneNiveau02
*
*
* @FOSRest\Get("/rest/getOneNiveau02")
* @OA\Response(
* response=200,
* description="get one niveau02 by label"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
* @OA\Parameter(
* name="label",
* in="header",
* required=true,
* description="Label",
* @OA\Schema(type="string")
* )
*/
public function getOneNiveau02(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$niveau02=$em->getRepository("App\Entity\Niveau02")->findOneBy(["label"=>$request->headers->get("label")]);
if(!$niveau02) {
$view = $this->view("Niveau02 inconnu", 403);
return $this->handleView($view);
}
$output=$this->niveau02Format($niveau02,true);
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getAllGroups
*
*
* @FOSRest\Get("/rest/getAllGroups")
* @OA\Response(
* response=200,
* description="get all group"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllGroups(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$groups=$em->getRepository("App\Entity\Group")->findAll();
foreach($groups as $group) {
if($group->getId()<0) continue;
array_push($output,$this->groupFormat($group,true));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneGroup
*
*
* @FOSRest\Get("/rest/getOneGroup")
* @OA\Response(
* response=200,
* description="get one group by label"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
* @OA\Parameter(
* name="label",
* in="header",
* required=true,
* description="Label",
* @OA\Schema(type="string")
* )
*/
public function getOneGroup(Request $request,ManagerRegistry $em) {
set_time_limit(0);
ini_set('memory_limit', '1024M');
// Récupération des parametres
if(!$this->iskey($request->headers->get("key"))) {
$view = $this->view("API Key inconnue", 403);
return $this->handleView($view);
}
$output = [];
$group=$em->getRepository("App\Entity\Group")->findOneBy(["label"=>$request->headers->get("label")]);
if(!$group) {
$view = $this->view("Group inconnu", 403);
return $this->handleView($view);
}
$output=$this->groupFormat($group,true);
$view = $this->view($output, 200);
return $this->handleView($view);
}
private function iskey($key) {
return ($key==$this->getParameter("appSecret"));
}
private function userFormat($user) {
$output=[];
$output["userid"]=$user->getId();
$output["userlogin"]=$user->getUsername();
$output["userlastname"]=$user->getLastname();
$output["userfirstname"]=$user->getFirstname();
$output["useremail"]=$user->getEmail();
$output["userjob"]=$user->getJob();
$output["userposition"]=$user->getPosition();
$output["userpostaladress"]=$user->getPostaladress();
$output["usertelephonenumber"]=$user->getTelephonenumber();
$output["useravatar"]="https://".str_replace("//","/",$this->getParameter("appWeburl").$this->getParameter("appAlias").$this->generateUrl('app_minio_image',["file"=>"avatar/".$user->getAvatar()],true));
$output["userniveau01"]=$this->niveau01Format($user->getNiveau01());
$output["userniveau02"]=$this->niveau02Format($user->getNiveau02());
$output["usergroups"]=[];
foreach($user->getGroups() as $usergroup) {
$groupFormat=$this->groupFormat($usergroup->getGroup());
if($groupFormat) array_push($output["usergroups"],$groupFormat);
}
if(empty($output["usergroups"])) $output["usergroups"]=null;
return $output;
}
private function niveau01Format($niveau01,$withmembers=false){
if(!$niveau01) return null;
$output=[];
$output["niveau01id"]=$niveau01->getId();
$output["niveau01label"]=$niveau01->getLabel();
if($withmembers) {
$output["niveau01users"]=[];
foreach($niveau01->getUsers() as $user) {
array_push($output["niveau01users"],["userid"=>$user->getId(),"userlogin"=>$user->getUsername()]);
}
if(empty($output["niveau01users"])) $output["niveau01users"]=null;
}
return $output;
}
private function niveau02Format($niveau02,$withmembers=false){
if(!$niveau02) return null;
$output=[];
$output["niveau02id"]=$niveau02->getId();
$output["niveau02label"]=$niveau02->getLabel();
if($withmembers) {
$output["niveau02niveau01"]=$this->niveau01Format($niveau02->getNiveau01());
$output["niveau02users"]=[];
foreach($niveau02->getUsers() as $user) {
array_push($output["niveau02users"],["userid"=>$user->getId(),"userlogin"=>$user->getUsername()]);
}
if(empty($output["niveau02users"])) $output["niveau02users"]=null;
}
return $output;
}
private function groupFormat($group,$withmembers=false){
if(!$group||$group->getId()<0) return null;
$output=[];
$output["groupid"]=$group->getId();
$output["grouplabel"]=$group->getLabel();
if($withmembers) {
$output["groupusers"]=[];
foreach($group->getUsers() as $usergroup) {
array_push($output["groupusers"],["userid"=>$usergroup->getUser()->getId(),"userlogin"=>$usergroup->getUser()->getUsername()]);
}
if(empty($output["groupusers"])) $output["groupusers"]=null;
}
return $output;
}
}