first commit symfony 6
This commit is contained in:
199
src/Controller/WhitelistController.php
Normal file
199
src/Controller/WhitelistController.php
Normal file
@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
use App\Entity\Whitelist as Entity;
|
||||
use App\Form\WhitelistType as Form;
|
||||
|
||||
class WhitelistController extends AbstractController
|
||||
{
|
||||
private $data="whitelist";
|
||||
private $entity="App\Entity\Whitelist";
|
||||
private $twig="Whitelist/";
|
||||
private $route="app_admin_whitelist";
|
||||
|
||||
public function list(): Response
|
||||
{
|
||||
return $this->render($this->twig.'list.html.twig',[
|
||||
"useheader"=>true,
|
||||
"usemenu"=>false,
|
||||
"usesidebar"=>true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function tablelist(Request $request,ManagerRegistry $em): Response
|
||||
{
|
||||
$query=$request->query->all();
|
||||
$start=$query['start'];
|
||||
$length=$query['length'];
|
||||
$search=$query['search'];
|
||||
$draw= $query['draw'];
|
||||
$ordercolumn=$query['order'][0]['column'];
|
||||
$orderdir=$query['order'][0]['dir'];
|
||||
|
||||
// Nombre total d'enregistrement
|
||||
$total = $em->getManager()->createQueryBuilder()->select('COUNT(entity)')->from($this->entity,'entity')->getQuery()->getSingleScalarResult();
|
||||
|
||||
// Nombre d'enregistrement filtré
|
||||
if(!$search||$search["value"]=="")
|
||||
$totalf = $total;
|
||||
else {
|
||||
$totalf= $em->getManager()->createQueryBuilder()
|
||||
->select('COUNT(entity)')
|
||||
->from($this->entity,'entity')
|
||||
->where('entity.label LIKE :value')
|
||||
->setParameter("value", "%".$search["value"]."%")
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
// Construction du tableau de retour
|
||||
$output = array(
|
||||
'draw' => $draw,
|
||||
'recordsFiltered' => $totalf,
|
||||
'recordsTotal' => $total,
|
||||
'data' => array(),
|
||||
);
|
||||
|
||||
// Parcours des Enregistrement
|
||||
$qb = $em->getManager()->createQueryBuilder();
|
||||
$qb->select('entity')->from($this->entity,'entity');
|
||||
if($search&&$search["value"]!="") {
|
||||
$qb ->andWhere('entity.label LIKE :value')
|
||||
->setParameter("value", "%".$search["value"]."%");
|
||||
}
|
||||
|
||||
if($ordercolumn) {
|
||||
switch($ordercolumn) {
|
||||
case 1 :
|
||||
$qb->orderBy('entity.label',$orderdir);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$datas=$qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
|
||||
|
||||
foreach($datas as $data) {
|
||||
// Action
|
||||
$action = "";
|
||||
$action.="<a href='".$this->generateUrl($this->route.'_update', array('id'=>$data->getId()))."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
|
||||
|
||||
$tmp=array();
|
||||
array_push($tmp,$action);
|
||||
array_push($tmp,$data->getLabel());
|
||||
|
||||
if($this->getParameter("appMasteridentity")=="LDAP"||$this->getParameter("appSynchro")=="LDAP2NINE") array_push($tmp,$data->getLdapfilter());
|
||||
if($this->getParameter("appMasteridentity")=="SSO") array_push($tmp,$data->getAttributes());
|
||||
|
||||
array_push($output["data"],$tmp);
|
||||
}
|
||||
|
||||
// Retour
|
||||
return new JsonResponse($output);
|
||||
}
|
||||
|
||||
public function submit(Request $request,ManagerRegistry $em): Response
|
||||
{
|
||||
// Initialisation de l'enregistrement
|
||||
$data = new Entity();
|
||||
|
||||
// Création du formulaire
|
||||
$form = $this->createForm(Form::class,$data,array("mode"=>"submit"));
|
||||
|
||||
// Récupération des data du formulaire
|
||||
$form->handleRequest($request);
|
||||
|
||||
// Sur validation
|
||||
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
||||
$data = $form->getData();
|
||||
|
||||
// Sauvegarde
|
||||
$em->getManager()->persist($data);
|
||||
$em->getManager()->flush();
|
||||
|
||||
// Retour à la liste
|
||||
return $this->redirectToRoute($this->route);
|
||||
}
|
||||
|
||||
// Affichage du formulaire
|
||||
return $this->render($this->twig.'edit.html.twig', [
|
||||
"useheader"=>true,
|
||||
"usemenu"=>false,
|
||||
"usesidebar"=>true,
|
||||
"mode"=>"submit",
|
||||
"form"=>$form->createView(),
|
||||
$this->data=>$data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update($id,Request $request,ManagerRegistry $em): Response
|
||||
{
|
||||
// Initialisation de l'enregistrement
|
||||
$data=$em->getRepository($this->entity)->find($id);
|
||||
if (!$data) throw $this->createNotFoundException('Unable to find entity.');
|
||||
|
||||
// Création du formulaire
|
||||
$form = $this->createForm(Form::class,$data,array("mode"=>"update"));
|
||||
|
||||
// Récupération des data du formulaire
|
||||
$form->handleRequest($request);
|
||||
|
||||
// Sur validation
|
||||
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
||||
$data = $form->getData();
|
||||
$em->getManager()->flush();
|
||||
|
||||
// Retour à la liste
|
||||
return $this->redirectToRoute($this->route);
|
||||
}
|
||||
|
||||
// Affichage du formulaire
|
||||
return $this->render($this->twig.'edit.html.twig', [
|
||||
'useheader' => true,
|
||||
'usemenu' => false,
|
||||
'usesidebar' => true,
|
||||
$this->data => $data,
|
||||
'mode' => 'update',
|
||||
'form' => $form->createView()
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete($id,Request $request,ManagerRegistry $em): Response
|
||||
{
|
||||
// Récupération de l'enregistrement courant
|
||||
$data=$em->getRepository($this->entity)->find($id);
|
||||
if (!$data) throw $this->createNotFoundException('Unable to find entity.');
|
||||
|
||||
// Tentative de suppression
|
||||
try{
|
||||
$em->getManager()->remove($data);
|
||||
$em->getManager()->flush();
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
$request->getSession()->getFlashBag()->add("error", $e->getMessage());
|
||||
return $this->redirectToRoute($this->route."_update",["id"=>$id]);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute($this->route);
|
||||
}
|
||||
|
||||
public function is(Request $request,ManagerRegistry $em)
|
||||
{
|
||||
$email=$request->request->get('email');
|
||||
$email=explode("@",$email);
|
||||
$domaine=end($email);
|
||||
|
||||
// Rechercher le mail dans la liste blanche
|
||||
$whitelist=$em->getRepository($this->entity)->findOneBy(["label"=>$domaine]);
|
||||
if($whitelist)
|
||||
return new Response("OK", 200);
|
||||
else
|
||||
return new Response("KO", 200);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user