You are here

public function LdapUserManager::getUserAccountFromPuid in Lightweight Directory Access Protocol (LDAP) 8.4

Fetches the user account based on the persistent UID.

Parameters

string $puid: As returned from ldap_read or other LDAP function (can be binary).

Return value

\Drupal\user\UserInterface|null The updated user or error.

File

ldap_servers/src/LdapUserManager.php, line 154

Class

LdapUserManager
LDAP User Manager.

Namespace

Drupal\ldap_servers

Code

public function getUserAccountFromPuid(string $puid) : ?UserInterface {
  $result = NULL;
  if ($this
    ->checkAvailability()) {
    $storage = $this->entityTypeManager
      ->getStorage('user');
    $query = $storage
      ->getQuery();
    $query
      ->condition('ldap_user_puid_sid', $this->server
      ->id(), '=')
      ->condition('ldap_user_puid', $puid, '=')
      ->condition('ldap_user_puid_property', $this->server
      ->getUniquePersistentAttribute(), '=')
      ->accessCheck(FALSE);
    $queryResult = $query
      ->execute();
    if (count($queryResult) === 1) {

      /** @var \Drupal\user\UserInterface $result */
      $result = $storage
        ->load(array_values($queryResult)[0]);
    }
    if (count($queryResult) > 1) {
      $uids = implode(',', $queryResult);
      $this->logger
        ->error('Multiple users (uids: %uids) with same puid (puid=%puid, sid=%sid, ldap_user_puid_property=%ldap_user_puid_property)', [
        '%uids' => $uids,
        '%puid' => $puid,
        '%id' => $this->server
          ->id(),
        '%ldap_user_puid_property' => $this->server
          ->getUniquePersistentAttribute(),
      ]);
    }
  }
  return $result;
}