fix(morelevel): ajout de niveau03 et niveau04
All checks were successful
Cadoles/nineskeletor/pipeline/head This commit looks good

This commit is contained in:
2022-09-27 11:52:49 +02:00
parent 89cb0433c0
commit d1431bcce4
46 changed files with 7163 additions and 3839 deletions

View File

@ -5,6 +5,8 @@ namespace App\Service;
use App\Entity\Group;
use App\Entity\Niveau01;
use App\Entity\Niveau02;
use App\Entity\Niveau03;
use App\Entity\Niveau04;
use App\Entity\User;
use App\Entity\UserGroup;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -23,6 +25,8 @@ class LdapService
private $baseorganisation;
private $baseniveau01;
private $baseniveau02;
private $baseniveau03;
private $baseniveau04;
private $basegroup;
private $baseuser;
private $username;
@ -55,6 +59,8 @@ class LdapService
$this->baseorganisation = $container->getParameter('ldapBaseorganisation');
$this->baseniveau01 = $container->getParameter('ldapBaseniveau01');
$this->baseniveau02 = $container->getParameter('ldapBaseniveau02');
$this->baseniveau03 = $container->getParameter('ldapBaseniveau03');
$this->baseniveau04 = $container->getParameter('ldapBaseniveau04');
$this->basegroup = $container->getParameter('ldapBasegroup');
$this->baseuser = $container->getParameter('ldapBaseuser');
$this->username = $container->getParameter('ldapUsername');
@ -75,7 +81,7 @@ class LdapService
public function isNine2Ldap()
{
return 'SQL' == $this->appMasteridentity && 'NINE2LDAP' == $this->synchro && $this->userwriter && $this->baseorganisation && $this->baseniveau01 && $this->baseniveau02 && $this->basegroup && $this->baseuser && $this->connect();
return ('SQL' == $this->appMasteridentity) && 'NINE2LDAP' == $this->synchro && $this->userwriter && $this->baseorganisation && $this->baseniveau01 && $this->baseniveau02 && $this->baseniveau03 && $this->baseniveau04 && $this->basegroup && $this->baseuser && $this->connect();
}
public function connect()
@ -91,7 +97,6 @@ class LdapService
return $this->connection;
} else {
$ldapConn = ldap_connect($this->host, $this->port);
if ($ldapConn) {
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0);
@ -146,6 +151,10 @@ class LdapService
break;
case 'baseniveau02': return $this->baseniveau02;
break;
case 'baseniveau03': return $this->baseniveau03;
break;
case 'baseniveau04': return $this->baseniveau04;
break;
case 'basedn': return $this->basedn;
break;
case 'filteruser': return $this->filteruser;
@ -279,6 +288,16 @@ class LdapService
$this->addOrganisation($this->baseniveau02);
}
$ldapentrys = $this->searchdn($this->baseniveau03, $this->baseorganisation);
if (empty($ldapentrys)) {
$this->addOrganisation($this->baseniveau03);
}
$ldapentrys = $this->searchdn($this->baseniveau04, $this->baseorganisation);
if (empty($ldapentrys)) {
$this->addOrganisation($this->baseniveau04);
}
$ldapentrys = $this->searchdn($this->basegroup, $this->baseorganisation);
if (empty($ldapentrys)) {
$this->addOrganisation($this->basegroup);
@ -482,6 +501,78 @@ class LdapService
}
}
// NIVEAU03
// On recherche le Niveau03 actuellement asscocié à l'utilisateur
$criteria = '(&(cn=*)(memberUid='.$user->getUsername().'))';
$subbranch = $this->baseniveau03;
$results = $this->search($criteria, ['cn'], $subbranch);
foreach ($results as $result) {
// Si Niveau03 différent de celui en cours on le détache de ce Niveau03
if (null === $user->getNiveau03() || $result['cn'] != $user->getNiveau03()->getLabel() || $todel) {
$dn = $this->getNiveau03DN($result['cn']);
$entry['memberuid'] = $user->getUsername();
$result = ldap_mod_del($connection, $dn, $entry);
if (!$result) {
$this->ldapError();
}
}
}
// On recherche le Niveau03 en cours
if (!$todel) {
if (null !== $user->getNiveau03()) {
$criteria = '(cn='.$user->getNiveau03()->getLabel().')';
$subbranch = $this->baseniveau03;
$result = $this->search($criteria, ['memberuid'], $subbranch);
// S'il n'est pas membre du Niveau03 on le rattache
if (empty($result) || !$this->in_array_r($user->getUsername(), $result[0])) {
$dn = $this->getNiveau03DN($user->getNiveau03()->getLabel());
$entry['memberuid'] = $user->getUsername();
$result = ldap_mod_add($connection, $dn, $entry);
if (!$result) {
$this->ldapError();
}
}
}
}
// NIVEAU04
// On recherche le Niveau04 actuellement asscocié à l'utilisateur
$criteria = '(&(cn=*)(memberUid='.$user->getUsername().'))';
$subbranch = $this->baseniveau04;
$results = $this->search($criteria, ['cn'], $subbranch);
foreach ($results as $result) {
// Si Niveau04 différent de celui en cours on le détache de ce Niveau04
if (null === $user->getNiveau04() || $result['cn'] != $user->getNiveau04()->getLabel() || $todel) {
$dn = $this->getNiveau04DN($result['cn']);
$entry['memberuid'] = $user->getUsername();
$result = ldap_mod_del($connection, $dn, $entry);
if (!$result) {
$this->ldapError();
}
}
}
// On recherche le Niveau04 en cours
if (!$todel) {
if (null !== $user->getNiveau04()) {
$criteria = '(cn='.$user->getNiveau04()->getLabel().')';
$subbranch = $this->baseniveau04;
$result = $this->search($criteria, ['memberuid'], $subbranch);
// S'il n'est pas membre du Niveau04 on le rattache
if (empty($result) || !$this->in_array_r($user->getUsername(), $result[0])) {
$dn = $this->getNiveau04DN($user->getNiveau04()->getLabel());
$entry['memberuid'] = $user->getUsername();
$result = ldap_mod_add($connection, $dn, $entry);
if (!$result) {
$this->ldapError();
}
}
}
}
return $result;
}
@ -816,6 +907,262 @@ class LdapService
return 'cn='.$id.','.$this->baseniveau02;
}
// ==================================================================================================================================================================
// == Function Niveau03==============================================================================================================================================
// ==================================================================================================================================================================
public function addNiveau03(Niveau03 $niveau03)
{
$connection = $this->connect();
$dn = $this->getNiveau03DN($niveau03->getLabel());
$attrs = [];
$attrs['objectclass'] = $this->getObjectClassesNiveau03();
$this->fillAttributesNiveau03($niveau03, $attrs);
foreach ($attrs as $key => $value) {
if (empty($value)) {
unset($attrs[$key]);
}
}
$result = ldap_add($connection, $dn, $attrs);
if (!$result) {
$this->ldapError();
}
return $result;
}
public function ismodifyNiveau03(Niveau03 $niveau03, $entry)
{
$attrs = [];
$this->fillAttributesNiveau03($niveau03, $attrs);
foreach ($attrs as $key => $value) {
if (!array_key_exists($key, $entry) && !empty($value)) {
return true;
} elseif (array_key_exists($key, $entry) && $value != $entry[$key]) {
return true;
}
}
foreach ($entry as $key => $value) {
if (!array_key_exists($key, $attrs) && !empty($value)) {
return true;
} elseif (array_key_exists($key, $attrs) && $value != $attrs[$key]) {
return true;
}
}
return false;
}
public function modifyNiveau03(Niveau03 $niveau03, $oldid)
{
$dn = $this->basedn;
$connection = $this->connect();
$attrs = [];
$this->fillAttributesNiveau03($niveau03, $attrs);
unset($attrs['cn']);
$dn = $this->getNiveau03DN($niveau03->getLabel());
foreach ($attrs as $key => $value) {
if (empty($value)) {
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
@ldap_mod_del($connection, $dn, [$key => []]);
unset($attrs[$key]);
}
}
if (isset($oldid) && $oldid != $niveau03->getLabel()) {
$olddn = $this->getNiveau03DN($oldid);
$this->rename($olddn, 'cn='.$niveau03->getLabel(), $this->baseniveau03);
}
$result = ldap_modify($connection, $dn, $attrs);
if (!$result) {
$this->ldapError();
}
}
public function deleteNiveau03(Niveau03 $niveau03)
{
$dn = $this->getNiveau03DN($niveau03->getLabel());
return $this->deleteByDN($dn);
}
private function getObjectClassesNiveau03()
{
$oc = [
'top',
'posixGroup',
];
return $oc;
}
public function listAttributesNiveau03()
{
return [
'cn',
'gidnumber',
'memberuid',
];
}
public function fillAttributesNiveau03(Niveau03 $niveau03, array &$attrs)
{
$attrs['cn'] = $niveau03->getLabel();
$attrs['gidnumber'] = $niveau03->getId();
$attrs['memberuid'] = [];
foreach ($niveau03->getUsers() as $user) {
array_push($attrs['memberuid'], $user->getUsername());
}
sort($attrs['memberuid']);
if (1 == count($attrs['memberuid'])) {
$attrs['memberuid'] = $attrs['memberuid'][0];
}
}
public function getNiveau03DN($id)
{
return 'cn='.$id.','.$this->baseniveau03;
}
// ==================================================================================================================================================================
// == Function Niveau04==============================================================================================================================================
// ==================================================================================================================================================================
public function addNiveau04(Niveau04 $niveau04)
{
$connection = $this->connect();
$dn = $this->getNiveau04DN($niveau04->getLabel());
$attrs = [];
$attrs['objectclass'] = $this->getObjectClassesNiveau04();
$this->fillAttributesNiveau04($niveau04, $attrs);
foreach ($attrs as $key => $value) {
if (empty($value)) {
unset($attrs[$key]);
}
}
$result = ldap_add($connection, $dn, $attrs);
if (!$result) {
$this->ldapError();
}
return $result;
}
public function ismodifyNiveau04(Niveau04 $niveau04, $entry)
{
$attrs = [];
$this->fillAttributesNiveau04($niveau04, $attrs);
foreach ($attrs as $key => $value) {
if (!array_key_exists($key, $entry) && !empty($value)) {
return true;
} elseif (array_key_exists($key, $entry) && $value != $entry[$key]) {
return true;
}
}
foreach ($entry as $key => $value) {
if (!array_key_exists($key, $attrs) && !empty($value)) {
return true;
} elseif (array_key_exists($key, $attrs) && $value != $attrs[$key]) {
return true;
}
}
return false;
}
public function modifyNiveau04(Niveau04 $niveau04, $oldid)
{
$dn = $this->basedn;
$connection = $this->connect();
$attrs = [];
$this->fillAttributesNiveau04($niveau04, $attrs);
unset($attrs['cn']);
$dn = $this->getNiveau04DN($niveau04->getLabel());
foreach ($attrs as $key => $value) {
if (empty($value)) {
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
@ldap_mod_del($connection, $dn, [$key => []]);
unset($attrs[$key]);
}
}
if (isset($oldid) && $oldid != $niveau04->getLabel()) {
$olddn = $this->getNiveau04DN($oldid);
$this->rename($olddn, 'cn='.$niveau04->getLabel(), $this->baseniveau04);
}
$result = ldap_modify($connection, $dn, $attrs);
if (!$result) {
$this->ldapError();
}
}
public function deleteNiveau04(Niveau04 $niveau04)
{
$dn = $this->getNiveau04DN($niveau04->getLabel());
return $this->deleteByDN($dn);
}
private function getObjectClassesNiveau04()
{
$oc = [
'top',
'posixGroup',
];
return $oc;
}
public function listAttributesNiveau04()
{
return [
'cn',
'gidnumber',
'memberuid',
];
}
public function fillAttributesNiveau04(Niveau04 $niveau04, array &$attrs)
{
$attrs['cn'] = $niveau04->getLabel();
$attrs['gidnumber'] = $niveau04->getId();
$attrs['memberuid'] = [];
foreach ($niveau04->getUsers() as $user) {
array_push($attrs['memberuid'], $user->getUsername());
}
sort($attrs['memberuid']);
if (1 == count($attrs['memberuid'])) {
$attrs['memberuid'] = $attrs['memberuid'][0];
}
}
public function getNiveau04DN($id)
{
return 'cn='.$id.','.$this->baseniveau04;
}
// ==================================================================================================================================================================
// == Function Group=================================================================================================================================================
// ==================================================================================================================================================================