2022-07-21 16:15:47 +02:00
< ? php
namespace App\Service ;
2022-09-23 16:14:15 +02:00
use App\Entity\Group ;
2022-07-21 16:15:47 +02:00
use App\Entity\Niveau01 ;
use App\Entity\Niveau02 ;
2022-09-27 11:52:49 +02:00
use App\Entity\Niveau03 ;
use App\Entity\Niveau04 ;
2022-09-23 16:14:15 +02:00
use App\Entity\User ;
2022-07-21 16:15:47 +02:00
use App\Entity\UserGroup ;
2022-09-23 16:14:15 +02:00
use Symfony\Component\DependencyInjection\ContainerInterface ;
2022-07-21 16:15:47 +02:00
class LdapService
{
2022-09-23 14:53:47 +02:00
private $appMasteridentity ;
private $synchro ;
private $host ;
private $port ;
private $usetls ;
private $userwriter ;
private $user ;
private $password ;
private $basedn ;
private $baseorganisation ;
private $baseniveau01 ;
private $baseniveau02 ;
2022-09-27 11:52:49 +02:00
private $baseniveau03 ;
private $baseniveau04 ;
2022-09-23 14:53:47 +02:00
private $basegroup ;
private $baseuser ;
private $username ;
private $firstname ;
private $lastname ;
private $email ;
private $avatar ;
private $memberof ;
private $groupgid ;
private $groupname ;
private $groupmember ;
private $groupmemberisdn ;
private $filtergroup ;
private $filteruser ;
private $userattributes ;
2022-07-21 16:15:47 +02:00
private $connection ;
public function __construct ( ContainerInterface $container )
{
2022-09-23 16:14:15 +02:00
$this -> appMasteridentity = $container -> getParameter ( 'appMasteridentity' );
$this -> synchro = $container -> getParameter ( 'appSynchro' );
$this -> host = $container -> getParameter ( 'ldapHost' );
$this -> port = $container -> getParameter ( 'ldapPort' );
$this -> usetls = $container -> getParameter ( 'ldapUsetls' );
$this -> userwriter = $container -> getParameter ( 'ldapUserwriter' );
$this -> user = $container -> getParameter ( 'ldapUser' );
$this -> password = $container -> getParameter ( 'ldapPassword' );
$this -> basedn = $container -> getParameter ( 'ldapBasedn' );
$this -> baseorganisation = $container -> getParameter ( 'ldapBaseorganisation' );
$this -> baseniveau01 = $container -> getParameter ( 'ldapBaseniveau01' );
$this -> baseniveau02 = $container -> getParameter ( 'ldapBaseniveau02' );
2022-09-27 11:52:49 +02:00
$this -> baseniveau03 = $container -> getParameter ( 'ldapBaseniveau03' );
$this -> baseniveau04 = $container -> getParameter ( 'ldapBaseniveau04' );
2022-09-23 16:14:15 +02:00
$this -> basegroup = $container -> getParameter ( 'ldapBasegroup' );
$this -> baseuser = $container -> getParameter ( 'ldapBaseuser' );
$this -> username = $container -> getParameter ( 'ldapUsername' );
$this -> firstname = $container -> getParameter ( 'ldapFirstname' );
$this -> lastname = $container -> getParameter ( 'ldapLastname' );
$this -> email = $container -> getParameter ( 'ldapEmail' );
$this -> avatar = $container -> getParameter ( 'ldapAvatar' );
$this -> memberof = $container -> getParameter ( 'ldapMemberof' );
$this -> groupgid = $container -> getParameter ( 'ldapGroupgid' );
$this -> groupname = $container -> getParameter ( 'ldapGroupname' );
$this -> groupmember = $container -> getParameter ( 'ldapGroupmember' );
$this -> groupmemberisdn = $container -> getParameter ( 'ldapGroupmemberisdn' );
$this -> filtergroup = $container -> getParameter ( 'ldapFiltergroup' );
$this -> filteruser = $container -> getParameter ( 'ldapFilteruser' );
$this -> userattributes = [ $this -> username , $this -> firstname , $this -> lastname , $this -> email , $this -> avatar , $this -> memberof ];
}
public function isNine2Ldap ()
{
2022-09-27 11:52:49 +02:00
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 ();
2022-09-23 16:14:15 +02:00
}
public function connect ()
{
2022-07-21 16:15:47 +02:00
// Si on est déjà co = on rebind pour gérer le cas d'un timeout de connection
2022-09-23 16:14:15 +02:00
if ( $this -> connection ) {
if ( !@ ldap_bind ( $this -> connection , $this -> user , $this -> password )) {
2022-07-21 16:15:47 +02:00
$this -> disconnect ();
}
}
2022-09-23 16:14:15 +02:00
if ( $this -> connection ) {
2022-07-21 16:15:47 +02:00
return $this -> connection ;
} else {
$ldapConn = ldap_connect ( $this -> host , $this -> port );
2022-09-23 16:14:15 +02:00
if ( $ldapConn ) {
2022-07-21 16:15:47 +02:00
ldap_set_option ( $ldapConn , LDAP_OPT_PROTOCOL_VERSION , 3 );
ldap_set_option ( $ldapConn , LDAP_OPT_REFERRALS , 0 );
2022-09-23 16:14:15 +02:00
if ( $this -> usetls ) {
ldap_start_tls ( $ldapConn );
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
if ( @ ldap_bind ( $ldapConn , $this -> user , $this -> password )) {
2022-07-21 16:15:47 +02:00
$this -> connection = $ldapConn ;
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> connection ;
}
}
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return false ;
}
2022-09-23 16:14:15 +02:00
public function userconnect ( $username , $userpassword )
{
2022-07-21 16:15:47 +02:00
$ldapConn = ldap_connect ( $this -> host , $this -> port );
$this -> connection = $ldapConn ;
2022-09-23 16:14:15 +02:00
if ( $this -> connection ) {
2022-07-21 16:15:47 +02:00
ldap_set_option ( $ldapConn , LDAP_OPT_PROTOCOL_VERSION , 3 );
ldap_set_option ( $ldapConn , LDAP_OPT_REFERRALS , 0 );
2022-09-23 16:14:15 +02:00
if ( $this -> usetls ) {
ldap_start_tls ( $ldapConn );
}
2022-07-21 16:15:47 +02:00
$dn = $this -> getUserDN ( $username );
2022-09-23 16:14:15 +02:00
if ( @ ldap_bind ( $ldapConn , $dn , $userpassword )) {
$res = $this -> search ( str_replace ( '*' , $username , $this -> filteruser ), $this -> userattributes , $this -> baseuser );
$this -> disconnect ();
2022-07-21 16:15:47 +02:00
return $res ;
}
}
$this -> disconnect ();
2022-09-23 16:14:15 +02:00
return false ;
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function getParameter ( $key )
{
switch ( $key ) {
case 'baseuser' : return $this -> baseuser ;
break ;
case 'basegroup' : return $this -> basegroup ;
break ;
case 'baseniveau01' : return $this -> baseniveau01 ;
break ;
case 'baseniveau02' : return $this -> baseniveau02 ;
break ;
2022-09-27 11:52:49 +02:00
case 'baseniveau03' : return $this -> baseniveau03 ;
break ;
case 'baseniveau04' : return $this -> baseniveau04 ;
break ;
2022-09-23 16:14:15 +02:00
case 'basedn' : return $this -> basedn ;
break ;
2022-10-04 15:58:21 +02:00
case 'baseorganisation' : return $this -> baseorganisation ;
break ;
2022-09-23 16:14:15 +02:00
case 'filteruser' : return $this -> filteruser ;
break ;
}
}
public function search ( $filter , $attributes = [], $subBranch = '' )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$branch = ( $subBranch ? $subBranch : $this -> basedn );
2022-09-23 16:14:15 +02:00
$result = ldap_search ( $connection , $branch , $filter , $attributes , 0 , 0 , 0 );
if ( ! $result ) {
2022-07-21 16:15:47 +02:00
$this -> ldapError ();
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> resultToArray ( $result );
}
2022-09-23 16:14:15 +02:00
public function searchdn ( $dn , $subBranch = '' )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$tbdn = ldap_explode_dn ( $dn , 0 );
2022-07-21 16:15:47 +02:00
$branch = ( $subBranch ? $subBranch : $this -> basedn );
2022-09-23 16:14:15 +02:00
$result = ldap_search ( $connection , $branch , '(' . $tbdn [ 0 ] . ')' , [], 0 , 0 , 0 );
if ( ! $result ) {
2022-07-21 16:15:47 +02:00
$this -> ldapError ();
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> resultToArray ( $result );
}
2022-10-04 15:58:21 +02:00
public function deleteByDN ( $dn , $recursive = false )
2022-09-23 16:14:15 +02:00
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
2022-10-04 15:58:21 +02:00
if ( false == $recursive ) {
$removed = ldap_delete ( $connection , $dn );
if ( ! $removed ) {
$this -> ldapError ();
}
} else {
// searching for sub entries
$sr = ldap_list ( $connection , $dn , 'ObjectClass=*' , [ '' ]);
$info = ldap_get_entries ( $connection , $sr );
for ( $i = 0 ; $i < $info [ 'count' ]; ++ $i ) {
$result = $this -> deleteByDN ( $info [ $i ][ 'dn' ], $recursive );
if ( ! $result ) {
return $result ;
}
}
return ldap_delete ( $connection , $dn );
2022-07-21 16:15:47 +02:00
}
}
2022-09-23 16:14:15 +02:00
public function rename ( $oldDN , $newDN , $parentDN = '' , $deleteOldDN = true )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$result = ldap_rename ( $connection , $oldDN , $newDN , $parentDN , $deleteOldDN );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
private function resultToArray ( $result )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$resultArray = [];
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
if ( $result ) {
2022-07-21 16:15:47 +02:00
$entry = ldap_first_entry ( $connection , $result );
2022-09-23 16:14:15 +02:00
while ( $entry ) {
$row = [];
2022-07-21 16:15:47 +02:00
$attr = ldap_first_attribute ( $connection , $entry );
2022-09-23 16:14:15 +02:00
while ( $attr ) {
2022-07-21 16:15:47 +02:00
$val = ldap_get_values_len ( $connection , $entry , $attr );
2022-09-23 16:14:15 +02:00
if ( array_key_exists ( 'count' , $val ) and 1 == $val [ 'count' ]) {
2022-07-21 16:15:47 +02:00
$row [ strtolower ( $attr )] = $val [ 0 ];
} else {
$row [ strtolower ( $attr )] = $val ;
}
2022-09-23 16:14:15 +02:00
if ( is_array ( $row [ strtolower ( $attr )])) {
unset ( $row [ strtolower ( $attr )][ 'count' ]);
2022-07-21 16:15:47 +02:00
}
$attr = ldap_next_attribute ( $connection , $entry );
}
$resultArray [] = $row ;
$entry = ldap_next_entry ( $connection , $entry );
}
}
return $resultArray ;
}
2022-09-23 16:14:15 +02:00
public function in_array_r ( $item , $array )
{
return preg_match ( '/"' . $item . '"/i' , json_encode ( $array ));
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function disconnect ()
{
if ( $this -> connection ) {
2022-07-21 16:15:47 +02:00
ldap_unbind ( $this -> connection );
2022-09-23 16:14:15 +02:00
$this -> connection = null ;
2022-07-21 16:15:47 +02:00
}
}
2022-09-23 16:14:15 +02:00
public function ldapError ()
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
throw new \Exception ( 'Error: (' . ldap_errno ( $connection ) . ') ' . ldap_error ( $connection ));
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function ldapModify ( $dn , $attrs )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function Organisation==========================================================================================================================================
// ==================================================================================================================================================================
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function addOrganisations ()
{
$ldapentrys = $this -> searchdn ( $this -> baseorganisation );
if ( empty ( $ldapentrys )) {
2022-08-22 13:13:10 +02:00
$this -> addOrganisation ( $this -> baseorganisation );
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
$ldapentrys = $this -> searchdn ( $this -> baseniveau01 , $this -> baseorganisation );
if ( empty ( $ldapentrys )) {
2022-07-21 16:15:47 +02:00
$this -> addOrganisation ( $this -> baseniveau01 );
}
2022-09-23 16:14:15 +02:00
$ldapentrys = $this -> searchdn ( $this -> baseniveau02 , $this -> baseorganisation );
if ( empty ( $ldapentrys )) {
2022-07-21 16:15:47 +02:00
$this -> addOrganisation ( $this -> baseniveau02 );
}
2022-09-27 11:52:49 +02:00
$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 );
}
2022-09-23 16:14:15 +02:00
$ldapentrys = $this -> searchdn ( $this -> basegroup , $this -> baseorganisation );
if ( empty ( $ldapentrys )) {
2022-07-21 16:15:47 +02:00
$this -> addOrganisation ( $this -> basegroup );
}
2022-09-23 16:14:15 +02:00
$ldapentrys = $this -> searchdn ( $this -> baseuser , $this -> baseorganisation );
if ( empty ( $ldapentrys )) {
2022-07-21 16:15:47 +02:00
$this -> addOrganisation ( $this -> baseuser );
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function addOrganisation ( $dn )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
$attrs [ 'objectclass' ] = [ 'top' , 'organizationalUnit' ];
2022-07-21 16:15:47 +02:00
$result = ldap_add ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function User==================================================================================================================================================
// ==================================================================================================================================================================
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function addUser ( User $user )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$dn = $this -> getUserDN ( $user -> getUsername ());
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$attrs [ 'objectclass' ] = $this -> getObjectClassesUser ();
$this -> fillAttributesUser ( $user , $attrs );
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_add ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
public function ismodifyUser ( User $user , $entry )
{
2022-07-21 16:15:47 +02:00
$attrs = [];
$this -> fillAttributesUser ( $user , $attrs );
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
return false ;
}
2022-09-23 16:14:15 +02:00
public function modifyUser ( User $user )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$this -> fillAttributesUser ( $user , $attrs );
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Rechercher le DN du user
$dn = $this -> getUserDN ( $user -> getUsername ());
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
2022-09-23 16:14:15 +02:00
@ ldap_mod_del ( $connection , $dn , [ $key => []]);
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function modifyUserpwd ( User $user )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
// Attributs associés au password
$attrs [ 'userpassword' ] = $user -> getPassword ();
// Rechercher le DN du user
$dn = $this -> getUserDN ( $user -> getUsername ());
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
2022-09-23 16:14:15 +02:00
@ ldap_mod_del ( $connection , $dn , [ $key => []]);
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function updateNiveauUser ( User $user , $todel = false )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
2022-09-23 16:14:15 +02:00
$connection = $this -> connect ();
$result = null ;
2022-10-02 14:50:48 +02:00
if ( ! $user -> isIsactive ()) {
$todel = true ;
}
2022-07-21 16:15:47 +02:00
// NIVEAU01
// On recherche le Niveau01 actuellement asscocié à l'utilisateur
$criteria = '(&(cn=*)(memberUid=' . $user -> getUsername () . '))' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> baseniveau01 ;
$results = $this -> search ( $criteria , [ 'cn' ], $subbranch );
foreach ( $results as $result ) {
2022-07-21 16:15:47 +02:00
// Si Niveau01 différent de celui en cours on le détache de ce Niveau01
2022-09-23 16:14:15 +02:00
if ( $result [ 'cn' ] != $user -> getNiveau01 () -> getLabel () || $todel ) {
$dn = $this -> getNiveau01DN ( $result [ 'cn' ]);
$entry [ 'memberuid' ] = $user -> getUsername ();
$result = ldap_mod_del ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
}
// On recherche le Niveau01 en cours
2022-09-23 16:14:15 +02:00
if ( ! $todel ) {
2022-07-21 16:15:47 +02:00
$criteria = '(cn=' . $user -> getNiveau01 () -> getLabel () . ')' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> baseniveau01 ;
$result = $this -> search ( $criteria , [ 'memberuid' ], $subbranch );
2022-07-21 16:15:47 +02:00
// S'il n'est pas membre du Niveau01 on le rattache
2022-09-23 16:14:15 +02:00
if ( ! $this -> in_array_r ( $user -> getUsername (), $result [ 0 ])) {
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau01DN ( $user -> getNiveau01 () -> getLabel ());
2022-09-23 16:14:15 +02:00
$entry [ 'memberuid' ] = $user -> getUsername ();
$result = ldap_mod_add ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
}
// NIVEAU02
// On recherche le Niveau02 actuellement asscocié à l'utilisateur
$criteria = '(&(cn=*)(memberUid=' . $user -> getUsername () . '))' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> baseniveau02 ;
$results = $this -> search ( $criteria , [ 'cn' ], $subbranch );
foreach ( $results as $result ) {
2022-07-21 16:15:47 +02:00
// Si Niveau02 différent de celui en cours on le détache de ce Niveau02
2022-09-23 16:14:15 +02:00
if ( null === $user -> getNiveau02 () || $result [ 'cn' ] != $user -> getNiveau02 () -> getLabel () || $todel ) {
$dn = $this -> getNiveau02DN ( $result [ 'cn' ]);
$entry [ 'memberuid' ] = $user -> getUsername ();
$result = ldap_mod_del ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
}
// On recherche le Niveau02 en cours
2022-09-23 16:14:15 +02:00
if ( ! $todel ) {
if ( null !== $user -> getNiveau02 ()) {
2022-07-21 16:15:47 +02:00
$criteria = '(cn=' . $user -> getNiveau02 () -> getLabel () . ')' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> baseniveau02 ;
$result = $this -> search ( $criteria , [ 'memberuid' ], $subbranch );
2022-07-21 16:15:47 +02:00
// S'il n'est pas membre du Niveau02 on le rattache
2022-09-23 16:14:15 +02:00
if ( empty ( $result ) || ! $this -> in_array_r ( $user -> getUsername (), $result [ 0 ])) {
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau02DN ( $user -> getNiveau02 () -> getLabel ());
2022-09-23 16:14:15 +02:00
$entry [ 'memberuid' ] = $user -> getUsername ();
$result = ldap_mod_add ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
}
}
2022-09-27 11:52:49 +02:00
// 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 ();
}
}
}
}
2022-09-23 16:14:15 +02:00
return $result ;
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function deleteUser ( User $user )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> getUserDN ( $user -> getUsername ());
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> deleteByDN ( $dn );
}
2022-09-23 16:14:15 +02:00
public function getObjectClassesUser ()
{
$oc = [
2022-07-21 16:15:47 +02:00
'top' ,
'person' ,
'organizationalPerson' ,
'inetOrgPerson' ,
2022-09-23 16:14:15 +02:00
];
2022-07-21 16:15:47 +02:00
return $oc ;
}
2022-09-23 16:14:15 +02:00
public function listAttributesUser ()
{
2022-07-21 16:15:47 +02:00
return [
2022-09-23 16:14:15 +02:00
'uid' ,
'cn' ,
'givenname' ,
'sn' ,
'mail' ,
'displayname' ,
'telephonenumber' ,
'postaladdress' ,
'userpassword' ,
2022-07-21 16:15:47 +02:00
];
}
2022-09-23 16:14:15 +02:00
public function fillAttributesUser ( User $user , array & $attrs )
{
$attrs [ 'uid' ] = $user -> getUsername ();
$attrs [ 'cn' ] = $user -> getFirstname () . ' ' . $user -> getLastname ();
$attrs [ 'givenname' ] = $user -> getFirstname ();
$attrs [ 'sn' ] = $user -> getLastname ();
$attrs [ 'mail' ] = $user -> getEmail ();
$attrs [ 'displayname' ] = $user -> getFirstname () . ' ' . $user -> getLastname ();
$attrs [ 'telephonenumber' ] = $user -> getTelephonenumber ();
$attrs [ 'postaladdress' ] = $user -> getPostaladress ();
$attrs [ 'userpassword' ] = $user -> getPassword ();
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function getUserDN ( $username )
{
2022-07-21 16:15:47 +02:00
return $this -> username . '=' . $username . ',' . $this -> baseuser ;
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function Niveau01==============================================================================================================================================
// ==================================================================================================================================================================
public function findNiveau01 ( $ldapfilter )
{
$ldapentrys = $this -> search ( $ldapfilter , [ $this -> groupgid , $this -> groupname , $this -> groupmember ], $this -> baseniveau01 );
2022-07-21 16:15:47 +02:00
return $ldapentrys ;
}
2022-09-23 16:14:15 +02:00
public function findNiveau01ismember ( $ldapfilter , $username )
{
$ldapentrys = $this -> findNiveau01 ( $ldapfilter );
foreach ( $ldapentrys as $ldapentry ) {
if ( is_array ( $ldapentry [ $this -> groupmember ])) {
if ( in_array ( $username , $ldapentry [ $this -> groupmember ])) {
return true ;
}
} elseif ( $username == $ldapentry [ $this -> groupmember ]) {
return true ;
2022-07-21 16:15:47 +02:00
}
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return false ;
}
2022-09-23 16:14:15 +02:00
public function addNiveau01 ( Niveau01 $niveau01 )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$dn = $this -> getNiveau01DN ( $niveau01 -> getLabel ());
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$attrs [ 'objectclass' ] = $this -> getObjectClassesNiveau01 ();
$this -> fillAttributesNiveau01 ( $niveau01 , $attrs );
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_add ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
public function ismodifyNiveau01 ( Niveau01 $niveau01 , $entry )
{
2022-07-21 16:15:47 +02:00
$attrs = [];
$this -> fillAttributesNiveau01 ( $niveau01 , $attrs );
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
return false ;
}
2022-09-23 16:14:15 +02:00
public function modifyNiveau01 ( Niveau01 $niveau01 , $oldid )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$this -> fillAttributesNiveau01 ( $niveau01 , $attrs );
2022-09-23 16:14:15 +02:00
unset ( $attrs [ 'cn' ]);
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau01DN ( $niveau01 -> getLabel ());
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
2022-09-23 16:14:15 +02:00
@ ldap_mod_del ( $connection , $dn , [ $key => []]);
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
2022-09-23 16:14:15 +02:00
if ( isset ( $oldid ) && $oldid != $niveau01 -> getLabel ()) {
2022-07-21 16:15:47 +02:00
$olddn = $this -> getNiveau01DN ( $oldid );
2022-09-23 16:14:15 +02:00
$this -> rename ( $olddn , 'cn=' . $niveau01 -> getLabel (), $this -> baseniveau01 );
2022-07-21 16:15:47 +02:00
}
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function deleteNiveau01 ( Niveau01 $niveau01 )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau01DN ( $niveau01 -> getLabel ());
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> deleteByDN ( $dn );
}
2022-09-23 16:14:15 +02:00
private function getObjectClassesNiveau01 ()
{
$oc = [
2022-07-21 16:15:47 +02:00
'top' ,
'posixGroup' ,
2022-09-23 16:14:15 +02:00
];
2022-07-21 16:15:47 +02:00
return $oc ;
}
2022-09-23 16:14:15 +02:00
public function listAttributesNiveau01 ()
{
2022-07-21 16:15:47 +02:00
return [
2022-09-23 16:14:15 +02:00
'cn' ,
'gidnumber' ,
'memberuid' ,
2022-07-21 16:15:47 +02:00
];
}
2022-09-23 16:14:15 +02:00
public function fillAttributesNiveau01 ( Niveau01 $niveau01 , array & $attrs )
{
$attrs [ 'cn' ] = $niveau01 -> getLabel ();
2022-07-21 16:15:47 +02:00
$attrs [ 'gidnumber' ] = $niveau01 -> getId ();
$attrs [ 'memberuid' ] = [];
2022-09-23 16:14:15 +02:00
foreach ( $niveau01 -> getUsers () as $user ) {
2022-10-02 14:50:48 +02:00
if ( $user -> isIsactive ()) {
array_push ( $attrs [ 'memberuid' ], $user -> getUsername ());
}
2022-07-21 16:15:47 +02:00
}
sort ( $attrs [ 'memberuid' ]);
2022-09-23 16:14:15 +02:00
if ( 1 == count ( $attrs [ 'memberuid' ])) {
$attrs [ 'memberuid' ] = $attrs [ 'memberuid' ][ 0 ];
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function getNiveau01DN ( $id )
{
2022-07-21 16:15:47 +02:00
return 'cn=' . $id . ',' . $this -> baseniveau01 ;
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function Niveau02==============================================================================================================================================
// ==================================================================================================================================================================
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function addNiveau02 ( Niveau02 $niveau02 )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$dn = $this -> getNiveau02DN ( $niveau02 -> getLabel ());
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$attrs [ 'objectclass' ] = $this -> getObjectClassesNiveau02 ();
$this -> fillAttributesNiveau02 ( $niveau02 , $attrs );
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_add ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
public function ismodifyNiveau02 ( Niveau02 $niveau02 , $entry )
{
2022-07-21 16:15:47 +02:00
$attrs = [];
$this -> fillAttributesNiveau02 ( $niveau02 , $attrs );
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
return false ;
}
2022-09-23 16:14:15 +02:00
public function modifyNiveau02 ( Niveau02 $niveau02 , $oldid )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$this -> fillAttributesNiveau02 ( $niveau02 , $attrs );
2022-09-23 16:14:15 +02:00
unset ( $attrs [ 'cn' ]);
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau02DN ( $niveau02 -> getLabel ());
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
2022-09-23 16:14:15 +02:00
@ ldap_mod_del ( $connection , $dn , [ $key => []]);
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
2022-09-23 16:14:15 +02:00
if ( isset ( $oldid ) && $oldid != $niveau02 -> getLabel ()) {
2022-07-21 16:15:47 +02:00
$olddn = $this -> getNiveau02DN ( $oldid );
2022-09-23 16:14:15 +02:00
$this -> rename ( $olddn , 'cn=' . $niveau02 -> getLabel (), $this -> baseniveau02 );
2022-07-21 16:15:47 +02:00
}
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function deleteNiveau02 ( Niveau02 $niveau02 )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> getNiveau02DN ( $niveau02 -> getLabel ());
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> deleteByDN ( $dn );
}
2022-09-23 16:14:15 +02:00
private function getObjectClassesNiveau02 ()
{
$oc = [
2022-07-21 16:15:47 +02:00
'top' ,
'posixGroup' ,
2022-09-23 16:14:15 +02:00
];
2022-07-21 16:15:47 +02:00
return $oc ;
}
2022-09-23 16:14:15 +02:00
public function listAttributesNiveau02 ()
{
2022-07-21 16:15:47 +02:00
return [
2022-09-23 16:14:15 +02:00
'cn' ,
'gidnumber' ,
'memberuid' ,
2022-07-21 16:15:47 +02:00
];
}
2022-09-23 16:14:15 +02:00
public function fillAttributesNiveau02 ( Niveau02 $niveau02 , array & $attrs )
{
$attrs [ 'cn' ] = $niveau02 -> getLabel ();
$attrs [ 'gidnumber' ] = $niveau02 -> getId ();
2022-07-21 16:15:47 +02:00
$attrs [ 'memberuid' ] = [];
2022-09-23 16:14:15 +02:00
foreach ( $niveau02 -> getUsers () as $user ) {
2022-10-02 14:50:48 +02:00
if ( $user -> isIsactive ()) {
array_push ( $attrs [ 'memberuid' ], $user -> getUsername ());
}
2022-07-21 16:15:47 +02:00
}
sort ( $attrs [ 'memberuid' ]);
2022-09-23 16:14:15 +02:00
if ( 1 == count ( $attrs [ 'memberuid' ])) {
$attrs [ 'memberuid' ] = $attrs [ 'memberuid' ][ 0 ];
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function getNiveau02DN ( $id )
{
2022-07-21 16:15:47 +02:00
return 'cn=' . $id . ',' . $this -> baseniveau02 ;
}
2022-09-27 11:52:49 +02:00
// ==================================================================================================================================================================
// == 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 ) {
2022-10-02 14:50:48 +02:00
if ( $user -> isIsactive ()) {
array_push ( $attrs [ 'memberuid' ], $user -> getUsername ());
}
2022-09-27 11:52:49 +02:00
}
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 ) {
2022-10-02 14:50:48 +02:00
if ( $user -> isIsactive ()) {
array_push ( $attrs [ 'memberuid' ], $user -> getUsername ());
}
2022-09-27 11:52:49 +02:00
}
sort ( $attrs [ 'memberuid' ]);
if ( 1 == count ( $attrs [ 'memberuid' ])) {
$attrs [ 'memberuid' ] = $attrs [ 'memberuid' ][ 0 ];
}
}
public function getNiveau04DN ( $id )
{
return 'cn=' . $id . ',' . $this -> baseniveau04 ;
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function Group=================================================================================================================================================
// ==================================================================================================================================================================
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function addGroup ( Group $group )
{
2022-07-21 16:15:47 +02:00
$connection = $this -> connect ();
$dn = $this -> getGroupDN ( $group -> getLabel ());
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$attrs [ 'objectclass' ] = $this -> getObjectClassesGroup ();
$this -> fillAttributesGroup ( $group , $attrs );
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
$result = ldap_add ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
return $result ;
}
2022-09-23 16:14:15 +02:00
public function ismodifyGroup ( Group $group , $entry )
{
2022-07-21 16:15:47 +02:00
$attrs = [];
$this -> fillAttributesGroup ( $group , $attrs );
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
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 ;
}
2022-07-21 16:15:47 +02:00
}
return false ;
}
2022-09-23 16:14:15 +02:00
public function modifyGroup ( Group $group , $oldid )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
$attrs = [];
2022-07-21 16:15:47 +02:00
$this -> fillAttributesGroup ( $group , $attrs );
2022-09-23 16:14:15 +02:00
unset ( $attrs [ 'cn' ]);
2022-07-21 16:15:47 +02:00
$dn = $this -> getGroupDN ( $group -> getLabel ());
2022-09-23 16:14:15 +02:00
foreach ( $attrs as $key => $value ) {
if ( empty ( $value )) {
2022-07-21 16:15:47 +02:00
// Bien mettre un @ car si l'attribut est déjà vide cela crache une erreur car l'attribut n'existe déjà plus
2022-09-23 16:14:15 +02:00
@ ldap_mod_del ( $connection , $dn , [ $key => []]);
2022-07-21 16:15:47 +02:00
unset ( $attrs [ $key ]);
}
}
2022-09-23 16:14:15 +02:00
if ( isset ( $oldid ) && $oldid != $group -> getLabel ()) {
2022-07-21 16:15:47 +02:00
$olddn = $this -> getGroupDN ( $oldid );
2022-09-23 16:14:15 +02:00
$this -> rename ( $olddn , 'cn=' . $group -> getLabel (), $this -> basegroup );
2022-07-21 16:15:47 +02:00
}
$result = ldap_modify ( $connection , $dn , $attrs );
2022-09-23 16:14:15 +02:00
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function deleteGroup ( Group $group )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> getGroupDN ( $group -> getLabel ());
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return $this -> deleteByDN ( $dn );
}
2022-09-23 16:14:15 +02:00
private function getObjectClassesGroup ()
{
$oc = [
2022-07-21 16:15:47 +02:00
'top' ,
'posixGroup' ,
2022-09-23 16:14:15 +02:00
];
2022-07-21 16:15:47 +02:00
return $oc ;
}
2022-09-23 16:14:15 +02:00
public function listAttributesGroup ()
{
2022-07-21 16:15:47 +02:00
return [
2022-09-23 16:14:15 +02:00
'cn' ,
'gidnumber' ,
'memberuid' ,
2022-07-21 16:15:47 +02:00
];
}
2022-09-23 16:14:15 +02:00
public function fillAttributesGroup ( Group $group , array & $attrs )
{
$attrs [ 'cn' ] = $group -> getLabel ();
2022-07-21 16:15:47 +02:00
$attrs [ 'gidnumber' ] = $group -> getId ();
$attrs [ 'memberuid' ] = [];
2022-09-23 16:14:15 +02:00
foreach ( $group -> getUsers () as $usergroup ) {
2022-10-02 14:50:48 +02:00
if ( $usergroup -> getUser () -> isIsactive ()) {
array_push ( $attrs [ 'memberuid' ], $usergroup -> getUser () -> getUsername ());
}
2022-07-21 16:15:47 +02:00
}
sort ( $attrs [ 'memberuid' ]);
2022-09-23 16:14:15 +02:00
if ( 1 == count ( $attrs [ 'memberuid' ])) {
$attrs [ 'memberuid' ] = $attrs [ 'memberuid' ][ 0 ];
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function getGroupDN ( $id )
{
2022-07-21 16:15:47 +02:00
return 'cn=' . $id . ',' . $this -> basegroup ;
}
2022-09-23 16:14:15 +02:00
// ==================================================================================================================================================================
// == Function UserGroup=============================================================================================================================================
// ==================================================================================================================================================================
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function addUserGroup ( UserGroup $usergroup )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// On recherche le group en cours
$criteria = '(cn=' . $usergroup -> getGroup () -> getLabel () . ')' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> basegroup ;
$result = $this -> search ( $criteria , [ 'memberuid' ], $subbranch );
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
if ( ! $this -> in_array_r ( $usergroup -> getUser () -> getUsername (), $result [ 0 ])) {
2022-10-02 14:50:48 +02:00
if ( $usergroup -> getUser () -> isIsactive ()) {
$dn = $this -> getGroupDN ( $usergroup -> getGroup () -> getLabel ());
$entry [ 'memberuid' ] = $usergroup -> getUser () -> getUsername ();
$result = ldap_mod_add ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
}
return $result ;
}
2022-09-23 16:14:15 +02:00
public function delUserGroup ( UserGroup $usergroup )
{
2022-07-21 16:15:47 +02:00
$dn = $this -> basedn ;
$connection = $this -> connect ();
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// On recherche le group en cours
$criteria = '(cn=' . $usergroup -> getGroup () -> getLabel () . ')' ;
2022-09-23 16:14:15 +02:00
$subbranch = $this -> basegroup ;
$result = $this -> search ( $criteria , [ 'memberuid' ], $subbranch );
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
if ( $this -> in_array_r ( $usergroup -> getUser () -> getUsername (), $result [ 0 ])) {
2022-07-21 16:15:47 +02:00
$dn = $this -> getGroupDN ( $usergroup -> getGroup () -> getLabel ());
2022-09-23 16:14:15 +02:00
$entry [ 'memberuid' ] = $usergroup -> getUser () -> getUsername ();
$result = ldap_mod_del ( $connection , $dn , $entry );
if ( ! $result ) {
$this -> ldapError ();
}
2022-07-21 16:15:47 +02:00
}
return $result ;
}
}