Merge branch 'master' into dist/envole/6/master

This commit is contained in:
2021-01-08 14:55:28 +01:00
6 changed files with 124 additions and 66 deletions

View File

@ -36,6 +36,8 @@ class SynchroCommand extends Command
private $rootlog;
private $ldap;
private $ldap_basedn;
private $ldap_baseuser;
private $ldap_basegroup;
protected function configure()
{
@ -94,18 +96,23 @@ class SynchroCommand extends Command
$this->writeln('=====================================================');
$this->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');
$scribe_group = $this->container->getParameter('scribe_group');
$scribe_master = $this->container->getParameter('scribe_master');
$fieldstoread = array($ldap_username,$ldap_firstname,$ldap_lastname,$ldap_email);
$ldapusers = array();
$ldapmails = array();
$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'));
$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();
if($ldap_template=="scribe") {
$this->writeln('');
@ -179,7 +186,7 @@ class SynchroCommand extends Command
$this->writeln('');
$this->writeln('== GROUPES ==========================================');
$results = $this->ldap->search($this->container->getParameter('openldapreqgroup'), ['cn','description','gidNumber'], $this->ldap_basedn);
$results = $this->ldap->search($this->container->getParameter('openldapreqgroup'), ['cn','description','gidNumber'], $this->ldap_basegroup);
foreach($results as $result) {
$cn=$result["cn"];
$ldapfilter="(&".$this->container->getParameter('openldapreqgroup')."(cn=$cn))";
@ -209,7 +216,7 @@ class SynchroCommand extends Command
// On execute le filtre d'appartenance à ce niveau
$this->writeln("== Récupération des utilisateurs de l'annuaire");
$niveau01=$this->em->getRepository('CadolesCoreBundle:Niveau01')->find($data["id"]);
$results = $this->ldap->search($niveau01->getLdapfilter(), $fieldstoread, $this->ldap_basedn);
$results = $this->ldap->search($niveau01->getLdapfilter(), $fieldstoread, $this->ldap_baseuser);
$nbuserstotal=count($results);
// Pour chaque utilisateur ldap
@ -229,6 +236,7 @@ class SynchroCommand extends Command
$result[$ldap_username]=utf8_encode($result[$ldap_username]);
if(!isset($result[$ldap_lastname])) $result[$ldap_lastname] = "";
if(!isset($result[$ldap_firstname])) $result[$ldap_firstname] = "";
if(!array_key_exists($ldap_email,$result)) {
$this->writelnred(" - Création dans Bundle impossible >> ".$result[$ldap_username]." sans email");
continue;
@ -306,18 +314,29 @@ class SynchroCommand extends Command
$this->writeln('== '.$group->getLabel());
if(!is_null($ldapfilter)) {
$results = $this->ldap->search($ldapfilter,[$ldap_username,"memberuid"] , $this->ldap_basedn);
$results = $this->ldap->search($ldapfilter,[$ldap_username,$ldap_member] , $this->ldap_basedn);
foreach($results as $result) {
if(isset($result["memberuid"])) {
if(isset($result[$ldap_member])) {
// Si memberid est un tableau il y a plusieur user dedans
if(is_array($result["memberuid"])) {
foreach($result["memberuid"] as $key => $value) {
if(is_array($result[$ldap_member])) {
foreach($result[$ldap_member] as $key => $value) {
if(is_int($key)) {
$user=$this->em->getRepository('CadolesCoreBundle:User')->findOneBy(array('username' => $value));
$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));
if($user) {
array_push($ldapusersgroup,$value);
$this->writeln(" - Rattacher >> ".$value);
array_push($ldapusersgroup,$username);
$this->writeln(" - Rattacher >> ".$username);
if(!$simulate) $this->addtoGroup($user,$group);
}
}
@ -325,10 +344,19 @@ class SynchroCommand extends Command
}
// sinon m'a qu'un seul uid
else {
$user=$this->em->getRepository('CadolesCoreBundle:User')->findOneBy(array('username' => $result["memberuid"]));
$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));
if($user) {
array_push($ldapusersgroup,$result["memberuid"]);
$this->writeln(" - Rattacher >> ".$result["memberuid"]);
array_push($ldapusersgroup,$username);
$this->writeln(" - Rattacher >> ".$username);
if(!$simulate) $this->addtoGroup($user,$group);
}
}

View File

@ -106,6 +106,7 @@ services:
arguments:
- %ldap_host%
- %ldap_port%
- %ldap_tls%
calls:
- [setUser, ["%ldap_user%"]]
- [setPassword, ["%ldap_password%"]]

View File

@ -13,6 +13,7 @@ class ldapService
protected $host;
protected $port;
protected $tls;
protected $baseDN;
protected $baseUser;
protected $baseNiveau01;
@ -24,10 +25,11 @@ class ldapService
private $connection = null;
private $ldapSync = false;
public function __construct($host, $port)
public function __construct($host, $port, $tls)
{
$this->host = $host;
$this->port = $port;
$this->tls = $tls;
}
public function isEnabled() {
@ -39,8 +41,11 @@ class ldapService
return $this->connection;
} else {
$ldapConn = ldap_connect($this->host, $this->port);
if($ldapConn){
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
if($this->tls) ldap_start_tls($ldapConn);
if(ldap_bind( $ldapConn, $this->user, $this->password)){
$this->connection = $ldapConn;
return $this->connection;