2018-12-18 09:44:39 +01:00
< ? 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 ;
2019-04-02 11:26:41 +02:00
use Symfony\Component\Filesystem\Filesystem ;
2018-12-18 09:44:39 +01:00
use Symfony\Component\HttpKernel\KernelInterface ;
use Doctrine\DBAL\Connection as DBALConnection ;
use Doctrine\ORM\EntityManager ;
use Cadoles\CoreBundle\Entity\Niveau01 ;
use Cadoles\CoreBundle\Entity\Niveau02 ;
2019-03-12 14:50:57 +01:00
use Cadoles\CoreBundle\Entity\User ;
use Cadoles\CoreBundle\Entity\Group ;
use Cadoles\CoreBundle\Entity\UserGroup ;
2018-12-18 09:44:39 +01:00
global $bdd01 ;
global $config ;
class SynchroCommand extends Command
{
2019-03-21 17:15:06 +01:00
private $container ;
private $em ;
private $output ;
2019-04-02 11:26:41 +02:00
private $filesystem ;
private $rootlog ;
2019-03-21 17:15:06 +01:00
2018-12-18 09:44:39 +01:00
protected function configure ()
{
$this
-> setName ( 'Core:Synchro' )
-> setDescription ( 'Synchronisation Annuaire' )
-> setHelp ( 'This command Synchro for Core' )
-> addArgument ( 'simulate' , InputArgument :: OPTIONAL , 'true to simulate / false to run' )
2019-03-21 17:15:06 +01:00
-> addArgument ( 'cronid' , InputArgument :: OPTIONAL , 'ID Cron Job' )
-> addArgument ( 'lastchance' , InputArgument :: OPTIONAL , 'Lastchance to run the cron' )
2018-12-18 09:44:39 +01:00
;
}
protected function execute ( InputInterface $input , OutputInterface $output )
{
2019-03-21 17:15:06 +01:00
$this -> container = $this -> getApplication () -> getKernel () -> getContainer ();
$this -> em = $this -> container -> get ( 'doctrine' ) -> getEntityManager ();
$this -> output = $output ;
2019-04-02 11:26:41 +02:00
$this -> filesystem = new Filesystem ();
$this -> rootlog = $this -> container -> get ( 'kernel' ) -> getRootDir () . " /../var/logs/ " ;
2019-03-21 17:15:06 +01:00
$this -> writelnred ( '' );
$this -> writelnred ( '== Core:Synchro' );
2019-04-02 11:26:41 +02:00
$this -> writelnred ( '==========================================================================================================' );
2018-12-18 09:44:39 +01:00
setlocale ( LC_CTYPE , 'fr_FR' );
2019-03-21 17:15:06 +01:00
$labelniveau01 = mb_strtoupper ( $this -> container -> getParameter ( 'labelniveau01' ));
$labelniveau02 = mb_strtoupper ( $this -> container -> getParameter ( 'labelniveau02' ));
$masteridentity = $this -> container -> getParameter ( 'masteridentity' );
2018-12-18 09:44:39 +01:00
$simulate = $input -> getArgument ( 'simulate' );
if ( $simulate == " " ) $simulate = " true " ;
if ( $simulate != " true " && $simulate != " false " ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( 'Paramétre incorrect' );
2018-12-18 09:44:39 +01:00
return ;
}
$simulate = ( $simulate == " true " );
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
if ( $simulate ) $this -> writeln ( '** SIMULATION' );
else $this -> writeln ( '** REEL' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION ANNUAIRE ==========================' );
$this -> writeln ( '=====================================================' );
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$ldap = $this -> container -> get ( 'cadoles.core.service.ldap' );
2019-03-12 14:50:57 +01:00
if ( ! $ldap -> isEnabled ()) {
2019-03-21 17:15:06 +01:00
if ( $masteridentity == " LDAP " ) {
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION LDAP TO BUNDLE ====================' );
$this -> writeln ( '=====================================================' );
$ldap_basedn = $this -> container -> getParameter ( 'ldap_basedn' );
$ldap_template = $this -> container -> getParameter ( 'ldap_template' );
$ldap_username = $this -> container -> getParameter ( 'ldap_username' );
$ldap_firstname = $this -> container -> getParameter ( 'ldap_firstname' );
$ldap_lastname = $this -> container -> getParameter ( 'ldap_lastname' );
$ldap_email = $this -> container -> getParameter ( 'ldap_email' );
$ldap_usersadmin = $this -> container -> getParameter ( 'ldap_usersadmin' );
2019-03-12 14:50:57 +01:00
$fieldstoread = array ( $ldap_username , $ldap_firstname , $ldap_lastname , $ldap_email );
$ldapusers = array ();
if ( $ldap_template == " scribe " ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== PROFILS ==========================================' );
2019-03-12 14:50:57 +01:00
// Eleves
$ldapfilter = " (&(uid=*)(ENTPersonProfils=eleve)) " ;
$label = " PROFIL = Elèves " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2019-04-05 11:52:31 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
2019-03-12 14:50:57 +01:00
// Enseignants
$ldapfilter = " (|(&(uid=*)(ENTPersonProfils=enseignant))(&(uid=*)(typeadmin=0))(&(uid=*)(typeadmin=2))) " ;
$label = " PROFIL = Enseignants " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2019-04-05 11:52:31 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , true );
2019-03-12 14:50:57 +01:00
// Responsables
$ldapfilter = " (&(uid=*)(ENTPersonProfils=responsable)) " ;
$label = " PROFIL = Responsables " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2019-04-05 11:52:31 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
2019-03-12 14:50:57 +01:00
// Administratifs
$ldapfilter = " (&(uid=*)(ENTPersonProfils=administratif)) " ;
$label = " PROFIL = Administratifs " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2019-04-05 11:52:31 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , true );
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== CLASSES ==========================================' );
2019-03-12 14:50:57 +01:00
$results = $ldap -> search ( " type=Classe " , [ 'cn' , 'description' , 'gidNumber' ], $ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (|(&(type=Classe)(cn= $cn ))(&(type=Equipe)(cn=profs- $cn ))(&(ENTPersonProfils=Administratif)(divcod= $cn ))) " ;
$label = " CLASSE = " . $result [ " cn " ];
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2019-04-05 11:52:31 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , true );
2019-03-12 14:50:57 +01:00
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USERS ============================================' );
2019-03-12 14:50:57 +01:00
// On parcours les niveaux 01 pour connaitre les filtres ldap associé
2019-03-21 17:15:06 +01:00
$datas = $this -> em -> createQueryBuilder () -> select ( 'table' ) -> from ( 'CadolesCoreBundle:Niveau01' , 'table' ) -> where ( 'table.ldapfilter IS NOT NULL' ) -> getQuery () -> getResult ();
2019-03-12 14:50:57 +01:00
foreach ( $datas as $data ) {
// On execute le filtre d'appartenance à ce niveau
$results = $ldap -> search ( $data -> getLdapfilter (), $fieldstoread , $ldap_basedn );
// Pour chaque utilisateur ldap
foreach ( $results as $result ) {
// On sauvegarde ce user
array_push ( $ldapusers , $result [ $ldap_username ]);
// Création ou Modification du user
2019-03-21 17:15:06 +01:00
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findBy ( array ( 'username' => $result [ $ldap_username ]));
2019-03-12 14:50:57 +01:00
if ( ! $user ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Création dans Bundle >> " . $result [ $ldap_username ]);
if ( ! $simulate ) $this -> addUser ( $data , $result [ $ldap_username ], $result [ $ldap_firstname ], $result [ $ldap_lastname ], $result [ $ldap_email ], $ldap_usersadmin );
2019-03-12 14:50:57 +01:00
}
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Modification dans Bundle >> " . $result [ $ldap_username ]);
if ( ! $simulate ) $this -> modUser ( $user [ 0 ], $result [ $ldap_username ], $result [ $ldap_firstname ], $result [ $ldap_lastname ], $result [ $ldap_email ], $ldap_usersadmin );
2019-03-12 14:50:57 +01:00
}
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USERS GROUP ======================================' );
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$groups = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findAll ();
2019-03-12 14:50:57 +01:00
foreach ( $groups as $group ) {
$ldapusersgroup = array ();
$ldapfilter = $group -> getLdapfilter ();
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== ' . $group -> getLabel ());
2019-03-12 14:50:57 +01:00
if ( ! is_null ( $ldapfilter )) {
$results = $ldap -> search ( $ldapfilter ,[ $ldap_username , " memberuid " ] , $ldap_basedn );
foreach ( $results as $result ) {
if ( isset ( $result [ " memberuid " ])) {
// Si memberid est un tableau il y a plusieur user dedans
if ( is_array ( $result [ " memberuid " ])) {
foreach ( $result [ " memberuid " ] as $key => $value ) {
if ( is_int ( $key )) {
2019-03-21 17:15:06 +01:00
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $value ));
2019-03-12 14:50:57 +01:00
if ( $user ) {
array_push ( $ldapusersgroup , $value );
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Rattacher >> " . $value );
if ( ! $simulate ) $this -> addtoGroup ( $user , $group );
2019-03-12 14:50:57 +01:00
}
}
}
}
// sinon m'a qu'un seul uid
else {
2019-03-21 17:15:06 +01:00
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $result [ " memberuid " ]));
2019-03-12 14:50:57 +01:00
if ( $user ) {
array_push ( $ldapusersgroup , $result [ " memberuid " ]);
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Rattacher >> " . $result [ " memberuid " ]);
if ( ! $simulate ) $this -> addtoGroup ( $user , $group );
2019-03-12 14:50:57 +01:00
}
}
}
if ( isset ( $result [ $ldap_username ])) {
2019-03-21 17:15:06 +01:00
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $result [ $ldap_username ]));
2019-03-12 14:50:57 +01:00
if ( $user ) {
array_push ( $ldapusersgroup , $result [ $ldap_username ]);
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Rattacher >> " . $result [ $ldap_username ]);
if ( ! $simulate ) $this -> addtoGroup ( $user , $group );
2019-03-12 14:50:57 +01:00
}
}
}
2019-03-21 17:15:06 +01:00
$members = $this -> em -> getRepository ( 'CadolesCoreBundle:UserGroup' ) -> findBy ( array ( 'group' => $group ));
2019-03-12 14:50:57 +01:00
foreach ( $members as $member ) {
if ( ! in_array ( $member -> getUser () -> getUsername (), $ldapusersgroup )) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Détattacher >> " . $member -> getUser () -> getUsername ());
2019-03-12 14:50:57 +01:00
if ( ! $simulate ) {
2019-03-21 17:15:06 +01:00
$this -> em -> remove ( $member );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
}
}
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION BUNDLE TO LDAP ====================' );
$this -> writeln ( '=====================================================' );
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USERS ============================================' );
2019-03-12 14:50:57 +01:00
// Pour chaque utilisateur de la base
2019-03-21 17:15:06 +01:00
$users = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findAll ();
2019-03-12 14:50:57 +01:00
foreach ( $users as $user ) {
// Si l'utilisateur n'est pas dans la liste des users ldap : on le supprime
if ( ! in_array ( $user -> getUsername (), $ldapusers )) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - Suppression dans Bundle >> " . $user -> getUsername ());
2019-03-12 14:50:57 +01:00
if ( ! $simulate ) {
2019-03-21 17:15:06 +01:00
$this -> em -> remove ( $user );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
}
}
}
}
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION BUNDLE TO LDAP ====================' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU01 =========================================' );
$baseNiveau01 = $this -> container -> getParameter ( 'ldap_baseniveau01' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau01' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
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 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> modifyNiveau01 ( $data , $data -> getLabel ());
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> addNiveau01 ( $data );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU02 =========================================' );
$baseNiveau02 = $this -> container -> getParameter ( 'ldap_baseniveau02' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau02' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
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 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> modifyNiveau02 ( $data , $data -> getLabel ());
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> addNiveau02 ( $data );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== GROUP ============================================' );
$baseGroup = $this -> container -> getParameter ( 'ldap_basegroup' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Group' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
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 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> modifyGroup ( $data , $data -> getLabel ());
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> addGroup ( $data );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USER =============================================' );
$baseUser = $this -> container -> getParameter ( 'ldap_baseuser' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:User' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
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 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getUsername ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> modifyUser ( $data );
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getUsername ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $ldap -> addUser ( $data );
}
// Rattachement à Niveau01 et Niveau02
if ( ! $simulate ) $ldap -> addGroupUser ( $data );
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USER GROUP =======================================' );
$baseGroup = $this -> container -> getParameter ( 'ldap_basegroup' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Group' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
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 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
$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 ()));
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' > ' . $usergroupe -> getUser () -> getUsername ());
2018-12-18 09:44:39 +01:00
}
if ( ! $simulate ) $ldap -> ldapModify ( $dn , $attrs );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION LDAP TO BUNDLE ====================' );
$this -> writeln ( '=====================================================' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU01 =========================================' );
$baseNiveau01 = $this -> container -> getParameter ( 'ldap_baseniveau01' );
2018-12-18 09:44:39 +01:00
$criteria = '(cn=*)' ;
$subbranch = $baseNiveau01 ;
$results = $ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
foreach ( $results as $result ) {
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau01' ) -> findBy ( array ( 'label' => $result [ " cn " ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans annuaire >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
$dn = $ldap -> getNiveau01DN ( $result [ " cn " ]);
if ( ! $simulate ) $ldap -> deleteByDN ( $dn );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU02 =========================================' );
$baseNiveau02 = $this -> container -> getParameter ( 'ldap_baseniveau02' );
2018-12-18 09:44:39 +01:00
$criteria = '(cn=*)' ;
$subbranch = $baseNiveau02 ;
$results = $ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
foreach ( $results as $result ) {
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau02' ) -> findBy ( array ( 'label' => $result [ " cn " ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans annuaire >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
$dn = $ldap -> getNiveau02DN ( $result [ " cn " ]);
if ( ! $simulate ) $ldap -> deleteByDN ( $dn );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== GROUP ============================================' );
$baseGroup = $this -> container -> getParameter ( 'ldap_basegroup' );
2018-12-18 09:44:39 +01:00
$criteria = '(cn=*)' ;
$subbranch = $baseGroup ;
$results = $ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
foreach ( $results as $result ) {
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findBy ( array ( 'label' => $result [ " cn " ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans annuaire >> ' . $result [ " cn " ]);
2018-12-18 09:44:39 +01:00
$dn = $ldap -> getGroupDN ( $result [ " cn " ]);
if ( ! $simulate ) $ldap -> deleteByDN ( $dn );
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USER =============================================' );
$baseUser = $this -> container -> getParameter ( 'ldap_baseuser' );
2018-12-18 09:44:39 +01:00
$criteria = '(uid=*)' ;
$subbranch = $baseUser ;
$results = $ldap -> search ( $criteria , array ( 'uid' ), $subbranch );
foreach ( $results as $result ) {
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findBy ( array ( 'username' => $result [ " uid " ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $result [ " uid " ]);
2018-12-18 09:44:39 +01:00
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans annuaire >> ' . $result [ " uid " ]);
2018-12-18 09:44:39 +01:00
$dn = 'uid=' . $result [ " uid " ] . ',' . $baseUser ;
if ( ! $simulate ) $ldap -> deleteByDN ( $dn );
}
}
}
2019-03-21 17:15:06 +01:00
$eportail = $this -> container -> get ( 'cadoles.core.service.eportail' );
if ( $eportail -> isEnabled ()) {
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION BUNDLE TO EPORTAIL ================' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU01 =========================================' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau01' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
foreach ( $datas as $data ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Synchronisation eportail >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $eportail -> syncNiveau01 ( $data , $data -> getLabel ());
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAU02 =========================================' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau02' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
foreach ( $datas as $data ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Synchronisation eportail >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $eportail -> syncNiveau02 ( $data , $data -> getLabel ());
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== GROUP ============================================' );
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Group' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
foreach ( $datas as $data ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Synchronisation eportail >> ' . $data -> getLabel ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $eportail -> syncGroup ( $data , $data -> getLabel ());
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USER =============================================' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:User' , 'table' )
-> getQuery ()
-> getResult ();
2018-12-18 09:44:39 +01:00
foreach ( $datas as $data ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Synchronisation eportail >> ' . $data -> getUsername ());
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) $eportail -> syncUser ( $data );
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '=====================================================' );
$this -> writeln ( '== SYNCHONISATION EPORTAIL TO BUNDLE ================' );
$this -> writeln ( '=====================================================' );
$dbeportail = $this -> container -> get ( 'service_container' ) -> get ( 'doctrine.orm.eportail_entity_manager' ) -> getConnection ();
2018-12-18 09:44:39 +01:00
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== GROUP ============================================' );
2018-12-18 09:44:39 +01:00
$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 " ]);
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau01' ) -> findBy ( array ( 'label' => $tmp ));
2018-12-18 09:44:39 +01:00
if ( $data ) {
$fgOK = true ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Existe dans bundle >> ' . $row [ " group_name " ]);
2018-12-18 09:44:39 +01:00
}
}
if ( strpos ( $row [ " group_name " ], $labelniveau02 . ' = ' ) === 0 ) {
$tmp = str_replace ( " $labelniveau01 = " , " " , $row [ " group_name " ]);
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau02' ) -> findBy ( array ( 'label' => $tmp ));
2018-12-18 09:44:39 +01:00
if ( $data ) {
$fgOK = true ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Existe dans bundle >> ' . $row [ " group_name " ]);
2018-12-18 09:44:39 +01:00
}
}
if ( strpos ( $row [ " group_name " ], 'GROUPE = ' ) === 0 ) {
$tmp = str_replace ( " GROUPE = " , " " , $row [ " group_name " ]);
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findBy ( array ( 'label' => $tmp ));
2018-12-18 09:44:39 +01:00
if ( $data ) {
$fgOK = true ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Existe dans bundle >> ' . $row [ " group_name " ]);
2018-12-18 09:44:39 +01:00
}
}
if ( ! $fgOK ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans eportail >> ' . $row [ " group_name " ]);
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) delGroup ( $row [ " group_id " ]);
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== USER =============================================' );
2018-12-18 09:44:39 +01:00
$sql = " SELECT * FROM env_user WHERE user_id>0 " ;
$query = $dbeportail -> prepare ( $sql );
$query -> execute ();
while ( $row = $query -> fetch ()){
2019-03-21 17:15:06 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findBy ( array ( 'username' => $row [ " user_login " ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $row [ " user_login " ]);
2018-12-18 09:44:39 +01:00
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - A supprimer dans eportail >> ' . $row [ " user_login " ]);
2018-12-18 09:44:39 +01:00
if ( ! $simulate ) delUser ( $row [ " user_id " ]);
}
}
}
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
2019-04-02 11:26:41 +02:00
return 1 ;
2018-12-18 09:44:39 +01:00
}
2019-04-02 11:26:41 +02:00
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 " );
}
2019-03-21 17:15:06 +01:00
2019-04-05 11:52:31 +02:00
protected function addmodGroup ( $label , $ldapfilter , $fgcanshare ) {
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$group = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findOneBy ( array ( 'fgtemplate' => true , 'label' => $label ));
2019-03-12 14:50:57 +01:00
if ( ! $group ) {
$group = new Group ();
2019-04-05 11:52:31 +02:00
$group -> setFgcanshare ( $fgcanshare );
2019-03-12 14:50:57 +01:00
}
$group -> setLabel ( $label );
$group -> setFgopen ( false );
$group -> setFgall ( false );
$group -> setLdapfilter ( $ldapfilter );
$group -> setFgtemplate ( true );
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $group );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
2019-03-21 17:15:06 +01:00
protected function addtoGroup ( $user , $group ) {
$member = $this -> em -> getRepository ( 'CadolesCoreBundle:UserGroup' ) -> findOneBy ( array ( 'group' => $group , 'user' => $user ));
2019-03-12 14:50:57 +01:00
if ( ! $member ) {
$member = new UserGroup ();
$member -> setGroup ( $group );
$member -> setUser ( $user );
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $member );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
}
2019-03-21 17:15:06 +01:00
protected function addUser ( $niveau01 , $username , $firstname , $lastname , $email , $usersadmin ) {
2019-03-12 14:50:57 +01:00
$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 " );
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $user );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
2019-03-21 17:15:06 +01:00
protected function modUser ( $user , $username , $firstname , $lastname , $email , $usersadmin ) {
2019-03-12 14:50:57 +01:00
$user -> setLastname ( $lastname );
$user -> setFirstname ( $firstname );
$user -> setEmail ( $email );
if ( in_array ( $username , $usersadmin ))
$user -> setRole ( " ROLE_ADMIN " );
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $user );
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
2018-12-18 09:44:39 +01:00
}