You are here

public function Server::modifyLdapEntry in Lightweight Directory Access Protocol (LDAP) 8.3

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";.

bool|array $oldAttributes: Existing attributes.

Return value

bool Result of query.

File

ldap_servers/src/Entity/Server.php, line 571

Class

Server
Defines the Server entity.

Namespace

Drupal\ldap_servers\Entity

Code

public function modifyLdapEntry($dn, array $attributes = [], $oldAttributes = FALSE) {
  $this
    ->connectAndBindIfNotAlready();
  if (!$oldAttributes) {
    $result = @ldap_read($this->connection, $dn, 'objectClass=*');
    if (!$result) {
      $this->logger
        ->error("LDAP Server ldap_read(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %id, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ", [
        '%dn' => $dn,
        '%id' => $this
          ->id(),
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      ]);
      return FALSE;
    }
    $entries = ldap_get_entries($this->connection, $result);
    if (is_array($entries) && $entries['count'] == 1) {
      $oldAttributes = $entries[0];
    }
  }
  if (!empty($attributes['unicodePwd']) && $this
    ->get('type') == 'ad') {
    $attributes['unicodePwd'] = $this
      ->convertPasswordForActiveDirectoryunicodePwd($attributes['unicodePwd']);
  }
  $attributes = $this
    ->removeUnchangedAttributes($attributes, $oldAttributes);
  foreach ($attributes as $key => $currentValue) {
    $oldValue = FALSE;
    $keyLowercased = mb_strtolower($key);
    if (isset($oldAttributes[$keyLowercased])) {
      if ($oldAttributes[$keyLowercased]['count'] == 1) {
        $oldValue = $oldAttributes[$keyLowercased][0];
      }
      else {
        unset($oldAttributes[$keyLowercased]['count']);
        $oldValue = $oldAttributes[$keyLowercased];
      }
    }

    // Remove empty attributes.
    if ($currentValue == '' && $oldValue != '') {
      unset($attributes[$key]);
      $result = @ldap_mod_del($this->connection, $dn, [
        $keyLowercased => $oldValue,
      ]);
      if (!$result) {
        $this->logger
          ->error("LDAP Server ldap_mod_del(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %id, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ", [
          '%dn' => $dn,
          '%id' => $this
            ->id(),
          '%ldap_errno' => ldap_errno($this->connection),
          '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
        ]);
        return FALSE;
      }
    }
    elseif (is_array($currentValue)) {
      foreach ($currentValue as $nestedKey => $nestedValue) {
        if ($nestedValue == '') {

          // Remove empty values in multivalues attributes.
          unset($attributes[$key][$nestedKey]);
        }
        else {
          $attributes[$key][$nestedKey] = $nestedValue;
        }
      }
    }
  }
  if (count($attributes) > 0) {
    $result = @ldap_modify($this->connection, $dn, $attributes);
    if (!$result) {
      $this->logger
        ->error("LDAP Server ldap_modify(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %id, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ", [
        '%dn' => $dn,
        '%id' => $this
          ->id(),
        '%ldap_errno' => ldap_errno($this->connection),
        '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)),
      ]);
      return FALSE;
    }
  }
  return TRUE;
}