diff --git a/src/Service/HealthCheck.php b/src/Service/HealthCheck.php index 835648c875287997c9cd26cac118ce9b62a7d48b..e72c6eae2aa7337881b0865b1a2639fcec378bd1 100644 --- a/src/Service/HealthCheck.php +++ b/src/Service/HealthCheck.php @@ -41,6 +41,7 @@ class HealthCheck implements CheckInterface { $results = []; $results[] = $this->checkMethod('Check if we can connect to the LDAP server', [$this->ldap, 'checkConnection']); + $results[] = $this->checkMethod('Check if all attributes are available', [$this->ldap, 'checkAttributes']); return $results; } diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php index 85c814aaf70ad517672bdad05630debb6b34ddef..cac8b9e8836c0a147e4771cb48862a0968f610bb 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -111,6 +111,42 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface $builder->first(); } + public function checkAttributeExists(string $attribute): bool + { + $provider = $this->getProvider(); + $builder = $this->getCachedBuilder($provider); + + /** @var User $user */ + $user = $builder + ->where('objectClass', '=', $provider->getSchema()->person()) + ->whereHas($attribute) + ->first(); + + return $user !== null; + } + + public function checkAttributes() + { + $attributes = [ + $this->identifierAttributeName, + $this->givenNameAttributeName, + $this->familyNameAttributeName, + $this->emailAttributeName, + $this->birthdayAttributeName, + ]; + + $missing = []; + foreach ($attributes as $attr) { + if ($attr !== '' && !$this->checkAttributeExists($attr)) { + $missing[] = $attr; + } + } + + if (count($missing) > 0) { + throw new \RuntimeException('The following LDAP attributes were not found: '.join(', ', $missing)); + } + } + public function setDeploymentEnvironment(string $env) { $this->deploymentEnv = $env;