first commit symfony 6

This commit is contained in:
2022-07-21 16:15:47 +02:00
parent d9bfbb6b3c
commit 5c4961748b
282 changed files with 37482 additions and 0 deletions

View File

@ -0,0 +1,102 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use App\Entity\Tallyday as Tallyday;
class CleanCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:Clean')
->setDescription('Nettoyage des données obsolètes')
->setHelp('Nettoyage des données obsolètes')
->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job')
->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
$alias = $this->container->getParameter('appAlias');
$this->writelnred('');
$this->writelnred('== app:Clean');
$this->writelnred('==========================================================================================================');
$now=new \DateTime('now');
$directory=$this->container->get('kernel')->getProjectDir()."/public/uploads/avatar";
$this->writeln($directory);
$files=[];
$fs = new Filesystem();
if($fs->exists($directory)) {
$finder = new Finder();
$finder->in($directory)->files();
foreach (iterator_to_array($finder) as $file) {
$name = $file->getRelativePathname();
if($name!="admin.jpg"&&$name!="noavatar.png"&&$name!="system.jpg") {
$entity=$this->em->getRepository("App\Entity\User")->findBy(["avatar"=>$name]);
if(!$entity) {
$this->writeln($name);
$url=$directory."/".$name;
if($fs->exists($url)) {
$fs->remove($url);
}
}
}
}
}
$fs = new Filesystem();
$users=$this->em->getRepository("App\Entity\User")->findAll();
foreach($users as $user) {
if(!$fs->exists($directory."/".$user->getAvatar())) {
$this->writeln($user->getUsername());
$user->setAvatar("noavatar.png");
$this->em->persist($user);
$this->em->flush();
}
}
$this->writeln('');
return Command::SUCCESS;
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use App\Entity\Tallyday as Tallyday;
class CleanRegistrationCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
private $byexec;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:CleanRegistration')
->setDescription('Nettoyage des inscriptions obsolètes')
->setHelp('Nettoyage des inscriptions obsolètes')
->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job')
->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
$this->writelnred('');
$this->writelnred('== app:CleanRegistration');
$this->writelnred('==========================================================================================================');
$now=new \DateTime('now');
$datas = $this->em
->createQueryBuilder()
->select('table')
->from('App\Entity\Registration','table')
->where('table.keyexpire<:now')
->setParameter("now",$now->format("Y-m-d H:i:s"))
->getQuery()
->getResult();
foreach($datas as $data) {
$this->writeln('Inscription supprimée = '.$data->getkeyexpire()->format("Y-m-d H:i:s")." >> ".$data->getUsername());
$this->em->remove($data);
$this->em->flush();
}
$this->writeln('');
return Command::SUCCESS;
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
if($this->byexec) $this->filesystem->appendToFile($this->rootlog.'exec.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
if($this->byexec) $this->filesystem->appendToFile($this->rootlog.'exec.log', $string."\n");
}
}

136
src/Command/CronCommand.php Normal file
View File

@ -0,0 +1,136 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Command\LockableTrait;
use App\Entity\Cron;
class CronCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
use LockableTrait;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:Cron')
->setDescription('Execution of the cron command')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
if (!$this->lock()) {
$this->output->writeln("CRON LOCK");
return Command::FAILURE;
}
$crons = $this->em->getRepository('App\Entity\Cron')->toexec();
if($crons) {
$now=new \DateTime();
$this->writelnred('');
$this->writelnred('');
$this->writelnred('');
$this->writelnred('');
$this->writelnred('==========================================================================================================');
$this->writelnred('== CRON ==================================================================================================');
$this->writelnred('==========================================================================================================');
$this->writeln ('Date = '.$now->format('Y-m-d H:i:s'));
$this->writeln ('Application = '.$this->container->getParameter("appName"));
}
foreach($crons as $cron) {
// Id du cron
$idcron = $cron->getId();
// Flag d'execution en cours
$now=new \DateTime();
$cron->setStartexecdate($now);
//$cron->setStatut(1);
$this->em->persist($cron);
$this->em->flush();
// Récupération de la commande
$command = $this->getApplication()->find($cron->getCommand());
// Réccuépration des parametres
$jsonparameter=json_decode($cron->getJsonargument(),true);
// Parametre id du cron actuel
$jsonparameter["cronid"]=$cron->getId();
// Formater la chaine de parametre
$parameter = new ArrayInput($jsonparameter);
// Executer la commande
try{
$returnCode = $command->run($parameter, $output);
}
catch(\Exception $e) {
$this->writelnred("JOB EN ERREUR .".$e->getMessage());
$returnCode=Command::FAILURE;
}
// Flag de fin d'execution
$now=new \DateTime();
$cron->setEndexecdate($now);
// Si interval par heure
if(fmod($cron->getRepeatinterval(),3600)==0)
$next=clone $cron->getNextexecdate();
else
$next=new \DateTime();
$next->add(new \DateInterval('PT'.$cron->getRepeatinterval().'S'));
$cron->setNextexecdate($next);
// Statut OK/KO
$cron->setStatut(($returnCode==Command::FAILURE?0:1));
$this->em->persist($cron);
$this->em->flush();
}
if($crons) {
$this->writelnred("==");
$this->writelnred("FIN CRON");
$this->writelnred("==");
$this->writelnred("");
}
return Command::SUCCESS;
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}

571
src/Command/InitCommand.php Normal file
View File

