You are here

function LdapServer::modifyLdapEntry in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_servers/LdapServer.class.php \LdapServer::modifyLdapEntry()

modify attributes of ldap entry

Parameters

string $dn DN of entry:

array $attributes should follow the structure of ldap_add functions: entry array: http://us.php.net/manual/en/function.ldap-add.php $attributes["attribute1"] = "value"; $attributes["attribute2"][0] = "value1"; $attributes["attribute2"][1] = "value2";

@return TRUE on success FALSE on error

1 method overrides LdapServer::modifyLdapEntry()
LdapServerTest::modifyLdapEntry in ldap_test/LdapServerTest.class.php
modify attributes of ldap entry

File

ldap_servers/LdapServer.class.php, line 499
Defines server classes and related functions.

Class

LdapServer
LDAP Server Class

Code

function modifyLdapEntry($dn, $attributes = array(), $old_attributes = FALSE) {
  $this
    ->connectAndBindIfNotAlready();
  if (!$old_attributes) {
    $result = @ldap_read($this->connection, $dn, 'objectClass=*');
    if (!$result) {
      $error = "LDAP Server ldap_read(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
      $tokens = array(
        '%dn' => $dn,
        '%sid' => $this->sid,
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      );
      watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
      return FALSE;
    }
    $entries = ldap_get_entries($this->connection, $result);
    if (is_array($entries) && $entries['count'] == 1) {
      $old_attributes = $entries[0];
    }
  }
  $attributes = $this
    ->removeUnchangedAttributes($attributes, $old_attributes);
  foreach ($attributes as $key => $cur_val) {
    $old_value = FALSE;
    $key_lcase = drupal_strtolower($key);
    if (isset($old_attributes[$key_lcase])) {
      if ($old_attributes[$key_lcase]['count'] == 1) {
        $old_value = $old_attributes[$key_lcase][0];
      }
      else {
        unset($old_attributes[$key_lcase]['count']);
        $old_value = $old_attributes[$key_lcase];
      }
    }
    if ($cur_val == '' && $old_value != '') {

      // remove enpty attributes
      unset($attributes[$key]);
      $result = @ldap_mod_del($this->connection, $dn, array(
        $key_lcase => $old_value,
      ));
      if (!$result) {
        $error = "LDAP Server ldap_mod_del(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
        $tokens = array(
          '%dn' => $dn,
          '%sid' => $this->sid,
          '%ldap_errno' => ldap_errno($this->connection),
          '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
        );
        watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
        return FALSE;
      }
    }
    elseif (is_array($cur_val)) {
      foreach ($cur_val as $mv_key => $mv_cur_val) {
        if ($mv_cur_val == '') {
          unset($attributes[$key][$mv_key]);

          // remove empty values in multivalues attributes
        }
        else {
          $attributes[$key][$mv_key] = $mv_cur_val;
        }
      }
    }
  }
  if (count($attributes) > 0) {
    $result = @ldap_modify($this->connection, $dn, $attributes);
    if (!$result) {
      $error = "LDAP Server ldap_modify(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
      $tokens = array(
        '%dn' => $dn,
        '%sid' => $this->sid,
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      );
      watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
      return FALSE;
    }
  }
  return TRUE;
}