nineskeletor/src/Controller/RestController.php

712 lines
20 KiB
PHP

<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as FOSRest;
use OpenApi\Annotations as OA;
use Symfony\Component\HttpFoundation\Request;
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);
}
/**
* getAllNiveau03s.
*
* @FOSRest\Get("/rest/getAllNiveau03s")
* @OA\Response(
* response=200,
* description="get all niveau03"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllNiveau03s(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 = [];
$niveau03s = $em->getRepository("App\Entity\Niveau03")->findAll();
foreach ($niveau03s as $niveau03) {
array_push($output, $this->niveau03Format($niveau03, true));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneNiveau03.
*
* @FOSRest\Get("/rest/getOneNiveau03")
* @OA\Response(
* response=200,
* description="get one niveau03 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 getOneNiveau03(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 = [];
$niveau03 = $em->getRepository("App\Entity\Niveau03")->findOneBy(['label' => $request->headers->get('label')]);
if (!$niveau03) {
$view = $this->view('Niveau03 inconnu', 403);
return $this->handleView($view);
}
$output = $this->niveau03Format($niveau03, true);
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getAllNiveau04s.
*
* @FOSRest\Get("/rest/getAllNiveau04s")
* @OA\Response(
* response=200,
* description="get all niveau04"
* )
* )
* @OA\Parameter(
* name="key",
* in="header",
* required=true,
* description="APIKey",
* @OA\Schema(type="string")
* )
*/
public function getAllNiveau04s(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 = [];
$niveau04s = $em->getRepository("App\Entity\Niveau04")->findAll();
foreach ($niveau04s as $niveau04) {
array_push($output, $this->niveau04Format($niveau04, true));
}
$view = $this->view($output, 200);
return $this->handleView($view);
}
/**
* getOneNiveau04.
*
* @FOSRest\Get("/rest/getOneNiveau04")
* @OA\Response(
* response=200,
* description="get one niveau04 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 getOneNiveau04(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 = [];
$niveau04 = $em->getRepository("App\Entity\Niveau04")->findOneBy(['label' => $request->headers->get('label')]);
if (!$niveau04) {
$view = $this->view('Niveau04 inconnu', 403);
return $this->handleView($view);
}
$output = $this->niveau04Format($niveau04, 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['userniveau03'] = $this->niveau03Format($user->getNiveau03());
$output['userniveau04'] = $this->niveau03Format($user->getNiveau04());
$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 niveau03Format($niveau03, $withmembers = false)
{
if (!$niveau03) {
return null;
}
$output = [];
$output['niveau03id'] = $niveau03->getId();
$output['niveau03label'] = $niveau03->getLabel();
if ($withmembers) {
$output['niveau03niveau01'] = $this->niveau01Format($niveau03->getNiveau02()->getNiveau01());
$output['niveau03niveau02'] = $this->niveau02Format($niveau03->getNiveau02());
$output['niveau03users'] = [];
foreach ($niveau03->getUsers() as $user) {
array_push($output['niveau03users'], ['userid' => $user->getId(), 'userlogin' => $user->getUsername()]);
}
if (empty($output['niveau03users'])) {
$output['niveau03users'] = null;
}
}
return $output;
}
private function niveau04Format($niveau04, $withmembers = false)
{
if (!$niveau04) {
return null;
}
$output = [];
$output['niveau04id'] = $niveau04->getId();
$output['niveau04label'] = $niveau04->getLabel();
if ($withmembers) {
$output['niveau04niveau01'] = $this->niveau01Format($niveau04->getNiveau03()->getNiveau02()->getNiveau01());
$output['niveau04niveau02'] = $this->niveau02Format($niveau04->getNiveau03()->getNiveau02());
$output['niveau04niveau03'] = $this->niveau02Format($niveau04->getNiveau03());
$output['niveau04users'] = [];
foreach ($niveau04->getUsers() as $user) {
array_push($output['niveau04users'], ['userid' => $user->getId(), 'userlogin' => $user->getUsername()]);
}
if (empty($output['niveau04users'])) {
$output['niveau04users'] = 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;
}
}