You are here

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

Same name and namespace in other branches
  1. 8.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 value

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 629
Defines server classes and related functions.

Class

LdapServer
LDAP Server Class.

Code

public function modifyLdapEntry($dn, $attributes = [], $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 = [
        '%dn' => $dn,
        '%sid' => $this->sid,
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      ];
      watchdog('ldap_servers', $error, $tokens, WATCHDOG_ERROR);
      return FALSE;
    }
    $entries = ldap_get_entries($this->connection, $result);
    if (is_array($entries) && $entries['count'] == 1) {
      $old_attributes = $entries[0];
    }
  }
  if (!empty($attributes['unicodePwd']) && $this->ldap_type == 'ad') {
    $attributes['unicodePwd'] = ldap_servers_convert_password_for_active_directory_unicodePwd($attributes['unicodePwd']);
  }
  $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];
      }
    }

    // Remove enpty attributes.
    if ($cur_val == '' && $old_value != '') {
      unset($attributes[$key]);
      $result = @ldap_mod_del($this->connection, $dn, [
        $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 = [
          '%dn' => $dn,
          '%sid' => $this->sid,
          '%ldap_errno' => ldap_errno($this->connection),
          '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
        ];
        watchdog('ldap_servers', $error, $tokens, WATCHDOG_ERROR);
        return FALSE;
      }
    }
    elseif (is_array($cur_val)) {
      foreach ($cur_val as $mv_key => $mv_cur_val) {
        if ($mv_cur_val == '') {

          // Remove empty values in multivalues attributes.
          unset($attributes[$key][$mv_key]);
        }
        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 = [
        '%dn' => $dn,
        '%sid' => $this->sid,
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      ];
      watchdog('ldap_servers', $error, $tokens, WATCHDOG_ERROR);
      return FALSE;
    }
  }
  return TRUE;
}