504 lines
22 KiB
PHP
504 lines
22 KiB
PHP
<?php
|
|
namespace Cadoles\CoreBundle\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\Finder\Finder;
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
use Doctrine\DBAL\Connection as DBALConnection;
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Cadoles\CoreBundle\Entity\User;
|
|
use Cadoles\CoreBundle\Entity\Niveau01;
|
|
use Cadoles\CoreBundle\Entity\Niveau02;
|
|
|
|
global $bdd01;
|
|
global $config;
|
|
|
|
class ImportCommand extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this
|
|
// the name of the command (the part after "bin/console")
|
|
->setName('Core:Import')
|
|
|
|
// the short description shown while running "php bin/console list"
|
|
->setDescription("Importation Utilisateur à partir d'un fichier CSV Annuaire")
|
|
|
|
// the full command description shown when running the command with
|
|
// the "--help" option
|
|
->setHelp('This command Import for Core')
|
|
|
|
->addArgument('simulate', InputArgument::OPTIONAL, 'true to simulate / false to run')
|
|
;
|
|
}
|
|
|
|
protected function convert($filename, $delimiter = ',', $enclosure = '"')
|
|
{
|
|
if(!file_exists($filename) || !is_readable($filename)) {
|
|
return FALSE;
|
|
}
|
|
|
|
$header = NULL;
|
|
$data = array();
|
|
|
|
if (($handle = fopen($filename, 'r')) !== FALSE) {
|
|
while (($row = fgetcsv($handle, 1000, $delimiter, $enclosure)) !== FALSE) {
|
|
if(!$header) {
|
|
$header = $row;
|
|
} else {
|
|
$data[] = array_combine($header, $row);
|
|
}
|
|
}
|
|
fclose($handle);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$kernel = new \AppKernel('dev', true);
|
|
$kernel->boot();
|
|
$kernel->loadClassCache();
|
|
$em = $kernel->getContainer()->get('service_container')->get('doctrine.orm.default_entity_manager');
|
|
|
|
$simulate = $input->getArgument('simulate');
|
|
if($simulate=="") $simulate="true";
|
|
|
|
if($simulate!="true"&&$simulate!="false") {
|
|
$output->writeln('Paramétre incorrect');
|
|
return;
|
|
}
|
|
$simulate=($simulate=="true");
|
|
|
|
$output->writeln('');
|
|
if($simulate) $output->writeln('** SIMULATION');
|
|
else $output->writeln('** REEL');
|
|
|
|
$output->writeln('');
|
|
$output->writeln('=====================================================');
|
|
$output->writeln('== IMPORT USERS =====================================');
|
|
$output->writeln('=====================================================');
|
|
|
|
// Read CSV
|
|
$fileName = 'src/Cadoles/CoreBundle/Command/import.csv';
|
|
|
|
// Convert
|
|
$datas = $this->convert($fileName);
|
|
|
|
foreach($datas as $row) {
|
|
if($row["uid"]!="") {
|
|
$data = $em->getRepository('CadolesCoreBundle:User')->findBy(array('username' => $row["uid"]));
|
|
if($data) {
|
|
$output->writeln(' - Existe déjà >> '.$row["uid"]);
|
|
}
|
|
else {
|
|
$data = $em->getRepository('CadolesCoreBundle:User')->findBy(array('email' => $row["mail"]));
|
|
if($data) {
|
|
$output->writeln(' - Existe déjà >> '.$row["uid"]);
|
|
}
|
|
else {
|
|
$output->writeln(' - Création Utilisateur >> '.$row["uid"]);
|
|
if(!$simulate) {
|
|
$niveau01=$em->getRepository('CadolesCoreBundle:Niveau01')->findOneBy(array("id"=>-100));
|
|
$user = new User();
|
|
$user->setUsername($row["uid"]);
|
|
$user->setPasswordDirect($row["userpassword"]);
|
|
$user->setSalt("IMPORT-".$row["uid"]);
|
|
$user->setNiveau01($niveau01);
|
|
$user->setSiren($user->getNiveau01()->getSiren());
|
|
$user->setSiret($user->getNiveau02()!==null?$user->getNiveau02()->getSiret():"");
|
|
$user->setAvatar("noavatar.png");
|
|
$user->setVisible(true);
|
|
$user->setAuthlevel("simple");
|
|
$user->setRole("ROLE_USER");
|
|
$user->setFirstname($row["givenname"]);
|
|
$user->setLastname($row["sn"]);
|
|
$user->setEmail($row["mail"]);
|
|
|
|
$em->persist($user);
|
|
$em->flush();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
$ldap = $kernel->getContainer()->get('cadoles.core.service.ldap');
|
|
if($ldap->isEnabled()) {
|
|
$output->writeln('');
|
|
$output->writeln('=====================================================');
|
|
$output->writeln('== SYNCHONISATION BUNDLE TO LDAP ====================');
|
|
$output->writeln('=====================================================');
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU01 =========================================');
|
|
$baseNiveau01 = $kernel->getContainer()->getParameter('ldap_baseniveau01');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Niveau01','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$criteria = '(cn='.$data->getLabel().')';
|
|
$subbranch=$baseNiveau01;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
|
|
// Mise à jour si elle existe
|
|
if(count($results) > 0) {
|
|
$output->writeln(' - Modification dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->modifyNiveau01($data,$data->getLabel());
|
|
}
|
|
// Sinon création de la fiche
|
|
else {
|
|
$output->writeln(' - Création dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->addNiveau01($data);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU02 =========================================');
|
|
$baseNiveau02 = $kernel->getContainer()->getParameter('ldap_baseniveau02');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Niveau02','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$criteria = '(cn='.$data->getLabel().')';
|
|
$subbranch=$baseNiveau02;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
|
|
// Mise à jour si elle existe
|
|
if(count($results) > 0) {
|
|
$output->writeln(' - Modification dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->modifyNiveau02($data,$data->getLabel());
|
|
}
|
|
// Sinon création de la fiche
|
|
else {
|
|
$output->writeln(' - Création dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->addNiveau02($data);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== GROUP ============================================');
|
|
$baseGroup = $kernel->getContainer()->getParameter('ldap_basegroup');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Group','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$criteria = '(cn='.$data->getLabel().')';
|
|
$subbranch=$baseGroup;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
|
|
// Mise à jour si elle existe
|
|
if(count($results) > 0) {
|
|
$output->writeln(' - Modification dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->modifyGroup($data,$data->getLabel());
|
|
}
|
|
// Sinon création de la fiche
|
|
else {
|
|
$output->writeln(' - Création dans annuaire >> '.$data->getLabel());
|
|
if(!$simulate) $ldap->addGroup($data);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== USER =============================================');
|
|
$baseUser = $kernel->getContainer()->getParameter('ldap_baseuser');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:User','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$criteria = '(uid='.$data->getUsername().')';
|
|
$subbranch=$baseUser;
|
|
$results = $ldap->search($criteria, array('uid'), $subbranch);
|
|
|
|
// S'assurer que SIREN correspond au Niveau01
|
|
if($data->getNiveau01()->getSiren()!=$data->getSiren()) {
|
|
$data->SetSiren($data->getNiveau01()->getSiren());
|
|
$data->flush();
|
|
}
|
|
|
|
// S'assurer que SIRET correspond au Niveau02
|
|
if($data->getNiveau02()!==null&&$data->getNiveau01()->getSiren()!=$data->getSiren()) {
|
|
$data->SetSiret($data->getNiveau02()->getSiret());
|
|
$data->flush();
|
|
}
|
|
|
|
// Mise à jour si elle existe
|
|
if(count($results) > 0) {
|
|
$output->writeln(' - Modification dans annuaire >> '.$data->getUsername());
|
|
if(!$simulate) $ldap->modifyUser($data);
|
|
}
|
|
// Sinon création de la fiche
|
|
else {
|
|
$output->writeln(' - Création dans annuaire >> '.$data->getUsername());
|
|
if(!$simulate) $ldap->addUser($data);
|
|
}
|
|
|
|
// Rattachement à Niveau01 et Niveau02
|
|
if(!$simulate) $ldap->addGroupUser($data);
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== USER GROUP =======================================');
|
|
$baseGroup = $kernel->getContainer()->getParameter('ldap_basegroup');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Group','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$criteria = '(cn='.$data->getLabel().')';
|
|
$subbranch=$baseGroup;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
|
|
// Mise à jour des membres du groupes
|
|
if(count($results) > 0) {
|
|
$output->writeln(' - '.$data->getLabel());
|
|
$dn=$ldap->getGroupDN($data->getLabel());
|
|
$attrs["memberuid"]=array();
|
|
$attrs["cadolesMember"]=array();
|
|
|
|
foreach($data->getUsers() as $usergroupe) {
|
|
array_push($attrs["memberuid"],$usergroupe->getUser()->getUsername());
|
|
array_push($attrs["cadolesMember"],$ldap->getUserDN($usergroupe->getUser()));
|
|
$output->writeln(' > '.$usergroupe->getUser()->getUsername());
|
|
}
|
|
if(!$simulate) $ldap->ldapModify($dn, $attrs);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('=====================================================');
|
|
$output->writeln('== SYNCHONISATION LDAP TO BUNDLE ====================');
|
|
$output->writeln('=====================================================');
|
|
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU01 =========================================');
|
|
$baseNiveau01 = $kernel->getContainer()->getParameter('ldap_baseniveau01');
|
|
|
|
$criteria = '(cn=*)';
|
|
$subbranch=$baseNiveau01;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
foreach($results as $result) {
|
|
$data = $em->getRepository('CadolesCoreBundle:Niveau01')->findBy(array('label' => $result["cn"]));
|
|
if($data) $output->writeln(' - Existe dans bundle >> '.$result["cn"]);
|
|
else {
|
|
$output->writeln(' - A supprimer dans annuaire >> '.$result["cn"]);
|
|
$dn=$ldap->getNiveau01DN($result["cn"]);
|
|
if(!$simulate) $ldap->deleteByDN($dn);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU02 =========================================');
|
|
$baseNiveau02 = $kernel->getContainer()->getParameter('ldap_baseniveau02');
|
|
|
|
$criteria = '(cn=*)';
|
|
$subbranch=$baseNiveau02;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
foreach($results as $result) {
|
|
$data = $em->getRepository('CadolesCoreBundle:Niveau02')->findBy(array('label' => $result["cn"]));
|
|
if($data) $output->writeln(' - Existe dans bundle >> '.$result["cn"]);
|
|
else {
|
|
$output->writeln(' - A supprimer dans annuaire >> '.$result["cn"]);
|
|
$dn=$ldap->getNiveau02DN($result["cn"]);
|
|
if(!$simulate) $ldap->deleteByDN($dn);
|
|
}
|
|
}
|
|
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== GROUP ============================================');
|
|
$baseGroup = $kernel->getContainer()->getParameter('ldap_basegroup');
|
|
|
|
$criteria = '(cn=*)';
|
|
$subbranch=$baseGroup;
|
|
$results = $ldap->search($criteria, array('cn'), $subbranch);
|
|
foreach($results as $result) {
|
|
$data = $em->getRepository('CadolesCoreBundle:Group')->findBy(array('label' => $result["cn"]));
|
|
if($data) $output->writeln(' - Existe dans bundle >> '.$result["cn"]);
|
|
else {
|
|
$output->writeln(' - A supprimer dans annuaire >> '.$result["cn"]);
|
|
$dn=$ldap->getGroupDN($result["cn"]);
|
|
if(!$simulate) $ldap->deleteByDN($dn);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== USER =============================================');
|
|
$baseUser = $kernel->getContainer()->getParameter('ldap_baseuser');
|
|
|
|
$criteria = '(uid=*)';
|
|
$subbranch=$baseUser;
|
|
$results = $ldap->search($criteria, array('uid'), $subbranch);
|
|
foreach($results as $result) {
|
|
$data = $em->getRepository('CadolesCoreBundle:User')->findBy(array('username' => $result["uid"]));
|
|
if($data) $output->writeln(' - Existe dans bundle >> '.$result["uid"]);
|
|
else {
|
|
$output->writeln(' - A supprimer dans annuaire >> '.$result["uid"]);
|
|
$dn='uid='.$result["uid"].','.$baseUser;
|
|
if(!$simulate) $ldap->deleteByDN($dn);
|
|
}
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('=====================================================');
|
|
$output->writeln('== SYNCHONISATION BUNDLE TO EPORTAIL ================');
|
|
$output->writeln('=====================================================');
|
|
|
|
|
|
|
|
$eportail = $kernel->getContainer()->get('cadoles.core.service.eportail');
|
|
if(!$eportail->isEnabled()) {
|
|
$output->writeln('');
|
|
$output->writeln(' Synchronisation ePortail désactivée');
|
|
}
|
|
else {
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU01 =========================================');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Niveau01','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$output->writeln(' - Synchronisation eportail >> '.$data->getLabel());
|
|
if(!$simulate) $eportail->syncNiveau01($data,$data->getLabel());
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== NIVEAU02 =========================================');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Niveau02','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$output->writeln(' - Synchronisation eportail >> '.$data->getLabel());
|
|
if(!$simulate) $eportail->syncNiveau02($data,$data->getLabel());
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== GROUP ============================================');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:Group','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$output->writeln(' - Synchronisation eportail >> '.$data->getLabel());
|
|
if(!$simulate) $eportail->syncGroup($data,$data->getLabel());
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== USER =============================================');
|
|
|
|
$datas = $em ->createQueryBuilder()
|
|
->select('table')
|
|
->from('CadolesCoreBundle:User','table')
|
|
->getQuery()
|
|
->getResult();
|
|
foreach($datas as $data) {
|
|
$output->writeln(' - Synchronisation eportail >> '.$data->getUsername());
|
|
if(!$simulate) $eportail->syncUser($data);
|
|
}
|
|
|
|
|
|
$output->writeln('');
|
|
$output->writeln('=====================================================');
|
|
$output->writeln('== SYNCHONISATION EPORTAIL TO BUNDLE ================');
|
|
$output->writeln('=====================================================');
|
|
$dbeportail= $kernel->getContainer()->get('service_container')->get('doctrine.orm.eportail_entity_manager')->getConnection();
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== GROUP ============================================');
|
|
|
|
$sql = "SELECT * FROM env_group WHERE group_id>0";
|
|
$query = $dbeportail->prepare($sql);
|
|
$query->execute();
|
|
while($row=$query->fetch()){
|
|
$fgOK=false;
|
|
if (strpos($row["group_name"], $labelniveau01.' = ') === 0) {
|
|
$tmp=str_replace("$labelniveau01 = ","",$row["group_name"]);
|
|
$data = $em->getRepository('CadolesCoreBundle:Niveau01')->findBy(array('label' =>$tmp));
|
|
if($data) {
|
|
$fgOK=true;
|
|
$output->writeln(' - Existe dans bundle >> '.$row["group_name"]);
|
|
}
|
|
}
|
|
|
|
if (strpos($row["group_name"], $labelniveau02.' = ') === 0) {
|
|
$tmp=str_replace("$labelniveau01 = ","",$row["group_name"]);
|
|
$data = $em->getRepository('CadolesCoreBundle:Niveau02')->findBy(array('label' =>$tmp));
|
|
if($data) {
|
|
$fgOK=true;
|
|
$output->writeln(' - Existe dans bundle >> '.$row["group_name"]);
|
|
}
|
|
}
|
|
|
|
if (strpos($row["group_name"], 'GROUPE = ') === 0) {
|
|
$tmp=str_replace("GROUPE = ","",$row["group_name"]);
|
|
$data = $em->getRepository('CadolesCoreBundle:Group')->findBy(array('label' =>$tmp));
|
|
if($data) {
|
|
$fgOK=true;
|
|
$output->writeln(' - Existe dans bundle >> '.$row["group_name"]);
|
|
}
|
|
}
|
|
|
|
if(!$fgOK) {
|
|
$output->writeln(' - A supprimer dans eportail >> '.$row["group_name"]);
|
|
if(!$simulate) delGroup($row["group_id"]);
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('== USER =============================================');
|
|
|
|
$sql = "SELECT * FROM env_user WHERE user_id>0";
|
|
$query = $dbeportail->prepare($sql);
|
|
$query->execute();
|
|
while($row=$query->fetch()){
|
|
$data = $em->getRepository('CadolesCoreBundle:User')->findBy(array('username' =>$row["user_login"]));
|
|
if($data) $output->writeln(' - Existe dans bundle >> '.$row["user_login"]);
|
|
else {
|
|
$output->writeln(' - A supprimer dans eportail >> '.$row["user_login"]);
|
|
if(!$simulate) delUser($row["user_id"]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$output->writeln('');
|
|
$output->writeln('');
|
|
}
|
|
|
|
protected static function determineKernelRootDir(Event $event) {
|
|
$extra = $event->getComposer()->getPackage()->getExtra();
|
|
$rootdir = rtrim(getcwd(), '/');
|
|
return $rootdir . '/' . trim($extra['symfony-app-dir'], '/');
|
|
}
|
|
}
|