You are here

public function LdapEntryDeletionSubscriber::deleteProvisionedLdapEntry in Lightweight Directory Access Protocol (LDAP) 8.4

Delete a provisioned LDAP entry.

Given a Drupal account, delete LDAP entry that was provisioned based on it. This is usually none or one entry but the ldap_user_prov_entries field supports multiple, and thus we are looping through them.

Parameters

\Drupal\ldap_user\Event\LdapUserDeletedEvent $event: Event.

File

ldap_user/src/EventSubscriber/LdapEntryDeletionSubscriber.php, line 80

Class

LdapEntryDeletionSubscriber
Delete LDAP entry.

Namespace

Drupal\ldap_user\EventSubscriber

Code

public function deleteProvisionedLdapEntry(LdapUserDeletedEvent $event) : void {
  if ($this->config
    ->get('ldapEntryProvisionServer') && \in_array(self::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_DELETE, $this->config
    ->get('ldapEntryProvisionTriggers'), TRUE)) {

    /** @var \Drupal\user\Entity\User $account */
    $account = $event->account;

    // Determine server that is associated with user.
    $entries = $account
      ->get('ldap_user_prov_entries')
      ->getValue();
    foreach ($entries as $entry) {
      $parts = explode('|', $entry['value']);
      if (count($parts) === 2) {
        [
          $sid,
          $dn,
        ] = $parts;
        $tokens = [
          '%sid' => $sid,
          '%dn' => $dn,
          '%username' => $account
            ->getAccountName(),
          '%uid' => $account
            ->id(),
        ];
        if ($this->ldapUserManager
          ->setServerById($sid) && $dn) {
          if ($this->ldapUserManager
            ->deleteLdapEntry($dn)) {
            $this->logger
              ->info('LDAP entry on server %sid deleted dn=%dn. username=%username, uid=%uid', $tokens);
          }
        }
        else {
          $this->logger
            ->warning("LDAP server %sid not available, cannot delete record '%dn.'", $tokens);
        }
      }
    }
  }
}