You are here

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

Remove unchanged attributes from entry.

Given 2 LDAP entries, old and new, removed unchanged values to avoid security errors and incorrect date modified.

Parameters

array $newEntry: LDAP entry in form <attribute> => <value>.

array $oldEntry: LDAP entry in form <attribute> => ['count' => N, [<value>,...<value>]].

Return value

array LDAP entry with no values that have NOT changed.

2 calls to Server::removeUnchangedAttributes()
Server::modifyLdapEntry in ldap_servers/src/Entity/Server.php
Modify attributes of LDAP entry.
ServerTests::testRemoveUnchangedAttributes in ldap_servers/tests/src/Unit/ServerTests.php
Test removing unchanged attributes.

File

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

Class

Server
Defines the Server entity.

Namespace

Drupal\ldap_servers\Entity

Code

public static function removeUnchangedAttributes(array $newEntry, array $oldEntry) {
  foreach ($newEntry as $key => $newValue) {
    $oldValue = FALSE;
    $oldValueIsScalar = NULL;
    $keyLowercased = mb_strtolower($key);

    // TODO: Make this if loop include the actions when tests are available.
    if (isset($oldEntry[$keyLowercased])) {
      if ($oldEntry[$keyLowercased]['count'] == 1) {
        $oldValue = $oldEntry[$keyLowercased][0];
        $oldValueIsScalar = TRUE;
      }
      else {
        unset($oldEntry[$keyLowercased]['count']);
        $oldValue = $oldEntry[$keyLowercased];
        $oldValueIsScalar = FALSE;
      }
    }

    // Identical multivalued attributes.
    if (is_array($newValue) && is_array($oldValue) && count(array_diff($newValue, $oldValue)) == 0) {
      unset($newEntry[$key]);
    }
    elseif ($oldValueIsScalar && !is_array($newValue) && mb_strtolower($oldValue) == mb_strtolower($newValue)) {

      // Don't change values that aren't changing to avoid false permission
      // constraints.
      unset($newEntry[$key]);
    }
  }
  return $newEntry;
}