You are here

class LdapEntryDeletionSubscriber in Lightweight Directory Access Protocol (LDAP) 8.4

Delete LDAP entry.

Hierarchy

Expanded class hierarchy of LdapEntryDeletionSubscriber

1 string reference to 'LdapEntryDeletionSubscriber'
ldap_user.services.yml in ldap_user/ldap_user.services.yml
ldap_user/ldap_user.services.yml
1 service uses LdapEntryDeletionSubscriber
ldap_user.ldap_delete in ldap_user/ldap_user.services.yml
Drupal\ldap_user\EventSubscriber\LdapEntryDeletionSubscriber

File

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

Namespace

Drupal\ldap_user\EventSubscriber
View source
class LdapEntryDeletionSubscriber implements EventSubscriberInterface, LdapUserAttributesInterface {

  /**
   * Config.
   *
   * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * LDAP User Manager.
   *
   * @var \Drupal\ldap_servers\LdapUserManager
   */
  protected $ldapUserManager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   Config factory.
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger.
   * @param \Drupal\ldap_servers\LdapUserManager $ldap_user_manager
   *   LDAP user manager.
   */
  public function __construct(ConfigFactory $config_factory, LoggerInterface $logger, LdapUserManager $ldap_user_manager) {
    $this->config = $config_factory
      ->get('ldap_user.settings');
    $this->logger = $logger;
    $this->ldapUserManager = $ldap_user_manager;
  }

  /**
   * {@inheritdoc}
   *
   * @uses LdapEntryDeletionSubscriber::deleteProvisionedLdapEntry()
   */
  public static function getSubscribedEvents() : array {
    $events[LdapUserDeletedEvent::EVENT_NAME] = [
      'deleteProvisionedLdapEntry',
    ];
    return $events;
  }

  /**
   * 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.
   *
   * @param \Drupal\ldap_user\Event\LdapUserDeletedEvent $event
   *   Event.
   */
  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);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LdapEntryDeletionSubscriber::$config protected property Config.
LdapEntryDeletionSubscriber::$ldapUserManager protected property LDAP User Manager.
LdapEntryDeletionSubscriber::$logger protected property Logger.
LdapEntryDeletionSubscriber::deleteProvisionedLdapEntry public function Delete a provisioned LDAP entry.
LdapEntryDeletionSubscriber::getSubscribedEvents public static function @uses LdapEntryDeletionSubscriber::deleteProvisionedLdapEntry()
LdapEntryDeletionSubscriber::__construct public function Constructor.
LdapUserAttributesInterface::ACCOUNT_CREATION_LDAP_BEHAVIOUR public constant Event config.
LdapUserAttributesInterface::ACCOUNT_CREATION_USER_SETTINGS_FOR_LDAP public constant Config.
LdapUserAttributesInterface::EVENT_CREATE_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_CREATE_LDAP_ENTRY public constant Event config.
LdapUserAttributesInterface::EVENT_LDAP_ASSOCIATE_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_SYNC_TO_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_SYNC_TO_LDAP_ENTRY public constant Event config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_LDAP_ASSOCIATE public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_NO_LDAP_ASSOCIATE public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_REJECT public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_SHOW_OPTION_ON_FORM public constant Config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_AUTHENTICATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_ON_MANUAL_CREATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_UPDATE_CREATE public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_AUTHENTICATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_DELETE public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_UPDATE_CREATE public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_ALL constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_DRUPAL public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_LDAP public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_NONE public constant Provision config.
LdapUserAttributesInterface::USER_CONFLICT_ATTEMPT_RESOLVE public constant Config.
LdapUserAttributesInterface::USER_CONFLICT_LOG public constant Config.