You are here

public function QueryController::execute in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_query/src/Controller/QueryController.php \Drupal\ldap_query\Controller\QueryController::execute()

Execute query.

Parameters

null|string $filter: Optional parameter to override filters. Useful for Views and other queries requiring filtering.

File

ldap_query/src/Controller/QueryController.php, line 110

Class

QueryController
Controller class for LDAP queries, in assistance to the entity itself.

Namespace

Drupal\ldap_query\Controller

Code

public function execute(?string $filter = NULL) : void {
  if ($this->query) {
    if ($filter === NULL) {
      $filter = $this->query
        ->getFilter();
    }

    // @todo exception handling.
    $this->ldapBridge
      ->setServerById($this->query
      ->getServerId());
    if ($this->ldapBridge
      ->bind()) {
      $base_dn_results = [];
      foreach ($this->query
        ->getProcessedBaseDns() as $base_dn) {
        $options = [
          'filter' => $this->query
            ->getProcessedAttributes(),
          'maxItems' => $this->query
            ->getSizeLimit(),
          'timeout' => $this->query
            ->getTimeLimit(),
          'deref' => $this->query
            ->getDereference(),
          'scope' => $this->query
            ->getScope(),
          // @todo Make this configurable on query or, ideally, server.
          'pageSize' => 1000,
        ];
        try {
          $ldap_response = $this->ldapBridge
            ->get()
            ->query($base_dn, $filter, $options)
            ->execute()
            ->toArray();
        } catch (LdapException $e) {
          $this->logger
            ->warning('LDAP query exception %message', [
            '@message' => $e
              ->getMessage(),
          ]);
          $ldap_response = FALSE;
        }
        if ($ldap_response) {
          $base_dn_results[] = $ldap_response;
        }
      }
      $this->results = array_merge(...$base_dn_results);
    }
  }
  else {
    $this->logger
      ->warning('Could not load query @query', [
      '@query' => $this->qid,
    ]);
  }
}