You are here

public function LdapUserProcessor::deleteProvisionedLdapEntries in Lightweight Directory Access Protocol (LDAP) 8.3

Delete a provisioned LDAP entry.

Given a Drupal account, delete LDAP entry that was provisioned based on it normally this will be 0 or 1 entry, but the ldap_user_prov_entries field attached to the user entity track each LDAP entry provisioned.

Parameters

\Drupal\user\Entity\User $account: Drupal user account.

Return value

bool FALSE indicates failed or action not enabled in LDAP user configuration.

File

ldap_user/src/Processor/LdapUserProcessor.php, line 479

Class

LdapUserProcessor
Processor for LDAP provisioning.

Namespace

Drupal\ldap_user\Processor

Code

public function deleteProvisionedLdapEntries(User $account) {

  // Determine server that is associated with user.
  $result = FALSE;
  $entries = $account
    ->get('ldap_user_prov_entries')
    ->getValue();
  foreach ($entries as $entry) {
    $parts = explode('|', $entry['value']);
    if (count($parts) == 2) {
      list($sid, $dn) = $parts;
      $factory = \Drupal::service('ldap.servers');
      $ldap_server = $factory
        ->getServerById($sid);
      if (is_object($ldap_server) && $dn) {

        /** @var \Drupal\ldap_servers\Entity\Server $ldap_server */
        $result = $ldap_server
          ->deleteLdapEntry($dn);
        $tokens = [
          '%sid' => $sid,
          '%dn' => $dn,
          '%username' => $account
            ->getAccountName(),
          '%uid' => $account
            ->id(),
        ];
        if ($result) {
          \Drupal::logger('ldap_user')
            ->info('LDAP entry on server %sid deleted dn=%dn. username=%username, uid=%uid', $tokens);
        }
        else {
          \Drupal::logger('ldap_user')
            ->error('LDAP entry on server %sid not deleted because error. username=%username, uid=%uid', $tokens);
        }
      }
      else {
        $result = FALSE;
      }
    }
  }
  return $result;
}