LSldap: fix updating internal attributes

LSldap::getEntry method now list expected attributes when using
the LSldap::getLdapEntry method. This method now also accept a
new $attrs attribute to specify expected attributes list.
This commit is contained in:
Benjamin Renard 2022-03-07 16:02:50 +01:00
parent 9c53608fa2
commit 9d3e69c86d

View file

@ -272,27 +272,28 @@ class LSldap extends LSlog_staticLoggerClass {
} }
/** /**
* Retourne une entrée existante ou nouvelle * Return an existing or new LDAP entry
* *
* @author Benjamin Renard <brenard@easter-eggs.com> * @author Benjamin Renard <brenard@easter-eggs.com>
* *
* @param[in] $object_type string Type de l'objet Ldap * @param[in] $object_type string The object type
* @param[in] $dn string DN de l'entré Ldap * @param[in] $dn string The DN of the LDAP entry
* *
* @retval ldapentry|array Un objet ldapentry (PEAR::Net_LDAP2) * @retval ldapentry|array A Net_LDAP2_Entry object or an array if
* ou un tableau (si c'est une nouvelle entr<EFBFBD>e): * it's a new entry:
* Array ( * Array (
* 'entry' => ldapentry, * 'entry' => Net_LDAP2_Entry,
* 'new' => true * 'new' => true
* ) * )
*/ */
public static function getEntry($object_type,$dn) { public static function getEntry($object_type, $dn) {
$obj_classes = LSconfig :: get("LSobjects.$object_type.objectclass"); $obj_classes = LSconfig :: get("LSobjects.$object_type.objectclass");
if(!is_array($obj_classes)){ if(!is_array($obj_classes)){
LSerror :: addErrorCode('LSldap_03'); LSerror :: addErrorCode('LSldap_03');
return; return;
} }
$entry = self :: getLdapEntry($dn); $attrs = array_keys(LSconfig :: get("LSobjects.$object_type.attrs", array(), 'array'));
$entry = self :: getLdapEntry($dn, $attrs);
if ($entry === false) { if ($entry === false) {
$newentry = self :: getNewEntry($dn, $obj_classes, array()); $newentry = self :: getNewEntry($dn, $obj_classes, array());
if (!$newentry) { if (!$newentry) {
@ -310,17 +311,17 @@ class LSldap extends LSlog_staticLoggerClass {
} }
/** /**
* Retourne un object NetLDAP d'une entree existante * Return a Net_LDAP2_Entry object of an existing entry
* *
* @author Benjamin Renard <brenard@easter-eggs.com> * @author Benjamin Renard <brenard@easter-eggs.com>
* *
* @param[in] $dn string DN de l'entré Ldap * @param[in] $dn string DN of the requested LDAP entry
* @param[in] $attrs array|null Array of requested attribute (optional, default: null == all attributes, excepted internal)
* *
* @retval ldapentry|boolean Un objet ldapentry (PEAR::Net_LDAP2) ou false en * @retval ldapentry|boolean A Net_LDAP2_Entry object or false if error occured
* cas de probl<EFBFBD>me
*/ */
public static function getLdapEntry($dn) { public static function getLdapEntry($dn, $attrs=null) {
$entry = self :: $cnx -> getEntry($dn); $entry = self :: $cnx -> getEntry($dn, (is_array($attrs)?$attrs:array()));
if (Net_LDAP2::isError($entry)) { if (Net_LDAP2::isError($entry)) {
return false; return false;
} }
@ -343,16 +344,22 @@ class LSldap extends LSlog_staticLoggerClass {
} }
/** /**
* Retourne une nouvelle entr<EFBFBD>e * Return a new Net_LDAP2_Entry object
* *
* @param[in] $dn string Le DN de l'objet * @param[in] $dn string The DN of the object
* @param[in] $objectClass array Un tableau contenant les objectClass de l'objet * @param[in] $objectClass array Array of the object's object classes
* @param[in] $attrs array Un tabeau du type array('attr_name' => attr_value, ...) * @param[in] $attrs array Array of object attributes values. Format: array('attr_name' => attr_value, ...)
* *
* @retval mixed Le nouvelle objet en cas de succ<EFBFBD>s, false sinon * @retval Net_LDAP2_Entry|False A Net_LDAP2_Entry object on success, False otherwise
*/ */
public static function getNewEntry($dn,$objectClass,$attrs,$add=false) { public static function getNewEntry($dn, $objectClass, $attrs, $add=false) {
$newentry = Net_LDAP2_Entry::createFresh($dn,array_merge(array('objectclass' =>$objectClass),(array)$attrs)); $newentry = Net_LDAP2_Entry::createFresh(
$dn,
array_merge(
array('objectclass' =>$objectClass),
ensureIsArray($attrs)
)
);
if(Net_LDAP2::isError($newentry)) { if(Net_LDAP2::isError($newentry)) {
return false; return false;
} }