includeFiles(); $providers = $this->findProviders(); $attributes = $this->getAttributeNames($providers); $userData = $this->getLdapData($userDn, $attributes, $ldap); return $this->getMessageFromProviders($providers, $userData, $ldap); } /** * Includes all plugin files. */ protected function includeFiles(): void { $pluginDir = dir(__DIR__); while ($entry = $pluginDir->read()) { if ((str_starts_with($entry, '.')) || ($entry === basename(__FILE__))) { continue; } include_once(__DIR__ . '/' . $entry); } } /** * Returns a list of ExtraInvalidCredentialsProvider objects. * * @return ExtraInvalidCredentialsProvider[] providers */ protected function findProviders() { $providers = []; foreach (get_declared_classes() as $declaredClass) { if (in_array('LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS\ExtraInvalidCredentialsProvider', class_implements($declaredClass))) { $providers[] = new $declaredClass(); } } return $providers; } /** * Returns the attribute names for the LDAP search. * * @param ExtraInvalidCredentialsProvider[] $providers * @return string[] attribute names */ protected function getAttributeNames(array $providers): array { $attributeNames = []; foreach ($providers as $provider) { $attributeNames = array_merge($attributeNames, $provider->getAttributeNamesToRead()); } $attributeNames = array_unique($attributeNames); logNewMessage(LOG_DEBUG, 'Extra invalid credentials - attribute names: ' . implode(', ', $attributeNames)); return $attributeNames; } /** * Reads the LDAP data. * * @param string $userDn user DN * @param string[] $attributes attribute names * @param Connection $ldap LDAP handle * @return array|null attribute values */ protected function getLdapData(string $userDn, array $attributes, $ldap) { $data = ldapGetDN($userDn, $attributes, $ldap); logNewMessage(LOG_DEBUG, 'Extra invalid credentials - LDAP data: ' . print_r($data, true)); if ($data === null) { $data = []; } return $data; } /** * Returns the message from the providers. * * @param ExtraInvalidCredentialsProvider[] $providers providers * @param array $userData LDAP data * @param Connection $ldap LDAP handle * @return null|string extra message */ protected function getMessageFromProviders(array $providers, array $userData, $ldap) { foreach ($providers as $provider) { $message = $provider->getExtraMessage($userData, $ldap); if ($message !== null) { return $message; } } return null; } } /** * Interface for provides of extra messages. * * @package LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS */ interface ExtraInvalidCredentialsProvider { /** * Returns the list of attribute names to read from LDAP. * * @return string[] attribute names */ public function getAttributeNamesToRead(): array; /** * Returns an extra message if any. * * @param array $attributes LDAP attributes * @param Connection $ldap LDAP handle * @return null|string message */ public function getExtraMessage(array $attributes, $ldap); }