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 ;
2019-09-30 16:30:52 +02:00
use Cadoles\PortalBundle\Entity\Calendar ;
use Cadoles\PortalBundle\Entity\Blog ;
2020-04-14 08:56:42 +02:00
use Cadoles\PortalBundle\Entity\Project ;
2019-09-30 16:30:52 +02:00
use Ramsey\Uuid\Uuid ;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException ;
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 ;
2019-08-23 17:01:04 +02:00
private $em2 ;
2019-03-21 17:15:06 +01:00
private $output ;
2019-04-02 11:26:41 +02:00
private $filesystem ;
private $rootlog ;
2019-09-30 16:30:52 +02:00
private $ldap ;
private $ldap_basedn ;
2021-01-08 14:53:43 +01:00
private $ldap_baseuser ;
private $ldap_basegroup ;
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
2021-01-12 12:03:02 +01:00
$this -> ldap_basedn = $this -> container -> getParameter ( 'ldap_basedn' );
$this -> ldap_baseuser = $this -> container -> getParameter ( 'ldap_baseuser' );
$this -> ldap_basegroup = $this -> container -> getParameter ( 'ldap_basegroup' );
$ldap_username = strtolower ( $this -> container -> getParameter ( 'ldap_username' ));
$ldap_firstname = strtolower ( $this -> container -> getParameter ( 'ldap_firstname' ));
$ldap_lastname = strtolower ( $this -> container -> getParameter ( 'ldap_lastname' ));
$ldap_email = strtolower ( $this -> container -> getParameter ( 'ldap_email' ));
$ldap_member = strtolower ( $this -> container -> getParameter ( 'ldap_member' ));
$scribe_group = strtolower ( $this -> container -> getParameter ( 'scribe_group' ));
2021-02-24 10:54:13 +01:00
$ldap_type = $this -> container -> getParameter ( 'ldap_type' );
2021-01-12 12:03:02 +01:00
$ldap_template = $this -> container -> getParameter ( 'ldap_template' );
$ldap_usersadmin = $this -> container -> getParameter ( 'ldap_usersadmin' );
$scribe_master = strtolower ( $this -> container -> getParameter ( 'scribe_master' ));
$fieldstoread = array ( $ldap_username , $ldap_firstname , $ldap_lastname , $ldap_email );
$ldapusers = array ();
$ldapmails = array ();
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-09-30 16:30:52 +02:00
$this -> ldap = $this -> container -> get ( 'cadoles.core.service.ldap' );
if ( ! $this -> 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 ( '=====================================================' );
2019-03-12 14:50:57 +01:00
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)) " ;
2020-07-10 09:31:00 +02:00
$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))) " ;
2020-07-10 09:31:00 +02:00
$label = " PROFIL - Enseignants " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2022-07-06 11:11:59 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
2019-03-12 14:50:57 +01:00
// Responsables
$ldapfilter = " (&(uid=*)(ENTPersonProfils=responsable)) " ;
2020-07-10 09:31:00 +02:00
$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)) " ;
2020-07-10 09:31:00 +02:00
$label = " PROFIL - Administratifs " ;
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2022-07-06 11:11:59 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
2019-03-12 14:50:57 +01:00
2022-05-09 13:51:33 +02:00
// Niveaux
$this -> writeln ( '' );
$this -> writeln ( '== NIVEAUX ==========================================' );
$results = $this -> ldap -> search ( " type=Niveau " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (&(type=Niveau)(cn= $cn )) " ;
$label = " NIVEAU - " . $result [ " cn " ];
$this -> writeln ( " - $label " );
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
}
2019-10-15 10:02:33 +02:00
// Classes
2019-03-21 17:15:06 +01:00
$this -> writeln ( '' );
$this -> writeln ( '== CLASSES ==========================================' );
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( " type=Classe " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
2019-03-12 14:50:57 +01:00
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (|(&(type=Classe)(cn= $cn ))(&(type=Equipe)(cn=profs- $cn ))(&(ENTPersonProfils=Administratif)(divcod= $cn ))) " ;
2020-07-10 09:31:00 +02:00
$label = " CLASSE - " . $result [ " cn " ];
2019-03-21 17:15:06 +01:00
$this -> writeln ( " - $label " );
2020-07-15 14:38:16 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , $scribe_group );
2019-03-12 14:50:57 +01:00
}
2019-10-15 10:02:33 +02:00
2022-05-09 13:51:33 +02:00
// Elèves des Classes
$this -> writeln ( '' );
$this -> writeln ( '== ELEVES DES CLASSES ==========================================' );
$results = $this -> ldap -> search ( " type=Classe " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (&(type=Classe)(cn= $cn )) " ;
2022-05-10 09:12:21 +02:00
$label = " ELEVES - " . $result [ " cn " ];
2022-05-09 13:51:33 +02:00
$this -> writeln ( " - $label " );
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
}
// Professeurs des Classes
$this -> writeln ( '' );
$this -> writeln ( '== PROFESSEURS DES CLASSES ==========================================' );
$results = $this -> ldap -> search ( " type=Classe " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (|(&(type=Equipe)(cn=profs- $cn ))(&(ENTPersonProfils=Administratif)(divcod= $cn ))) " ;
2022-05-10 09:12:21 +02:00
$label = " PROFESSEURS - " . $result [ " cn " ];
2022-05-09 13:51:33 +02:00
$this -> writeln ( " - $label " );
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
}
2019-10-15 10:02:33 +02:00
// Options
$this -> writeln ( '' );
$this -> writeln ( '== OPTIONS ==========================================' );
$results = $this -> ldap -> search ( " type=Option " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (|(&(type=Option)(cn= $cn ))(&(type=Equipe)(cn=profs- $cn ))(&(ENTPersonProfils=Administratif)(divcod= $cn ))) " ;
2020-07-10 09:31:00 +02:00
$label = " OPTION - " . $result [ " cn " ];
2019-10-15 10:02:33 +02:00
$this -> writeln ( " - $label " );
2020-07-15 14:38:16 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , $scribe_group );
2019-10-15 10:02:33 +02:00
}
// Groupes
$this -> writeln ( '' );
$this -> writeln ( '== GROUPES ==========================================' );
$results = $this -> ldap -> search ( " type=Groupe " , [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basedn );
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (&(type=Groupe)(cn= $cn )) " ;
2020-07-10 09:31:00 +02:00
$label = " GROUPE - " . $result [ " cn " ];
2019-10-15 10:02:33 +02:00
$this -> writeln ( " - $label " );
2020-07-15 14:38:16 +02:00
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , $scribe_group );
2019-10-15 10:02:33 +02:00
}
2019-03-12 14:50:57 +01:00
}
2020-09-29 17:18:21 +02:00
elseif ( $ldap_template == " open " ) {
if ( $this -> container -> getParameter ( 'openldapsynchrogroup' )) {
$this -> writeln ( '' );
$this -> writeln ( '== GROUPES ==========================================' );
2021-01-08 14:53:43 +01:00
$results = $this -> ldap -> search ( $this -> container -> getParameter ( 'openldapreqgroup' ), [ 'cn' , 'description' , 'gidNumber' ], $this -> ldap_basegroup );
2020-09-29 17:18:21 +02:00
foreach ( $results as $result ) {
$cn = $result [ " cn " ];
$ldapfilter = " (& " . $this -> container -> getParameter ( 'openldapreqgroup' ) . " (cn= $cn )) " ;
$label = $result [ " cn " ];
$this -> writeln ( " - $label " );
if ( ! $simulate ) $this -> addmodGroup ( $label , $ldapfilter , false );
}
}
}
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
2019-06-24 15:24:28 +02:00
// On stocke tout les email déjà existant
2019-08-23 17:01:04 +02:00
$this -> writeln ( '== Stocker les emails utilisateurs existants' );
$users = $this -> em -> createQueryBuilder () -> select ( 'table.email' ) -> from ( 'CadolesCoreBundle:User' , 'table' ) -> getQuery () -> getArrayResult ();
2019-06-24 15:24:28 +02:00
foreach ( $users as $user ) {
2019-08-23 17:01:04 +02:00
array_push ( $ldapmails , $user [ " email " ]);
}
2019-06-24 15:24:28 +02:00
2019-03-12 14:50:57 +01:00
// On parcours les niveaux 01 pour connaitre les filtres ldap associé
2019-06-25 15:53:10 +02:00
$nbusers = 0 ;
2019-08-23 17:01:04 +02:00
$datas = $this -> em -> createQueryBuilder () -> select ( 'table' ) -> from ( 'CadolesCoreBundle:Niveau01' , 'table' ) -> where ( 'table.ldapfilter IS NOT NULL' ) -> getQuery () -> getArrayResult ();
$tberrors = [];
2019-03-12 14:50:57 +01:00
foreach ( $datas as $data ) {
// On execute le filtre d'appartenance à ce niveau
2019-08-23 17:01:04 +02:00
$this -> writeln ( " == Récupération des utilisateurs de l'annuaire " );
$niveau01 = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau01' ) -> find ( $data [ " id " ]);
2021-01-08 14:53:43 +01:00
$results = $this -> ldap -> search ( $niveau01 -> getLdapfilter (), $fieldstoread , $this -> ldap_baseuser );
2019-08-23 17:01:04 +02:00
$nbuserstotal = count ( $results );
2019-03-12 14:50:57 +01:00
// Pour chaque utilisateur ldap
2019-08-23 17:01:04 +02:00
$this -> writeln ( '== Traitement des utilisateurs du niveau = ' . $niveau01 -> getLabel () . " = " . $niveau01 -> getLdapfilter ());
2019-03-12 14:50:57 +01:00
foreach ( $results as $result ) {
2019-08-23 17:01:04 +02:00
// Niveau 01 en cours à récupérer car on clear à em à chaque $result
$niveau01 = $this -> em -> getRepository ( 'CadolesCoreBundle:Niveau01' ) -> find ( $data [ " id " ]);
2019-06-25 15:53:10 +02:00
// Compteur de users
$nbusers ++ ;
2019-06-24 15:24:28 +02:00
// Formatage du résultat
if ( is_array ( $result [ $ldap_username ])) {
$result [ $ldap_username ] = $result [ $ldap_username ][ 0 ];
}
$result [ $ldap_username ] = utf8_encode ( $result [ $ldap_username ]);
if ( ! isset ( $result [ $ldap_lastname ])) $result [ $ldap_lastname ] = " " ;
if ( ! isset ( $result [ $ldap_firstname ])) $result [ $ldap_firstname ] = " " ;
2021-01-08 14:53:43 +01:00
2021-01-21 16:00:15 +01:00
if ( ! array_key_exists ( $ldap_email , $result ) || empty ( $result [ $ldap_email ])) {
$result [ $ldap_email ] = $result [ $ldap_username ] . " @nomail.fr " ;
2020-09-28 13:51:25 +02:00
}
2019-06-24 15:24:28 +02:00
$result [ $ldap_email ] = strtolower ( $result [ $ldap_email ]);
$result [ $ldap_email ] = utf8_encode ( $result [ $ldap_email ]);
2019-03-12 14:50:57 +01:00
// On sauvegarde ce user
2019-06-24 15:24:28 +02:00
if ( in_array ( $result [ $ldap_username ], $ldapusers )) {
$this -> writelnred ( " - Création dans Bundle impossible >> " . $result [ $ldap_username ] . " deux users avec le meme uid " );
continue ;
}
2019-03-12 14:50:57 +01:00
array_push ( $ldapusers , $result [ $ldap_username ]);
// Création ou Modification du user
2019-06-24 15:24:28 +02:00
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $result [ $ldap_username ]));
2019-03-12 14:50:57 +01:00
if ( ! $user ) {
2019-04-18 15:13:54 +02:00
if ( empty ( $result [ $ldap_email ]))
2019-08-23 17:01:04 +02:00
array_push ( $tberrors , " - Création dans Bundle impossible >> " . $result [ $ldap_username ] . " sans email " );
2019-04-18 15:13:54 +02:00
else {
2019-06-24 15:24:28 +02:00
if ( in_array ( $result [ $ldap_email ], $ldapmails ))
2019-08-23 17:01:04 +02:00
array_push ( $tberrors , " - Création dans Bundle impossible >> " . $result [ $ldap_username ] . " un autre utilisateur a déjà ce mail = " . $result [ $ldap_email ]);
2019-06-24 15:24:28 +02:00
else {
array_push ( $ldapmails , $result [ $ldap_email ]);
$this -> writeln ( " - Création dans Bundle >> " . $result [ $ldap_username ]);
2019-08-23 17:01:04 +02:00
if ( ! $simulate ) $this -> addUser ( $niveau01 , $result [ $ldap_username ], $result [ $ldap_firstname ], $result [ $ldap_lastname ], $result [ $ldap_email ], $ldap_usersadmin );
2019-06-24 15:24:28 +02:00
}
2019-04-18 15:13:54 +02:00
}
2019-03-12 14:50:57 +01:00
}
else {
2019-06-24 11:48:49 +02:00
$toadmin = false ;
2019-06-24 15:24:28 +02:00
if ( in_array ( $result [ $ldap_username ], $ldap_usersadmin ) &&! in_array ( " ROLE_ADMIN " , $user -> getRoles ()))
2019-06-24 11:48:49 +02:00
$toadmin = true ;
2019-06-24 15:24:28 +02:00
if ( $user -> getLastname () != $result [ $ldap_lastname ] || $user -> getFirstname () != $result [ $ldap_firstname ] || $user -> getEmail () != $result [ $ldap_email ] || $toadmin ) {
$usermail = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'email' => $result [ $ldap_email ]));
2020-07-15 14:07:39 +02:00
if ( $usermail && $usermail != $user ) {
2019-08-23 17:01:04 +02:00
array_push ( $tberrors , " - Modification dans Bundle impossible >> " . $result [ $ldap_username ] . " un autre utilisateur a déjà ce mail = " . $result [ $ldap_email ]);
2019-06-24 15:24:28 +02:00
}
else {
$this -> writeln ( " - Modification dans Bundle >> " . $result [ $ldap_username ]);
if ( ! $simulate ) $this -> modUser ( $user , $result [ $ldap_username ], $result [ $ldap_firstname ], $result [ $ldap_lastname ], $result [ $ldap_email ], $ldap_usersadmin );
}
2019-06-24 11:48:49 +02:00
}
2019-03-12 14:50:57 +01:00
}
2019-06-25 15:53:10 +02:00
2019-08-23 17:01:04 +02:00
if ( ! $simulate ) {
$this -> em -> clear ();
2019-06-25 15:53:10 +02:00
}
2019-08-23 17:01:04 +02:00
if ( $nbusers % 1000 == 0 ) $this -> writeln ( " == Nombre d'utilisateurs traités = $nbusers sur $nbuserstotal == " );
2019-03-12 14:50:57 +01:00
}
}
2019-06-25 15:53:10 +02:00
if ( ! $simulate ) {
2019-08-23 17:01:04 +02:00
$this -> writeln ( " == Nombre d'utilisateurs traités = $nbusers sur $nbuserstotal == " );
2019-06-25 15:53:10 +02:00
$this -> em -> flush ();
2019-08-23 17:01:04 +02:00
$this -> em -> clear ();
}
foreach ( $tberrors as $error ) {
$this -> writelnred ( " == ERROR == $error " );
2019-06-25 15:53:10 +02:00
}
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 )) {
2021-01-08 14:53:43 +01:00
$results = $this -> ldap -> search ( $ldapfilter ,[ $ldap_username , $ldap_member ] , $this -> ldap_basedn );
2019-09-30 16:30:52 +02:00
2019-03-12 14:50:57 +01:00
foreach ( $results as $result ) {
2021-01-08 14:53:43 +01:00
if ( isset ( $result [ $ldap_member ])) {
2019-03-12 14:50:57 +01:00
// Si memberid est un tableau il y a plusieur user dedans
2021-01-08 14:53:43 +01:00
if ( is_array ( $result [ $ldap_member ])) {
foreach ( $result [ $ldap_member ] as $key => $value ) {
2019-03-12 14:50:57 +01:00
if ( is_int ( $key )) {
2021-01-08 14:53:43 +01:00
$username = $value ;
// si le username forme un DN, il faut récupérer juste la première entrée
$tmp = explode ( " , " , $username );
if ( is_array ( $tmp ) && count ( $tmp ) > 1 ) {
$tmp = explode ( " = " , $tmp [ 0 ]);
$username = $tmp [ 1 ];
}
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $username ));
2019-03-12 14:50:57 +01:00
if ( $user ) {
2021-01-08 14:53:43 +01:00
array_push ( $ldapusersgroup , $username );
$this -> writeln ( " - Rattacher >> " . $username );
2019-03-21 17:15:06 +01:00
if ( ! $simulate ) $this -> addtoGroup ( $user , $group );
2019-03-12 14:50:57 +01:00
}
}
}
}
// sinon m'a qu'un seul uid
else {
2021-01-08 14:53:43 +01:00
$username = $result [ $ldap_member ];
// si le username forme un DN, il faut récupérer juste la première entrée
$tmp = explode ( " , " , $username );
if ( is_array ( $tmp ) && count ( $tmp ) > 1 ) {
$tmp = explode ( " = " , $tmp [ 0 ]);
$username = $tmp [ 1 ];
}
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findOneBy ( array ( 'username' => $username ));
2019-03-12 14:50:57 +01:00
if ( $user ) {
2021-01-08 14:53:43 +01:00
array_push ( $ldapusersgroup , $username );
$this -> writeln ( " - Rattacher >> " . $username );
2019-03-21 17:15:06 +01:00
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-08-23 17:01:04 +02:00
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-08-23 17:01:04 +02:00
//$users=$this->em->getRepository('CadolesCoreBundle:User')->findAll();
$datas = $this -> em -> createQueryBuilder () -> select ( 'table.id,table.username' ) -> from ( 'CadolesCoreBundle:User' , 'table' ) -> getQuery () -> getArrayResult ();
$nbusers = 0 ;
// tentative d'optimisation
$flipped = array_flip ( $ldapusers );
foreach ( $datas as $data ) {
$nbusers ++ ;
2019-03-12 14:50:57 +01:00
// Si l'utilisateur n'est pas dans la liste des users ldap : on le supprime
2019-08-23 17:01:04 +02:00
if ( ! isset ( $flipped [ $data [ " username " ]])) {
$user = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> find ( $data [ " id " ]);
2019-04-18 15:13:54 +02:00
if ( $user -> getUsername () == " admin " )
$this -> writeln ( " - Ne jamais supprimer >> " . $user -> getUsername ());
else {
$this -> writeln ( " - Suppression dans Bundle >> " . $user -> getUsername ());
if ( ! $simulate ) {
$this -> em -> remove ( $user );
2019-08-23 17:01:04 +02:00
$this -> em -> flush ();
2019-04-18 15:13:54 +02:00
}
2019-03-12 14:50:57 +01:00
}
}
2019-08-23 17:01:04 +02:00
else {
//on peut unset les recherches suivantes seront plus rapide
2019-09-30 16:30:52 +02:00
unset ( $flipped [ $data [ " username " ]]);
2019-08-23 17:01:04 +02:00
}
$this -> em -> clear ();
if ( $nbusers % 1000 == 0 ) $this -> writelnred ( " == Nombre d'utilisateurs traités = $nbusers == " );
2019-03-12 14:50:57 +01:00
}
2020-07-15 15:08:42 +02:00
$this -> writeln ( '' );
$this -> writeln ( '== GROUPS ============================================' );
// Pour chaque groupe annuaire on s'assure que le groupe existe bien dans l'annuaire
$groups = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findBy ([ " fgtemplate " => true ]);
foreach ( $groups as $group ) {
if ( stripos ( $group -> getLabel (), " PROFIL - " ) === false ) {
$results = $this -> ldap -> search ( $group -> getLdapfilter (), [], $this -> ldap_basedn );
if ( count ( $results ) == 0 ) {
$this -> writeln ( " - Suppression dans Bundle >> " . $group -> getLabel ());
if ( ! $simulate ) {
if ( $group -> getFgcanshare ()) {
// Sur l'ensemble des pages liés
$pages = $group -> getPages ();
foreach ( $pages as $page ) {
$getgroups = $page -> getGroups ();
// si la page est lié qu'à un seul group on peut la supprimer
if ( $getgroups -> count () == 1 )
$this -> em -> remove ( $page );
}
// Sur l'ensemble des calendar liés
$calendars = $group -> getCalendars ();
foreach ( $calendars as $calendar ) {
$getgroups = $calendar -> getGroups ();
// si la page est lié qu'à un seul group on peut la supprimer
if ( $getgroups -> count () == 1 )
$this -> em -> remove ( $calendar );
}
// Sur l'ensemble des blog liés
$blogs = $group -> getBlogs ();
foreach ( $blogs as $blog ) {
$getgroups = $blog -> getGroups ();
// si la page est lié qu'à un seul group on peut la supprimer
if ( $getgroups -> count () == 1 )
$this -> em -> remove ( $blog );
}
// Sur l'ensemble des project liés
$projects = $group -> getProjects ();
foreach ( $projects as $project ) {
$getgroups = $project -> getGroups ();
// si la page est lié qu'à un seul group on peut la supprimer
if ( $getgroups -> count () == 1 )
$this -> em -> remove ( $project );
}
}
$this -> em -> remove ( $group );
$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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
// Mise à jour si elle existe
if ( count ( $results ) > 0 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2021-01-12 12:03:02 +01:00
if ( ! $simulate ) $this -> ldap -> modifyNiveau01 ( $data , $data -> getLabel (), $results [ 0 ][ " cn " ]);
2018-12-18 09:44:39 +01:00
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> addNiveau01 ( $data );
2018-12-18 09:44:39 +01:00
}
}
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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
// Mise à jour si elle existe
if ( count ( $results ) > 0 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2021-01-12 12:03:02 +01:00
if ( ! $simulate ) $this -> ldap -> modifyNiveau02 ( $data , $data -> getLabel (), $results [ 0 ][ " cn " ]);
2018-12-18 09:44:39 +01:00
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> addNiveau02 ( $data );
2018-12-18 09:44:39 +01:00
}
}
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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
// Mise à jour si elle existe
if ( count ( $results ) > 0 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Modification dans annuaire >> ' . $data -> getLabel ());
2021-01-12 12:03:02 +01:00
if ( ! $simulate ) $this -> ldap -> modifyGroup ( $data , $data -> getLabel (), $results [ 0 ][ " cn " ]);
2018-12-18 09:44:39 +01:00
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> addGroup ( $data );
2018-12-18 09:44:39 +01:00
}
}
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 ) {
2021-01-12 12:03:02 +01:00
$criteria = '(' . $ldap_username . '=' . $data -> getUsername () . ')' ;
2018-12-18 09:44:39 +01:00
$subbranch = $baseUser ;
2021-01-12 12:03:02 +01:00
$results = $this -> ldap -> search ( $criteria , array ( $ldap_username ), $subbranch );
2018-12-18 09:44:39 +01:00
// 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 ());
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> modifyUser ( $data );
2018-12-18 09:44:39 +01:00
}
// Sinon création de la fiche
else {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - Création dans annuaire >> ' . $data -> getUsername ());
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> addUser ( $data );
2018-12-18 09:44:39 +01:00
}
// Rattachement à Niveau01 et Niveau02
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> addGroupUser ( $data );
2018-12-18 09:44:39 +01:00
}
2019-06-06 16:37:34 +02:00
$this -> writeln ( '' );
$this -> writeln ( '== USER NIVEAU01 =======================================' );
$baseniveau01 = $this -> container -> getParameter ( 'ldap_baseniveau01' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau01' , 'table' )
-> getQuery ()
-> getResult ();
foreach ( $datas as $data ) {
$criteria = '(cn=' . $data -> getLabel () . ')' ;
$subbranch = $baseniveau01 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2019-06-06 16:37:34 +02:00
// Mise à jour des membres du groupes
if ( count ( $results ) > 0 ) {
$this -> writeln ( ' - ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getNiveau01DN ( $data -> getLabel ());
2019-06-06 16:37:34 +02:00
$attrs [ " memberuid " ] = array ();
$attrs [ " cadolesMember " ] = array ();
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) $attrs [ " member " ] = array ();
2019-06-06 16:37:34 +02:00
foreach ( $data -> getUsers () as $userniveau01 ) {
array_push ( $attrs [ " memberuid " ], $userniveau01 -> getUsername ());
2019-09-30 16:30:52 +02:00
array_push ( $attrs [ " cadolesMember " ], $this -> ldap -> getUserDN ( $userniveau01 ));
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) array_push ( $attrs [ " member " ], $this -> ldap -> getUserDN ( $userniveau01 ));
2019-06-06 16:37:34 +02:00
$this -> writeln ( ' > ' . $userniveau01 -> getUsername ());
}
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> ldapModify ( $dn , $attrs );
2019-06-06 16:37:34 +02:00
}
}
$this -> writeln ( '' );
$this -> writeln ( '== USER NIVEAU02 =======================================' );
$baseniveau02 = $this -> container -> getParameter ( 'ldap_baseniveau02' );
$datas = $this -> em
-> createQueryBuilder ()
-> select ( 'table' )
-> from ( 'CadolesCoreBundle:Niveau02' , 'table' )
-> getQuery ()
-> getResult ();
foreach ( $datas as $data ) {
$criteria = '(cn=' . $data -> getLabel () . ')' ;
$subbranch = $baseniveau02 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2019-06-06 16:37:34 +02:00
// Mise à jour des membres du groupes
if ( count ( $results ) > 0 ) {
$this -> writeln ( ' - ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getNiveau02DN ( $data -> getLabel ());
2019-06-06 16:37:34 +02:00
$attrs [ " memberuid " ] = array ();
$attrs [ " cadolesMember " ] = array ();
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) $attrs [ " member " ] = array ();
2019-06-06 16:37:34 +02:00
foreach ( $data -> getUsers () as $userniveau01 ) {
array_push ( $attrs [ " memberuid " ], $userniveau01 -> getUsername ());
2019-09-30 16:30:52 +02:00
array_push ( $attrs [ " cadolesMember " ], $this -> ldap -> getUserDN ( $userniveau01 ));
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) array_push ( $attrs [ " member " ], $this -> ldap -> getUserDN ( $userniveau01 ));
2019-06-06 16:37:34 +02:00
$this -> writeln ( ' > ' . $userniveau01 -> getUsername ());
}
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> ldapModify ( $dn , $attrs );
2019-06-06 16:37:34 +02:00
}
}
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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
// Mise à jour des membres du groupes
if ( count ( $results ) > 0 ) {
2019-03-21 17:15:06 +01:00
$this -> writeln ( ' - ' . $data -> getLabel ());
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getGroupDN ( $data -> getLabel ());
2018-12-18 09:44:39 +01:00
$attrs [ " memberuid " ] = array ();
$attrs [ " cadolesMember " ] = array ();
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) $attrs [ 'member' ] = array ();
2018-12-18 09:44:39 +01:00
foreach ( $data -> getUsers () as $usergroupe ) {
2020-06-18 09:57:05 +02:00
if ( ! in_array ( $usergroupe -> getUser () -> getUsername (), $attrs [ " memberuid " ])) {
array_push ( $attrs [ " memberuid " ], $usergroupe -> getUser () -> getUsername ());
array_push ( $attrs [ " cadolesMember " ], $this -> ldap -> getUserDN ( $usergroupe -> getUser ()));
2021-02-24 10:54:13 +01:00
if ( $ldap_type == " AD " ) array_push ( $attrs [ " member " ], $this -> ldap -> getUserDN ( $usergroupe -> getUser ()));
2020-06-18 09:57:05 +02:00
$this -> writeln ( ' > ' . $usergroupe -> getUser () -> getUsername ());
}
else {
$this -> writelnred ( ' > EXIST' . $usergroupe -> getUser () -> getUsername ());
}
2018-12-18 09:44:39 +01:00
}
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> ldapModify ( $dn , $attrs );
2018-12-18 09:44:39 +01:00
}
}
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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
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 " ]);
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getNiveau01DN ( $result [ " cn " ]);
if ( ! $simulate ) $this -> ldap -> deleteByDN ( $dn );
2018-12-18 09:44:39 +01:00
}
}
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 ;
2019-09-30 16:30:52 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
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 " ]);
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getNiveau02DN ( $result [ " cn " ]);
if ( ! $simulate ) $this -> ldap -> deleteByDN ( $dn );
2018-12-18 09:44:39 +01:00
}
}
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 ;
2019-10-14 13:30:50 +02:00
$results = $this -> ldap -> search ( $criteria , array ( 'cn' ), $subbranch );
2018-12-18 09:44:39 +01:00
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 " ]);
2019-09-30 16:30:52 +02:00
$dn = $this -> ldap -> getGroupDN ( $result [ " cn " ]);
if ( ! $simulate ) $this -> ldap -> deleteByDN ( $dn );
2018-12-18 09:44:39 +01:00
}
}
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
2021-01-12 12:03:02 +01:00
$criteria = '(' . $ldap_username . '=*)' ;
2018-12-18 09:44:39 +01:00
$subbranch = $baseUser ;
2021-01-12 12:03:02 +01:00
$results = $this -> ldap -> search ( $criteria , array ( $ldap_username ), $subbranch );
2018-12-18 09:44:39 +01:00
foreach ( $results as $result ) {
2021-01-12 12:03:02 +01:00
$data = $this -> em -> getRepository ( 'CadolesCoreBundle:User' ) -> findBy ( array ( 'username' => $result [ $ldap_username ]));
if ( $data ) $this -> writeln ( ' - Existe dans bundle >> ' . $result [ $ldap_username ]);
2018-12-18 09:44:39 +01:00
else {
2021-01-12 12:03:02 +01:00
$this -> writeln ( ' - A supprimer dans annuaire >> ' . $result [ $ldap_username ]);
$dn = $ldap_username . '=' . $result [ $ldap_username ] . ',' . $baseUser ;
2019-09-30 16:30:52 +02:00
if ( ! $simulate ) $this -> ldap -> deleteByDN ( $dn );
2018-12-18 09:44:39 +01:00
}
}
}
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-09-30 16:30:52 +02:00
$portal_activate = $this -> container -> getParameter ( 'portal_activate' );
2020-09-29 17:18:21 +02:00
$group = $this -> em -> getRepository ( 'CadolesCoreBundle:Group' ) -> findOneBy ( array ( 'fgtemplate' => false , 'label' => $label ));
if ( $group ) {
$this -> writelnred ( " - " . $label . " existe déjà comme groupe interne à Ninegate " );
return 0 ;
}
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-09-30 16:30:52 +02:00
$group -> setFgcancreatepage ( false );
$group -> setFgcancreateblog ( false );
2020-04-14 08:56:42 +02:00
$group -> setFgcancreatecalendar ( false );
$group -> setFgcancreateproject ( false );
2019-03-12 14:50:57 +01:00
}
2020-07-15 14:38:16 +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-09-30 16:30:52 +02:00
if ( $portal_activate ) {
$pagetemplate = $this -> em -> getRepository ( " CadolesPortalBundle:Page " ) -> find ( - 110 );
if ( $pagetemplate ) $group -> setPagetemplate ( $pagetemplate );
}
2019-03-12 14:50:57 +01:00
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $group );
2019-09-30 16:30:52 +02:00
$this -> em -> flush ();
if ( $fgcanshare && $portal_activate ) {
// On regarde s'il a au moins une page
if ( $group -> getPages () -> isEmpty ()) {
$page = $this -> em -> getRepository ( " CadolesPortalBundle:Page " ) -> clonePage ( null , $group -> getPagetemplate ());
$group -> addPage ( $page );
$page -> setName ( $group -> getLabel ());
$this -> em -> persist ( $page );
$this -> em -> persist ( $group );
$this -> em -> flush ();
}
// On regarde s'il a au moins un calendrier
if ( $group -> getCalendars () -> isEmpty ()) {
$calendar = new Calendar ();
$key = Uuid :: uuid4 ();
$calendar -> setName ( $group -> getLabel ());
$calendar -> setKeyvalue ( $key );
$calendar -> addGroup ( $group );
$calendar -> setColor ( $group -> getColor ());
$calendar -> setType ( 0 );
$this -> em -> persist ( $calendar );
$this -> em -> flush ();
}
// On regarde s'il a au moins un blog
if ( $group -> getBlogs () -> isEmpty ()) {
$blog = new Blog ();
$blog -> setName ( $group -> getLabel ());
$blog -> addGroup ( $group );
$this -> em -> persist ( $blog );
$this -> em -> flush ();
2020-04-14 08:56:42 +02:00
}
// On regarde s'il a au moins un project
if ( $group -> getProjects () -> isEmpty ()) {
$project = new Project ();
$project -> setName ( $group -> getLabel ());
$project -> addGroup ( $group );
$this -> em -> persist ( $project );
$this -> em -> flush ();
}
2019-09-30 16:30:52 +02:00
}
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 );
2021-03-25 15:56:36 +01:00
$member -> setRolegroup ( 0 );
2019-09-30 16:30:52 +02:00
}
// Si modèle scribe
$ldap_template = $this -> container -> getParameter ( 'ldap_template' );
2020-07-15 14:38:16 +02:00
$scribe_master = $this -> container -> getParameter ( 'scribe_master' );
if ( $ldap_template == " scribe " && $scribe_master ) {
2019-09-30 16:30:52 +02:00
$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 );
2021-03-25 15:56:36 +01:00
if ( $results ) $member -> setRolegroup ( 50 );
2019-09-30 16:30:52 +02:00
}
2019-03-12 14:50:57 +01:00
2019-09-30 16:30:52 +02: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 " );
2019-09-30 16:30:52 +02:00
else {
2019-03-12 14:50:57 +01:00
$user -> setRole ( " ROLE_USER " );
2019-09-30 16:30:52 +02:00
// 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 " );
}
}
2019-03-21 17:15:06 +01:00
$this -> em -> persist ( $user );
2019-08-23 17:01:04 +02:00
$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 );
2019-08-23 17:01:04 +02:00
$this -> em -> flush ();
2019-03-12 14:50:57 +01:00
}
2018-12-18 09:44:39 +01:00
}