You are here

public function LDAPAuthorizationProvider::filterProposals in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_authorization/src/Plugin/authorization/Provider/LDAPAuthorizationProvider.php \Drupal\ldap_authorization\Plugin\authorization\Provider\LDAPAuthorizationProvider::filterProposals()

Provider-side filtering.

Parameters

array $proposals: Available proposals.

array $providerMapping: What the proposal should be filtered against in the provider.

Return value

array Filtered proposals.

Overrides ProviderInterface::filterProposals

File

ldap_authorization/src/Plugin/authorization/Provider/LDAPAuthorizationProvider.php, line 295

Class

LDAPAuthorizationProvider
The LDAP authorization provider for authorization module.

Namespace

Drupal\ldap_authorization\Plugin\authorization\Provider

Code

public function filterProposals(array $proposedLdapAuthorizations, array $providerMapping) : array {
  $filtered_proposals = [];
  foreach ($proposedLdapAuthorizations as $key => $value) {
    if ($providerMapping['is_regex']) {
      $pattern = $providerMapping['query'];
      try {
        if (preg_match($pattern, $value, $matches)) {

          // If there is a sub-pattern then return the first one.
          // Named sub-patterns not supported.
          if (count($matches) > 1) {
            $filtered_proposals[$key] = $matches[1];
          }
          elseif (count($matches) === 1) {
            $filtered_proposals[$key] = $matches[0];
          }
          else {
            $filtered_proposals[$key] = $value;
          }
        }
      } catch (\Exception $e) {
        \Drupal::logger('ldap')
          ->error('Error in matching regular expression @regex', [
          '@regex' => $pattern,
        ]);
      }
    }
    elseif (mb_strtolower($value) === mb_strtolower($providerMapping['query'])) {
      $filtered_proposals[$key] = $value;
    }
  }
  return $filtered_proposals;
}