This commit is contained in:
afornerot 2019-06-24 15:24:28 +02:00
parent 75d979fa09
commit dc9f198cce
1 changed files with 40 additions and 7 deletions

View File

@ -93,6 +93,7 @@ class SynchroCommand extends Command
$ldap_usersadmin = $this->container->getParameter('ldap_usersadmin');
$fieldstoread = array($ldap_username,$ldap_firstname,$ldap_lastname,$ldap_email);
$ldapusers = array();
$ldapmails = array();
if($ldap_template=="scribe") {
$this->writeln('');
@ -138,6 +139,12 @@ class SynchroCommand extends Command
$this->writeln('');
$this->writeln('== USERS ============================================');
// On stocke tout les email déjà existant
$users=$this->em->getRepository('CadolesCoreBundle:User')->findAll();
foreach($users as $user) {
array_push($ldapmails,$user->getEmail());
}
// On parcours les niveaux 01 pour connaitre les filtres ldap associé
$datas=$this->em->createQueryBuilder()->select('table')->from('CadolesCoreBundle:Niveau01','table')->where('table.ldapfilter IS NOT NULL')->getQuery()->getResult();
foreach($datas as $data) {
@ -146,27 +153,53 @@ class SynchroCommand extends Command
// Pour chaque utilisateur ldap
foreach($results as $result) {
// 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] = "";
$result[$ldap_email]=strtolower($result[$ldap_email]);
$result[$ldap_email]=utf8_encode($result[$ldap_email]);
// On sauvegarde ce user
if(in_array($result[$ldap_username],$ldapusers)) {
$this->writelnred(" - Création dans Bundle impossible >> ".$result[$ldap_username]." deux users avec le meme uid");
continue;
}
array_push($ldapusers,$result[$ldap_username]);
// Création ou Modification du user
$user=$this->em->getRepository('CadolesCoreBundle:User')->findBy(array('username' => $result[$ldap_username]));
$user=$this->em->getRepository('CadolesCoreBundle:User')->findOneBy(array('username' => $result[$ldap_username]));
if(!$user) {
if(empty($result[$ldap_email]))
$this->writelnred(" - Création dans Bundle impossible >> ".$result[$ldap_username]." sans email");
else {
$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);
if(in_array($result[$ldap_email],$ldapmails))
$this->writelnred(" - Création dans Bundle impossible >> ".$result[$ldap_username]." un autre utilisateur a déjà ce mail = ".$result[$ldap_email]);
else {
array_push($ldapmails,$result[$ldap_email]);
$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);
}
}
}
else {
$toadmin=false;
if(in_array($result[$ldap_username],$ldap_usersadmin)&&$user->getRole()!="ROLE_ADMIN")
if(in_array($result[$ldap_username],$ldap_usersadmin)&&!in_array("ROLE_ADMIN",$user->getRoles()))
$toadmin=true;
if($user->getLastname()!=$result[$ldap_username]||$user->getFirstname()!=$result[$ldap_firstname]||$user->getEmail()!=$result[$ldap_email]||$toadmin) {
$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);
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]));
if($usermail!=$user) {
$this->writelnred(" - Modification dans Bundle impossible >> ".$result[$ldap_username]." un autre utilisateur a déjà ce mail = ".$result[$ldap_email]);
}
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);
}
}
}
}