From 31b584b3388a596fed007dbeda9849fa550d472e Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 11 Nov 2015 12:44:08 +0100 Subject: [PATCH] LdapConnection: Fix method fetchOne() The method suffered from multiple issues: * Actual NULL values were interpreted as if the row does not have any cols * Which attribute's value got returned was dependent on the result set instead of the desired columns refs #10567 --- .../Icinga/Protocol/Ldap/LdapConnection.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Protocol/Ldap/LdapConnection.php b/library/Icinga/Protocol/Ldap/LdapConnection.php index 77872781c..1cad06680 100644 --- a/library/Icinga/Protocol/Ldap/LdapConnection.php +++ b/library/Icinga/Protocol/Ldap/LdapConnection.php @@ -476,8 +476,28 @@ class LdapConnection implements Selectable, Inspectable */ public function fetchOne(LdapQuery $query, array $fields = null) { - $row = (array) $this->fetchRow($query, $fields); - return array_shift($row) ?: false; + $row = $this->fetchRow($query, $fields); + if ($row === false) { + return false; + } + + $values = get_object_vars($row); + if (empty($values)) { + return false; + } + + if ($fields === null) { + // Fetch the desired columns from the query if not explicitly overriden in the method's parameter + $fields = $query->getColumns(); + } + + if (empty($fields)) { + // The desired columns may be empty independently whether provided by the query or the method's parameter + return array_shift($values); + } + + $alias = key($fields); + return $values[is_string($alias) ? $alias : $fields[$alias]]; } /**