@ -0,0 +1,571 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Ramsey\Uuid\Uuid;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Id\AssignedGenerator;
use App\Entity\Group;
use App\Entity\Niveau01;
use App\Entity\User;
use App\Entity\Config;
use App\Entity\Cron;
class InitCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
private $appname;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:Init')
->setDescription('Init Data for App')
->setHelp('This command Init Data App')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
$this->appname = $this->container->getParameter('appName');
$this->writeln('APP = Default Data');
// On s'assure que le groupe tout le monde existe
$metadata = $this->em->getClassMetaData('App\Entity\Group');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
$group=$this->em->getRepository('App\Entity\Group')->findOneBy(['id'=>'-1']);
if(!$group) {
$group=new Group();
$group->setId(-1);
$group->setLabel("Tout le monde");
$group->setIsopen(false);
$group->setIsworkgroup(false);
$group->setApikey(Uuid::uuid4());
$this->em->persist($group);
$this->em->flush();
}
// On s'assure qu'il exite un niveau01
$metadata = $this->em->getClassMetaData('App\Entity\Niveau01');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
$niveau01=$this->em->getRepository('App\Entity\Niveau01')->findOneBy(['id'=>'-1']);
if(!$niveau01) {
$niveau01=new Niveau01();
$niveau01->setId(-1);
$niveau01->setLabel($this->appname);
$niveau01->setApikey(Uuid::uuid4());
$this->em->persist($niveau01);
$this->em->flush();
}
// On s'assure que le user admin existe
$metadata = $this->em->getClassMetaData('App\Entity\User');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
$user=$this->em->getRepository('App\Entity\User')->findOneBy(['id'=>'-1']);
if(!$user) {
$user=new User();
$user->setId(-1);
$user->setUsername("admin");
$user->setFirstname("admin");
$user->setLastname($this->appname);
$user->setPassword($this->container->getParameter('appSecret'));
$user->setEmail($this->container->getParameter('appMailnoreply'));
$user->setApikey(Uuid::uuid4());
$user->setAvatar("admin.jpg");
$user->setIsVisible(true);
$user->setNiveau01($niveau01);
$this->em->persist($user);
$this->em->flush();
}
// On s'assure que les appAdmins sont bien admin
foreach($this->container->getParameter('appAdmins') as $admin) {
$user=$this->em->getRepository('App\Entity\User')->findOneBy(['username'=>$admin]);
if($user&&!$user->hasRole("ROLE_ADMIN")) {
$user->setRole("ROLE_ADMIN");
$this->em->flush();
}
}
// colorbgbody = Couleur des fonds de page
$this->insertConfig(
1, // order
"site", // category
"appname", // id
"Titre de votre site", // title
"", // value
$this->appname, // default
"string", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Titre de votre site"
);
$this->insertConfig(
2, // order
"site", // category
"appsubname", // id
"Sous-titre de votre site", // title
"", // value
"", // default
"string", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Sous-titre de votre site"
);
$this->insertConfig(
3, // order
"site", // category
"appdescription", // id
"Description de votre site", // title
"", // value
"", // default
"editor", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Description de votre site"
);
$this->insertConfig(
100, // order
"site", // category
"fgforceconnect", // id
"Forcer la connexion", // title
"", // value
"0", // default
"boolean", // type,
true, // visible
true, // changeable
true, // required
"", // grouped
"Forcer la connexion afin de rendre votre site privé"
);
$this->insertConfig(
200, // order
"site", // category
"permgroup", // id
"Rôle créateur de groupe de travail", // title
"", // value
"ROLE_MASTER", // default
"role", // type,
true, // visible
true, // changeable
true, // required
"", // grouped
"Détermine quel rôle aura la permission de créer des groupes de travail"
);
$this->insertConfig(
201, // order
"site", // category
"permannu", // id
"Rôle accédant à l'annuaire", // title
"", // value
"ROLE_USER", // default
"role", // type,
true, // visible
true, // changeable
true, // required
"", // grouped
"Détermine quel rôle aura la permission de voir l'annuaire"
);
$this->insertConfig(
202, // order
"site", // category
"scopeannu", // id
"Scope de l'annuaire", // title
"", // value
"ALL", // default
"scopeannu", // type,
true, // visible
true, // changeable
true, // required
"", // grouped
"Détermine le scope des utilisateurs visibles dans l'annuaire par d'autres utilisateurs"
);
$this->insertConfig(
500, // order
"site", // category
"apptheme", // id
"Thème de votre site", // title
"", // value
"", // default
"string", // type,
false, // visible
true, // changeable
false, // required
"", // grouped
"Thème de votre site"
);
// colorbgbody = Couleur des fonds de page
$this->insertConfig(
1, // order
"colorbgbody", // category
"colorbgbodydark", // id
"Couleur de fond fonçée", // title
"", // value
"#2e3131", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur de fond quand le site a besoin d'avoir une couleur de fond foncée"
);
$this->insertConfig(
2, // order
"colorbgbody", // category
"colorbgbodylight", // id
"Couleur de fond claire", // title
"", // value
"#ffffff", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur de fond quand le site a besoin d'avoir une couleur de fond claire"
);
// colorfttitle = Couleur des fontes titre
$this->insertConfig(
1, // order
"colorfttitle", // category
"colorfttitledark", // id
"Couleur des titres sur fond fonçé", // title
"", // value
"#ffffff", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur des titres sur fond fonçé"
);
$this->insertConfig(
2, // order
"colorfttitle", // category
"colorfttitlelight", // id
"Couleur des titres sur fond claire", // title
"", // value
"#2e3131", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur des titres sur fond claire"
);
// colorftbody = Couleur des fontes titre
$this->insertConfig(
1, // order
"colorftbody", // category
"colorftbodydark", // id
"Couleur de la police sur fond fonçé", // title
"", // value
"#ffffff", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur de la police sur fond fonçé"
);
$this->insertConfig(
2, // order
"colorftbody", // category
"colorftbodylight", // id
"Couleur de la police sur fond claire", // title
"", // value
"#343a40", // default
"color", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur de la police sur fond claire"
);
// font = nom des polices
$this->insertConfig(
1, // order
"font", // category
"fonttitle", // id
"Police pour les titres", // title
"", // value
"Theboldfont", // default
"font", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"La couleur de la police de votre site"
);
$this->insertConfig(
2, // order
"font", // category
"fontbody", // id
"Police principale", // title
"", // value
"Roboto-Regular", // default
"font", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Nom de la police principale"
);
$this->insertConfig(
3, // order
"font", // category
"fontsizeh1", // id
"Taille des titres h1", // title
"", // value
"40", // default
"integer", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Taille des titres h1 en px"
);
$this->insertConfig(
4, // order
"font", // category
"fontsizeh2", // id
"Taille des titres h2", // title
"", // value
"32", // default
"integer", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Taille des titres h2 en px"
);
$this->insertConfig(
5, // order
"font", // category
"fontsizeh3", // id
"Taille des titres h3", // title
"", // value
"28", // default
"integer", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Taille des titres h3 en px"
);
$this->insertConfig(
6, // order
"font", // category
"fontsizeh4", // id
"Taille des titres h4", // title
"", // value
"24", // default
"integer", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Taille des titres h4 en px"
);
// logo =
$this->insertConfig(
1, // order
"logo", // category
"logodark", // id
"Logo sur fond fonçé", // title
"", // value
"logo.png", // default
"logo", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Logo sur fond fonçé"
);
$this->insertConfig(
2, // order
"logo", // category
"logolight", // id
"Logo sur fond clair", // title
"", // value
"logo.png", // default
"logo", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Logo sur fond clair"
);
// header =
$this->insertConfig(
1, // order
"header", // category
"headerimage", // id
"Image de fond de la bannière", // title
"", // value
"header.jpg", // default
"header", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Image appnamede fond de la bannière"
);
$this->insertConfig(
1, // order
"header", // category
"headerheight", // id
"Hauteur de la bannière", // title
"", // value
"100", // default
"integer", // type,
true, // visible
true, // changeable
false, // required
"", // grouped
"Image de fond de la bannière"
);
$output->writeln('');
// Job de purge des fichiers obsolète
// Toute les 24h à 3h00
$entity = $this->em->getRepository('App\Entity\Cron')->findOneBy(["command"=>"app:Clean"]);
if(!$entity) {
$entity = new Cron;
$nextdate=$entity->getSubmitdate();
$nextdate->setTime(3,0);
$entity->setCommand("app:Clean");
$entity->setDescription("Nettoyage des données obsolètes");
$entity->setStatut(1);
$entity->setRepeatinterval(86400);
$entity->setNextexecdate($nextdate);
$this->em->persist($entity);
}
// Job synchronisation des comptes utilisateur
// Toute les 24h à 3h00
$entity = $this->em->getRepository('App\Entity\Cron')->findOneBy(["command"=>"app:Synchro"]);
if(!$entity) {
$entity = new Cron;
$nextdate=$entity->getSubmitdate();
$nextdate->setTime(4,0);
$entity->setCommand("app:Synchro");
$entity->setDescription("Synchronisation des comptes utilisateurs");
$entity->setStatut(1);
$entity->setRepeatinterval(86400);
$entity->setNextexecdate($nextdate);
$this->em->persist($entity);
}
// Job purge des registrations obsolètes
// Toute les 5mn
$entity = $this->em->getRepository('App\Entity\Cron')->findOneBy(["command"=>"app:CleanRegistration"]);
if(!$entity) {
$entity = new Cron;
$entity->setCommand("app:CleanRegistration");
$entity->setDescription("Nettoyage des Inscriptions obsolètes");
$entity->setStatut(1);
$entity->setRepeatinterval(300);
$entity->setNextexecdate($entity->getSubmitdate());
$this->em->persist($entity);
}
$this->em->flush();
$output->writeln('');
return Command::SUCCESS;
}
private function insertConfig($order,$category,$id,$title,$value,$default,$type,$visible,$changeable,$required,$grouped,$help) {
$entity=$this->em->getRepository("App\Entity\Config")->find($id);
if(!$entity) {
$entity= new Config();
$entity->setId($id);
$entity->setValue($value);
}
$entity->setDefault($default);
$entity->setCategory($category);
$entity->setOrder($order);
$entity->setTitle($title);
$entity->setType($type);
$entity->setVisible($visible);
$entity->setChangeable($changeable);
$entity->setRequired($required);
$entity->setGrouped($grouped);
$entity->setHelp($help);
$this->em->persist($entity);
$this->em->flush();
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Cadoles\CoreBundle\Entity\User;
class SetPasswordCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:SetPassword')
->setDescription("Modifier le password d'un utilisateur")
->setHelp("Modifier le password d'un utilisateur")
->addArgument('username', InputArgument::OPTIONAL, 'username')
->addArgument('password', InputArgument::OPTIONAL, 'password')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
$this->writelnred('');
$this->writelnred('== app:SetPasword');
$this->writelnred('==========================================================================================================');
$username = $input->getArgument('username');
$this->writeln($username);
$password = $input->getArgument('password');
$this->writeln($password);
$user = $this->em->getRepository('App\Entity\User')->findOneBy(array('username' => $username));
if($user) {
// Set Password
$user->setPassword($password);
$this->em->persist($user);
$this->em->flush();
}
$this->writeln('');
return Command::SUCCESS;
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
if(!$string) $string=" ";
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}

