diff --git a/lib/Access.php b/lib/Access.php index 273616d9..bd584c21 100644 --- a/lib/Access.php +++ b/lib/Access.php @@ -42,6 +42,7 @@ use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\Attributes\ConverterHub; +use OCA\User_LDAP\Attributes\ConverterException; use OCP\Util; /** @@ -1549,12 +1550,21 @@ private function getAdvancedFilterPartForSearch($search, $searchAttributes) { } $searchWords = \explode(' ', \trim($search)); $wordFilters = []; + $converterHub = ConverterHub::getDefaultConverterHub(); foreach ($searchWords as $word) { - $word = $this->prepareSearchTerm($word); + $preparedWord = $this->prepareSearchTerm($word); //every word needs to appear at least once $wordMatchOneAttrFilters = []; foreach ($searchAttributes as $attr) { - $wordMatchOneAttrFilters[] = "$attr=$word"; + if ($converterHub->hasConverter($attr)) { + try { + $wordMatchOneAttrFilters[] = "$attr=" . $this->prepareSearchTerm($converterHub->str2filter($attr, $word)); + } catch (ConverterException $e) { + $wordMatchOneAttrFilters[] = "$attr=$preparedWord"; + } + } else { + $wordMatchOneAttrFilters[] = "$attr=$preparedWord"; + } } $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); } @@ -1583,15 +1593,33 @@ private function getFilterPartForSearch($search, $searchAttributes, $fallbackAtt } } - $search = $this->prepareSearchTerm($search); + $preparedSearch = $this->prepareSearchTerm($search); + $converterHub = ConverterHub::getDefaultConverterHub(); if (!\is_array($searchAttributes) || \count($searchAttributes) === 0) { if ($fallbackAttribute === '') { return ''; } - $filter[] = "$fallbackAttribute=$search"; + if ($converterHub->hasConverter($fallbackAttribute)) { + try { + $filter[] = "$fallbackAttribute=" . $this->prepareSearchTerm($converterHub->str2filter($fallbackAttribute, $search)); + } catch (ConverterException $e) { + // if failed to convert, then do no convert + $filter[] = "$fallbackAttribute=$preparedSearch"; + } + } else { + $filter[] = "$fallbackAttribute=$preparedSearch"; + } } else { foreach ($searchAttributes as $attribute) { - $filter[] = "$attribute=$search"; + if ($converterHub->hasConverter($attribute)) { + try { + $filter[] = "$attribute=" . $this->prepareSearchTerm($converterHub->str2filter($attribute, $search)); + } catch (ConverterException $e) { + $filter[] = "$attribute=$preparedSearch"; + } + } else { + $filter[] = "$attribute=$preparedSearch"; + } } } if (\count($filter) === 1) { diff --git a/lib/User/UserEntry.php b/lib/User/UserEntry.php index 333f1a74..e65a8210 100644 --- a/lib/User/UserEntry.php +++ b/lib/User/UserEntry.php @@ -314,6 +314,7 @@ public function getAvatarImage() { * @return string[] */ public function getSearchTerms() { + $converterHub = ConverterHub::getDefaultConverterHub(); $rawAttributes = $this->connection->ldapAttributesForUserSearch; $attributes = empty($rawAttributes) ? [] : $rawAttributes; // Get from LDAP if we don't have it already @@ -321,7 +322,11 @@ public function getSearchTerms() { foreach ($attributes as $attr) { $attr = \strtolower($attr); if (isset($this->ldapEntry[$attr])) { + $mustConvert = $converterHub->hasConverter($attr); foreach ($this->ldapEntry[$attr] as $value) { + if ($mustConvert) { + $value = $converterHub->bin2str($attr, $value); + } $value = \trim($value); if (!empty($value)) { $searchTerms[\strtolower($value)] = true; diff --git a/tests/unit/User/UserEntryTest.php b/tests/unit/User/UserEntryTest.php index 7bcaf82b..54173f51 100644 --- a/tests/unit/User/UserEntryTest.php +++ b/tests/unit/User/UserEntryTest.php @@ -610,6 +610,23 @@ public function testGetSearchTerms() { self::assertEquals(['a@b.c', 'alt@b.c', 'foo'], $userEntry->getSearchTerms()); } + public function testGetSearchTermsWithConversion() { + $this->connection->expects($this->once()) + ->method('__get') + ->with($this->equalTo('ldapAttributesForUserSearch')) + ->will($this->returnValue(['objectguid'])); // objectguid is converted by default + $userEntry = new UserEntry( + $this->config, + $this->logger, + $this->connection, + [ + 'dn' => [0 => 'cn=foo,dc=foobar,dc=bar'], + 'objectguid' => [0 => "\xf3\x71\xe2\x36\xa9\x48\x63\x4e\xb6\xbd\x41\xb6\x9d\x9b\x59\xb3"], // all mails should be found + ] + ); + self::assertEquals(['36e271f3-48a9-4e63-b6bd-41b69d9b59b3'], $userEntry->getSearchTerms()); + } + public function testGetSearchTermsUnconfigured() { $this->connection->expects($this->once()) ->method('__get')