View File

@ -0,0 +1,912 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Ramsey\Uuid\Uuid;
use App\Service\LdapService;
use App\Service\ApiService;
use App\Entity\Niveau01;
use App\Entity\Niveau02;
use App\Entity\User;
use App\Entity\Group;
use App\Entity\UserGroup;
class SynchroCommand extends Command
{
public function __construct(ContainerInterface $container,EntityManagerInterface $em,LdapService $ldapservice,ApiService $apiservice)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
$this->ldap = $ldapservice;
$this->apiservice = $apiservice;
}
protected function configure()
{
$this
->setName('app:Synchro')
->setDescription('Synchronisation Annuaire')
->setHelp('This command Synchro for Core')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
$this->appMasteridentity = $this->container->getParameter('appMasteridentity');
$appSynchro = $this->container->getParameter('appSynchro');
$this->synchro = $this->container->getParameter("appSynchro");
$this->synchropurgeniveau01 = $this->container->getParameter("appSynchroPurgeNiveau01");
$this->synchropurgeniveau02 = $this->container->getParameter("appSynchroPurgeNiveau02");
$this->synchropurgegroup = $this->container->getParameter("appSynchroPurgeGroup");
$this->synchropurgeuser = $this->container->getParameter("appSynchroPurgeUser");
$this->host = $this->container->getParameter("ldapHost");
$this->port = $this->container->getParameter("ldapPort");
$this->usetls = $this->container->getParameter("ldapUsetls");
$this->userwriter = $this->container->getParameter("ldapUserwriter");
$this->user = $this->container->getParameter("ldapUser");
$this->password = $this->container->getParameter("ldapPassword");
$this->basedn = $this->container->getParameter("ldapBasedn");
$this->baseorganisation = $this->container->getParameter("ldapBaseorganisation");
$this->baseniveau01 = $this->container->getParameter("ldapBaseniveau01");
$this->baseniveau02 = $this->container->getParameter("ldapBaseniveau02");
$this->basegroup = $this->container->getParameter("ldapBasegroup");
$this->baseuser = $this->container->getParameter("ldapBaseuser");
$this->username = $this->container->getParameter("ldapUsername");
$this->firstname = $this->container->getParameter("ldapFirstname");
$this->lastname = $this->container->getParameter("ldapLastname");
$this->email = $this->container->getParameter("ldapEmail");
$this->avatar = $this->container->getParameter("ldapAvatar");
$this->memberof = $this->container->getParameter("ldapMemberof");
$this->groupgid = $this->container->getParameter("ldapGroupgid");
$this->groupname = $this->container->getParameter("ldapGroupname");
$this->groupmember = $this->container->getParameter("ldapGroupmember");
$this->groupmemberisdn = $this->container->getParameter("ldapGroupmemberisdn");
$this->filtergroup = $this->container->getParameter("ldapFiltergroup");
$this->filteruser = $this->container->getParameter("ldapFilteruser");
switch($appSynchro){
case "LDAP2NINE":
$return=$this->ldap2nine();
break;
case "NINE2LDAP":
$return=$this->nine2ldap();
break;
case "NINE2NINE":
$return=$this->nine2nine();
break;
}
$this->writeln('');
return $return;
}
private function ldap2nine()
{
$this->writelnred('');
$this->writelnred('== app:Synchro');
$this->writelnred('==========================================================================================================');
// Synchronisation ldap2nine possible uniquement si appMasteridentity=LDAP or SSO
if($this->appMasteridentity!="LDAP"&&$this->appMasteridentity!="SSO") {
$this->writeln("Synchronisation impossible si appMasteridentity!=LDAP et appMasteridentity!=SSO");
return Command::FAILURE;
}
// Synchronisation impossible si aucune connexion à l'annuaire
if(!$this->ldap->connect()) {
$this->writeln("Synchronisation impossible connexion impossible à l'annuaire");
return Command::FAILURE;
}
$this->writeln('');
$this->writeln('=====================================================');
$this->writeln('== SYNCHONISATION LDAP TO NINE ======================');
$this->writeln('=====================================================');
$tbniveau01members=[];
$tbgroupmembers=[];
$tbniveau01s=[];
$tbgroups=[];
$tbusers=[];
$ldapniveau01s=$this->em->createQueryBuilder()->select('entity')->from('App:Niveau01','entity')->where('entity.ldapfilter IS NOT NULL')->getQuery()->getResult();
$ldapgroups=$this->em->createQueryBuilder()->select('entity')->from('App:Group','entity')->where('entity.ldapfilter IS NOT NULL')->getQuery()->getResult();
$fgsynchroniveau01s=(!empty($this->baseniveau01)&&!empty($this->groupgid)&&!empty($this->groupname)&&!empty($this->filtergroup));
$fgsynchrogroups=(!empty($this->basegroup)&&!empty($this->groupgid)&&!empty($this->groupname)&&!empty($this->filtergroup));
$fgsynchrousers=(!empty($this->baseuser)&&!empty($this->username)&&!empty($this->email)&&!empty($this->filteruser));
$fgsynchropurgeniveau01s=($fgsynchroniveau01s&&$this->synchropurgeniveau01);
$fgsynchropurgegroups=($fgsynchrogroups&&$this->synchropurgegroup);
$fgsynchropurgeusers=($fgsynchrousers&&$this->synchropurgeuser);
// Synchronisation des niveau01s
if($fgsynchroniveau01s) {
$this->writeln('');
$this->writeln('== NIVEAU01 =========================================');
$ldapentrys=$this->ldap->search($this->filtergroup,[$this->groupgid,$this->groupname,$this->groupmember],$this->baseniveau01);
foreach($ldapentrys as $ldapentry) {
$niveau01other=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["label"=>$ldapentry[$this->groupname]]);
if($niveau01other&&$niveau01other->getIdexternal()!=$ldapentry[$this->groupgid]) {
$this->writelnred(" > ".$ldapentry[$this->groupname]." = Impossible à synchroniser un autre niveau01 existe déjà avec ce label");
continue;
}
// On recherche le groupe via le gid
$this->writeln(' > '.$ldapentry[$this->groupname]);
$niveau01=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["idexternal"=>$ldapentry[$this->groupgid]]);
if(!$niveau01) {
$niveau01=new Niveau01();
$niveau01->setApikey(Uuid::uuid4());
$this->em->persist($niveau01);
}
$niveau01->setIdexternal($ldapentry[$this->groupgid]);
$niveau01->setLabel($ldapentry[$this->groupname]);
$niveau01->setLdapfilter("(".$this->groupname."=".$ldapentry[$this->groupname].")");
$this->em->flush();
// Sauvegarde du niveau01ldap
array_push($tbniveau01s,$ldapentry[$this->groupname]);
// Sauvegarde des membres du niveau01
if(!empty($ldapentry[$this->groupmember])) {
if(!is_array($ldapentry[$this->groupmember])) {
$member=$ldapentry[$this->groupmember];
if(!array_key_exists($member,$tbniveau01members)) $tbniveau01members[$member]=[];
array_push($tbniveau01members[$member],$ldapentry[$this->groupname]);
}
else {
foreach($ldapentry[$this->groupmember] as $member) {
if(!array_key_exists($member,$tbniveau01members)) $tbniveau01members[$member]=[];
array_push($tbniveau01members[$member],$ldapentry[$this->groupname]);
}
}
}
}
}
else {
$this->writeln('');
$this->writeln('== NIVEAU01 =========================================');
$this->writelnred(" > Synchronisation impossible il vous manque des paramétres ldap pour le faire");
}
// Synchronisation des groups
if($fgsynchrogroups) {
$this->writeln('');
$this->writeln('== GROUP ============================================');
$ldapentrys=$this->ldap->search($this->filtergroup,[$this->groupgid,$this->groupname,$this->groupmember],$this->basegroup);
foreach($ldapentrys as $ldapentry) {
$groupother=$this->em->getRepository("App\Entity\Group")->findOneBy(["label"=>$ldapentry[$this->groupname]]);
if($groupother&&$groupother->getIdexternal()!=$ldapentry[$this->groupgid]) {
$this->writelnred(" > ".$ldapentry[$this->groupname]." = Impossible à synchroniser un autre groupe existe déjà avec ce label");
continue;
}
// On recherche le groupe via le gid
$this->writeln(' > '.$ldapentry[$this->groupname]);
$group=$this->em->getRepository("App\Entity\Group")->findOneBy(["idexternal"=>$ldapentry[$this->groupgid]]);
if(!$group) {
$group=new Group();
$group->setIsopen(false);
$group->setIsworkgroup(false);
$group->setApikey(Uuid::uuid4());
$this->em->persist($group);
}
$group->setIdexternal($ldapentry[$this->groupgid]);
$group->setLabel($ldapentry[$this->groupname]);
$group->setLdapfilter("(".$this->groupname."=".$ldapentry[$this->groupname].")");
$this->em->flush();
// Sauvegarde du groupldap
array_push($tbgroups,$ldapentry[$this->groupname]);
// Sauvegarde des membres du group
if(!empty($ldapentry[$this->groupmember])) {
if(!is_array($ldapentry[$this->groupmember])) {
$member=$ldapentry[$this->groupmember];
if(!array_key_exists($member,$tbgroupmembers)) $tbgroupmembers[$member]=[];
array_push($tbgroupmembers[$member],$ldapentry[$this->groupname]);
}
else {
foreach($ldapentry[$this->groupmember] as $member) {
if(!array_key_exists($member,$tbgroupmembers)) $tbgroupmembers[$member]=[];
array_push($tbgroupmembers[$member],$ldapentry[$this->groupname]);
}
}
}
}
}
else {
$this->writeln('');
$this->writeln('== GROUP ============================================');
$this->writelnred(" > Synchronisation impossible il vous manque des paramétres ldap pour le faire");
}
// Synchronisation des users
if($fgsynchrousers) {
$this->writeln('');
$this->writeln('== USER =============================================');
$ldapentrys=$this->ldap->search($this->filteruser,[$this->username,$this->firstname,$this->lastname,$this->email,$this->avatar,$this->memberof],$this->baseuser);
foreach($ldapentrys as $ldapentry) {
$userother=$this->em->getRepository("App\Entity\User")->findOneBy(["email"=>$ldapentry[$this->email]]);
if($userother&&$userother->getUSername()!=$ldapentry[$this->username]) {
$this->writelnred(" > ".$ldapentry[$this->groupname]." = Impossible à synchroniser un autre user existe déjà avec ce mail");
continue;
}
$userother=$this->em->getRepository("App\Entity\Registration")->findOneBy(["email"=>$ldapentry[$this->email]]);
if($userother&&$userother->getUSername()!=$ldapentry[$this->username]) {
$this->writelnred(" > ".$ldapentry[$this->username]." = Impossible à synchroniser un autre user existe déjà avec ce mail");
continue;
}
// On recherche le user via le username
$this->writeln(' > '.$ldapentry[$this->username]);
$user=$this->em->getRepository("App\Entity\User")->findOneBy(["username"=>$ldapentry[$this->username]]);
if(!$user) {
$user=new User();
$user->setUsername($ldapentry[$this->username]);
$user->setIsvisible(true);
$user->setApikey(Uuid::uuid4());
$user->setPassword("LDAPPWD-".$ldapentry[$this->username]);
$user->setRole("ROLE_USER");
$user->setAvatar("noavatar.png");
$this->em->persist($user);
}
// Recherche du niveau01
$niveau01=null;
if($user->getNiveau01()&&empty($user->getNiveau01()->getIdexternal()))
$niveau01=$user->getNiveau01();
if(array_key_exists($ldapentry[$this->username],$tbniveau01members))
$niveau01=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["label"=>$tbniveau01members[$ldapentry[$this->username]][0]]);
if(!$niveau01)
$niveau01=$this->em->getRepository('App\Entity\Niveau01')->find(-1);
// Mise à jour des attributs
if(!empty($ldapentry[$this->lastname])) $user->setLastname($ldapentry[$this->lastname]);
if(!empty($ldapentry[$this->firstname])) $user->setFirstname($ldapentry[$this->firstname]);
if(!empty($ldapentry[$this->email])) $user->setEmail($ldapentry[$this->email]);
if(!empty($ldapentry[$this->avatar])) $user->setAvatar($ldapentry[$this->avatar]);
// Mise à jour du niveau01
if($niveau01!=$user->getNiveau01()) $user->setNiveau02(null);
$user->setNiveau01($niveau01);
// Mise à jour du role
if(in_array($ldapentry[$this->username],$this->container->getParameter("appAdmins")))
$user->setRole("ROLE_ADMIN");
// Sauvegarde en bdd
$this->em->flush();
// Sauvegarde du userldap
array_push($tbusers,$ldapentry[$this->username]);
// Inscription au groupe
if(array_key_exists($ldapentry[$this->username],$tbgroupmembers)) {
foreach($tbgroupmembers[$ldapentry[$this->username]] as $grouplabel) {
$group=$this->em->getRepository("App\Entity\Group")->findOneBy(["label"=>$grouplabel]);
if($group) {
$usergroup=$this->em->getRepository("App\Entity\UserGroup")->findOneBy(["user"=>$user,"group"=>$group]);
if(!$usergroup) {
$usergroup=new UserGroup();
$usergroup->setUser($user);
$usergroup->setGroup($group);
$usergroup->setApikey(Uuid::uuid4());
$usergroup->setRolegroup(0);
$this->em->persist($usergroup);
$this->em->flush();
}
}
}
}
// Desinscription des group ldap
foreach($ldapgroups as $group) {
if(!array_key_exists($ldapentry[$this->username],$tbgroupmembers)||!in_array($group->getLabel(),$tbgroupmembers[$ldapentry[$this->username]])) {
$usergroup=$this->em->getRepository("App\Entity\UserGroup")->findOneBy(["user"=>$user,"group"=>$group]);
if($usergroup) {
$this->em->remove($usergroup);
$this->em->flush();
}
}
}
}
}
else {
$this->writeln('');
$this->writeln('== USER =============================================');
$this->writelnred(" > Synchronisation impossible il vous manque des paramétres ldap pour le faire");
}
// Purge des users
if($fgsynchropurgeusers) {
$this->writeln('');
$this->writeln('== PURGE USER =============================================');
$users=$this->em->getRepository("App\Entity\User")->findAll();
foreach($users as $user) {
if(!in_array($user->getUsername(),$tbusers)) {
if($user->getId()>0) {
$this->writeln(' > '.$user->getUSername());
$this->em->remove($user);
$this->em->flush();
}
}
}
}
// Purge des groups
if($fgsynchropurgegroups) {
$this->writeln('');
$this->writeln('== PURGE GROUP =============================================');
foreach($ldapgroups as $group) {
if(!in_array($group->getLabel(),$tbgroups)) {
if($group->getId()>0) {
$this->writeln(' > '.$group->getLabel());
$this->em->remove($group);
}
else {
$group->setLdapfilter(null);
$group->setIdexternal(null);
}
$this->em->flush();
}
}
}
// Purge des niveau01s
if($fgsynchropurgeniveau01s) {
$this->writeln('');
$this->writeln('== PURGE NIVEAU01 =============================================');
foreach($ldapniveau01s as $niveau01) {
if(!in_array($niveau01->getLabel(),$tbniveau01s)) {
if($niveau01->getId()>0) {
$user=$this->em->getRepository("App\Entity\User")->findOneBy(["niveau01"=>$niveau01]);
if($user) {
$resetniveau01=$this->em->getRepository("App\Entity\User")->find(-1);
$user->setNiveau01($resetniveau01);
$user->setNiveau02(null);
}
$this->writeln(' > '.$niveau01->getLabel());
$this->em->remove($niveau01);
}
else {
$niveau01->setLdapfilter(null);
$niveau01->setIdexternal(null);
}
$this->em->flush();
}
}
}
return Command::SUCCESS;
}
private function nine2ldap()
{
$this->writelnred('');
$this->writelnred('== app:Synchro');
$this->writelnred('==========================================================================================================');
// Synchronisation impossible si aucune connexion à l'annuaire
if(!$this->ldap->isNine2Ldap()) {
$this->writeln("Synchronisation impossible soit :");
$this->writeln("- connexion impossible à l'annuaire");
$this->writeln("- appMasteridentity!=SQL");
$this->writeln("- votre user ldap n'a pas de permission en écriture");
$this->writeln("- vous n'avez pas renseigné les bases de votre organisation");
return Command::FAILURE;
}
$this->writeln('');
$this->writeln('=====================================================');
$this->writeln('== SYNCHONISATION NINE TO LDAP ======================');
$this->writeln('=====================================================');
$this->writeln('');
$this->writeln('== ORGANISATION =====================================');
$this->writeln($this->baseorganisation);
$this->writeln($this->baseniveau01);
$this->writeln($this->baseniveau02);
$this->writeln($this->basegroup);
$this->writeln($this->baseuser);
$this->ldap->addOrganisations();
$this->writeln('');
$this->writeln('== USER =============================================');
$users=$this->em->getRepository("App\Entity\User")->findAll();
$attributes=$this->ldap->listAttributesUser();
foreach($users as $user) {
$filter=str_replace("*",$user->getUsername(),$this->filteruser);
$ldapentrys=$this->ldap->search($filter,$attributes,$this->baseuser);
if(empty($ldapentrys)) {
$this->writeln($user->getUsername()." = SUBMIT");
$this->ldap->addUser($user);
}
elseif($this->ldap->ismodifyUser($user,$ldapentrys[0])) {
$this->writeln($user->getUsername()." = UPDATE");
$this->ldap->modifyUser($user);
}
}
$ldapentrys=$this->ldap->search($this->filteruser,$attributes,$this->baseuser);
foreach($ldapentrys as $ldapentry) {
$user=$this->em->getRepository("App\Entity\User")->findOneBy(["username"=>$ldapentry["uid"]]);
if(!$user) {
$this->writeln($ldapentry["uid"]." = DELETE");
$dn=$this->ldap->getUserDN($ldapentry["uid"]);
$this->ldap->deleteByDN($dn);
}
}
$this->writeln('');
$this->writeln('== GROUP ============================================');
$groups=$this->em->getRepository("App\Entity\Group")->findAll();
$attributes=$this->ldap->listAttributesGroup();
foreach($groups as $group) {
if($group->getLdapfilter()) {
$group->setLdapfilter(null);
$this->em->flush();
}
$filter="gidnumber=".$group->getId();
$ldapentrys=$this->ldap->search($filter,$attributes,$this->basegroup);
if(empty($ldapentrys)) {
$this->writeln($group->getLabel()." = SUBMIT");
$this->ldap->addGroup($group);
}
elseif($this->ldap->ismodifyGroup($group,$ldapentrys[0])) {
$this->writeln($group->getLabel()." = UPDATE");
$this->ldap->modifyGroup($group,$ldapentrys[0]["cn"]);
}
}
$ldapentrys=$this->ldap->search($this->filtergroup,$attributes,$this->basegroup);
foreach($ldapentrys as $ldapentry) {
$group=$this->em->getRepository("App\Entity\Group")->find($ldapentry["gidnumber"]);
if(!$group) {
$this->writeln($ldapentry["cn"]." = DELETE");
$dn=$this->ldap->getGroupDN($ldapentry["cn"]);
$this->ldap->deleteByDN($dn);
}
}
$this->writeln('');
$this->writeln('== NIVEAU02 =========================================');
$niveau02s=$this->em->getRepository("App\Entity\Niveau02")->findAll();
$attributes=$this->ldap->listAttributesNiveau02();
foreach($niveau02s as $niveau02) {
$filter="gidnumber=".$niveau02->getId();
$ldapentrys=$this->ldap->search($filter,$attributes,$this->baseniveau02);
if(empty($ldapentrys)) {
$this->writeln($niveau02->getLabel()." = SUBMIT");
$this->ldap->addNiveau02($niveau02);
}
elseif($this->ldap->ismodifyNiveau02($niveau02,$ldapentrys[0])) {
$this->writeln($niveau02->getLabel()." = UPDATE");
$this->ldap->modifyNiveau02($niveau02,$ldapentrys[0]["cn"]);
}
}
$ldapentrys=$this->ldap->search($this->filtergroup,$attributes,$this->baseniveau02);
foreach($ldapentrys as $ldapentry) {
$niveau02=$this->em->getRepository("App\Entity\Niveau02")->find($ldapentry["gidnumber"]);
if(!$niveau02) {
$this->writeln($ldapentry["cn"]." = DELETE");
$dn=$this->ldap->getNiveau02DN($ldapentry["cn"]);
$this->ldap->deleteByDN($dn);
}
}
$this->writeln('');
$this->writeln('== NIVEAU01 =========================================');
$niveau01s=$this->em->getRepository("App\Entity\Niveau01")->findAll();
$attributes=$this->ldap->listAttributesNiveau01();
foreach($niveau01s as $niveau01) {
if($niveau01->getLdapfilter()) {
$niveau01->setLdapfilter(null);
$this->em->flush();
}
$filter="gidnumber=".$niveau01->getId();
$ldapentrys=$this->ldap->search($filter,$attributes,$this->baseniveau01);
if(empty($ldapentrys)) {
$this->writeln($niveau01->getLabel()." = SUBMIT");
$this->ldap->addNiveau01($niveau01);
}
elseif($this->ldap->ismodifyNiveau01($niveau01,$ldapentrys[0])) {
$this->writeln($niveau01->getLabel()." = UPDATE");
$this->ldap->modifyNiveau01($niveau01,$ldapentrys[0]["cn"]);
}
}
$ldapentrys=$this->ldap->search($this->filtergroup,$attributes,$this->baseniveau01);
foreach($ldapentrys as $ldapentry) {
$niveau01=$this->em->getRepository("App\Entity\Niveau01")->find($ldapentry["gidnumber"]);
if(!$niveau01) {
$this->writeln($ldapentry["cn"]." = DELETE");
$dn=$this->ldap->getNiveau01DN($ldapentry["cn"]);
$this->ldap->deleteByDN($dn);
}
}
return Command::SUCCESS;
}
private function nine2nine()
{
$this->writelnred('');
$this->writelnred('== app:Synchro');
$this->writelnred('==========================================================================================================');
// Synchronisation ldap2nine possible uniquement si appMasteridentity=NINE
if($this->appMasteridentity!="NINE") {
$this->writeln("Synchronisation impossible si appMasteridentity!=NINE");
return Command::FAILURE;
}
$nineurl = $this->container->getParameter("nineUrl");
$ninesecret = $this->container->getParameter("nineSecret");
if(!$nineurl||!$ninesecret) {
$this->writeln("Synchronisation impossible soit parametres NINE_URL et/ou NINE_SECRET manquant");
return Command::FAILURE;
}
$nineurl.="/rest/";
$this->writeln('');
$this->writeln('=====================================================');
$this->writeln('== SYNCHONISATION NINE TO NINE ======================');
$this->writeln('=====================================================');
$nineniveau01s=$this->em->createQueryBuilder()->select('entity')->from('App:Niveau01','entity')->where('entity.idexternal IS NOT NULL')->getQuery()->getResult();
$ninegroups=$this->em->createQueryBuilder()->select('entity')->from('App:Group','entity')->where('entity.idexternal IS NOT NULL')->getQuery()->getResult();
$tbniveau01members=[];
$tbgroupmembers=[];
$tbniveau01s=[];
$tbgroups=[];
$tbusers=[];
$fgsynchropurgeniveau01s=($this->synchropurgeniveau01);
$fgsynchropurgegroups=($this->synchropurgegroup);
$fgsynchropurgeusers=($this->synchropurgeuser);
$this->writeln('');
$this->writeln('== NIVEAU01 =========================================');
$response = $this->apiservice->run("GET",$nineurl."getAllNiveau01s",null,["key"=>$ninesecret]);
if($response->code!="200") return Command::FAILURE;
foreach($response->body as $nineniveau01 ) {
$niveau01other=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["label"=>$nineniveau01->niveau01label]);
if($niveau01other&&$niveau01other->getIdexternal()!=$nineniveau01->niveau01id) {
$this->writelnred(" > ".$nineniveau01->niveau01label." = Impossible à synchroniser un autre niveau01 existe déjà avec ce label");
continue;
}
// On recherche le groupe via le gid
$this->writeln(' > '.$nineniveau01->niveau01label);
$niveau01=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["idexternal"=>$nineniveau01->niveau01id]);
if(!$niveau01) {
$niveau01=new Niveau01();
$niveau01->setApikey(Uuid::uuid4());
$this->em->persist($niveau01);
}
$niveau01->setIdexternal($nineniveau01->niveau01id);
$niveau01->setLabel($nineniveau01->niveau01label);
$this->em->flush();
// Sauvegarde du niveau01nine
array_push($tbniveau01s,$nineniveau01->niveau01label);
// Sauvegarde des membres du niveau01
if(!empty($nineniveau01->niveau01users)) {
foreach($nineniveau01->niveau01users as $member) {
if(!array_key_exists($member->userlogin,$tbniveau01members)) $tbniveau01members[$member->userlogin]=[];
array_push($tbniveau01members[$member->userlogin],$nineniveau01->niveau01label);
}
}
}
$this->writeln('');
$this->writeln('== GROUP ============================================');
$response = $this->apiservice->run("GET",$nineurl."getAllGroups",null,["key"=>$ninesecret]);
if($response->code!="200") return Command::FAILURE;
foreach($response->body as $ninegroup ) {
$groupother=$this->em->getRepository("App\Entity\Group")->findOneBy(["label"=>$ninegroup->grouplabel]);
if($groupother&&$groupother->getIdexternal()!=$ninegroup->groupid) {
$this->writelnred(" > ".$ninegroup->grouplabel." = Impossible à synchroniser un autre group existe déjà avec ce label");
continue;
}
// On recherche le groupe via le gid
$this->writeln(' > '.$ninegroup->grouplabel);
$group=$this->em->getRepository("App\Entity\Group")->findOneBy(["idexternal"=>$ninegroup->groupid]);
if(!$group) {
$group=new Group();
$group->setIsopen(false);
$group->setIsworkgroup(false);
$group->setApikey(Uuid::uuid4());
$this->em->persist($group);
}
$group->setIdexternal($ninegroup->groupid);
$group->setLabel($ninegroup->grouplabel);
$this->em->flush();
// Sauvegarde du groupnine
array_push($tbgroups,$ninegroup->grouplabel);
// Sauvegarde des membres du group
if(!empty($ninegroup->groupusers)) {
foreach($ninegroup->groupusers as $member) {
if(!array_key_exists($member->userlogin,$tbgroupmembers)) $tbgroupmembers[$member->userlogin]=[];
array_push($tbgroupmembers[$member->userlogin],$ninegroup->grouplabel);
}
}
}
$this->writeln('');
$this->writeln('== USER =============================================');
$response = $this->apiservice->run("GET",$nineurl."getAllUsers",null,["key"=>$ninesecret]);
if($response->code!="200") return Command::FAILURE;
$nineusers=$response->body;
foreach($nineusers as $nineuser) {
$userother=$this->em->getRepository("App\Entity\User")->findOneBy(["email"=>$nineuser->useremail]);
if($userother&&$userother->getUsername()!=$nineuser->userlogin) {
$this->writelnred(" > ".$nineuser->userlogin." = Impossible à synchroniser un autre user existe déjà avec ce mail");
continue;
}
$userother=$this->em->getRepository("App\Entity\Registration")->findOneBy(["email"=>$nineuser->useremail]);
if($userother&&$userother->getUSername()!=$nineuser->userlogin) {
$this->writelnred(" > ".$nineuser->userlogin." = Impossible à synchroniser un autre user existe déjà avec ce mail");
continue;
}
// On recherche le user via le username
$this->writeln(' > '.$nineuser->userlogin);
$user=$this->em->getRepository("App\Entity\User")->findOneBy(["username"=>$nineuser->userlogin]);
if(!$user) {
$user=new User();
$user->setUsername($nineuser->userlogin);
$user->setIsvisible(true);
$user->setApikey(Uuid::uuid4());
$user->setPassword("NINEPWD-".$nineuser->userlogin);
$user->setRole("ROLE_USER");
$user->setAvatar($nineuser->useravatar);
$this->em->persist($user);
}
// Recherche du niveau01
$niveau01=null;
if($user->getNiveau01()&&empty($user->getNiveau01()->getIdexternal()))
$niveau01=$user->getNiveau01();
if(array_key_exists($nineuser->userlogin,$tbniveau01members))
$niveau01=$this->em->getRepository("App\Entity\Niveau01")->findOneBy(["label"=>$tbniveau01members[$nineuser->userlogin][0]]);
if(!$niveau01)
$niveau01=$this->em->getRepository('App\Entity\Niveau01')->find(-1);
// Mise à jour des attributs
if(!empty($nineuser->userlastname)) $user->setLastname($nineuser->userlastname);
if(!empty($nineuser->userfirstname)) $user->setFirstname($nineuser->userfirstname);
if(!empty($nineuser->useremail)) $user->setEmail($nineuser->useremail);
if(!empty($nineuser->useravatar)) $user->setAvatar($nineuser->useravatar);
// Mise à jour du niveau01
if($niveau01!=$user->getNiveau01()) $user->setNiveau02(null);
$user->setNiveau01($niveau01);
// Mise à jour du role
if(in_array($nineuser->userlogin,$this->container->getParameter("appAdmins")))
$user->setRole("ROLE_ADMIN");
// Sauvegarde en bdd
$this->em->flush();
// Sauvegarde du userldap
array_push($tbusers,$nineuser->userlogin);
// Inscription au groupe
if(array_key_exists($nineuser->userlogin,$tbgroupmembers)) {
foreach($tbgroupmembers[$nineuser->userlogin] as $grouplabel) {
$group=$this->em->getRepository("App\Entity\Group")->findOneBy(["label"=>$grouplabel]);
if($group) {
$usergroup=$this->em->getRepository("App\Entity\UserGroup")->findOneBy(["user"=>$user,"group"=>$group]);
if(!$usergroup) {
$usergroup=new UserGroup();
$usergroup->setUser($user);
$usergroup->setGroup($group);
$usergroup->setApikey(Uuid::uuid4());
$usergroup->setRolegroup(0);
$this->em->persist($usergroup);
$this->em->flush();
}
}
}
}
// Desinscription des group ldap
foreach($ninegroups as $group) {
if(!array_key_exists($nineuser->userlogin,$tbgroupmembers)||!in_array($group->getLabel(),$tbgroupmembers[$nineuser->userlogin])) {
$usergroup=$this->em->getRepository("App\Entity\UserGroup")->findOneBy(["user"=>$user,"group"=>$group]);
if($usergroup) {
$this->em->remove($usergroup);
$this->em->flush();
}
}
}
}
// Purge des users
if($fgsynchropurgeusers) {
$this->writeln('');
$this->writeln('== PURGE USER =============================================');
$users=$this->em->getRepository("App\Entity\User")->findAll();
foreach($users as $user) {
if(!in_array($user->getUsername(),$tbusers)) {
if($user->getId()>0) {
$this->writeln(' > '.$user->getUsername());
$this->em->remove($user);
$this->em->flush();
}
}
}
}
// Purge des groups
if($fgsynchropurgegroups) {
$this->writeln('');
$this->writeln('== PURGE GROUP =============================================');
foreach($ninegroups as $group) {
if(!in_array($group->getLabel(),$tbgroups)) {
if($group->getId()>0) {
$this->writeln(' > '.$group->getLabel());
$this->em->remove($group);
}
else {
$group->setLdapfilter(null);
$group->setIdexternal(null);
}
$this->em->flush();
}
}
}
// Purge des niveau01s
if($fgsynchropurgeniveau01s) {
$this->writeln('');
$this->writeln('== PURGE NIVEAU01 =============================================');
foreach($nineniveau01s as $niveau01) {
if(!in_array($niveau01->getLabel(),$tbniveau01s)) {
if($niveau01->getId()>0) {
$user=$this->em->getRepository("App\Entity\User")->findOneBy(["niveau01"=>$niveau01]);
if($user) {
$resetniveau01=$this->em->getRepository("App\Entity\User")->find(-1);
$user->setNiveau01($resetniveau01);
$user->setNiveau02(null);
}
$this->writeln(' > '.$niveau01->getLabel());
$this->em->remove($niveau01);
}
else {
$niveau01->setLdapfilter(null);
$niveau01->setIdexternal(null);
}
$this->em->flush();
}
}
}
return Command::SUCCESS;
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
protected function addUser($niveau01,$username,$firstname,$lastname,$email,$usersadmin) {
$user = new User();
$user->setUsername($username);
$user->setLastname($lastname);
$user->setFirstname($firstname);
$user->setEmail($email);
$user->setNiveau01($niveau01);
$user->setSiren($niveau01->getSiren());
$user->setPassword("PASSWORDFROMEXTERNE");
$user->setVisible(true);
$user->setAuthlevel("simple");
$user->setBelongingpopulation("agent");
if(in_array($username,$usersadmin))
$user->setRole("ROLE_ADMIN");
else {
$user->setRole("ROLE_USER");
// Si modèle scribe
$ldap_template = $this->container->getParameter('ldap_template');
if($ldap_template=="scribe") {
$ldapfilter="(|(&(uid=".$user->getUsername().")(ENTPersonProfils=enseignant))(&(uid=".$user->getUsername().")(typeadmin=0))(&(uid=".$user->getUsername().")(typeadmin=2)))";
$results = $this->ldap->search($ldapfilter, ['uid'], $this->ldap_basedn);
if($results) $user->setRole("ROLE_ANIM");
}
}
$this->em->persist($user);
$this->em->flush();
}
protected function modUser($user,$username,$firstname,$lastname,$email,$usersadmin) {
$user->setLastname($lastname);
$user->setFirstname($firstname);
$user->setEmail($email);
if(in_array($username,$usersadmin))
$user->setRole("ROLE_ADMIN");
$this->em->persist($user);
$this->em->flush();
}
}