Skip to content

Commit 4c7d9b1

Browse files
committed
fix: search terms for binary fields will be converted if possible
1 parent d8e1491 commit 4c7d9b1

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/Access.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCA\User_LDAP\User\Manager;
4343
use OCA\User_LDAP\Mapping\AbstractMapping;
4444
use OCA\User_LDAP\Attributes\ConverterHub;
45+
use OCA\User_LDAP\Attributes\ConverterException;
4546
use OCP\Util;
4647

4748
/**
@@ -1549,12 +1550,21 @@ private function getAdvancedFilterPartForSearch($search, $searchAttributes) {
15491550
}
15501551
$searchWords = \explode(' ', \trim($search));
15511552
$wordFilters = [];
1553+
$converterHub = ConverterHub::getDefaultConverterHub();
15521554
foreach ($searchWords as $word) {
1553-
$word = $this->prepareSearchTerm($word);
1555+
$preparedWord = $this->prepareSearchTerm($word);
15541556
//every word needs to appear at least once
15551557
$wordMatchOneAttrFilters = [];
15561558
foreach ($searchAttributes as $attr) {
1557-
$wordMatchOneAttrFilters[] = "$attr=$word";
1559+
if ($converterHub->hasConverter($attr)) {
1560+
try {
1561+
$wordMatchOneAttrFilters[] = "$attr=" . $this->prepareSearchTerm($converterHub->str2filter($attr, $word));
1562+
} catch (ConverterException $e) {
1563+
$wordMatchOneAttrFilters[] = "$attr=$preparedWord";
1564+
}
1565+
} else {
1566+
$wordMatchOneAttrFilters[] = "$attr=$preparedWord";
1567+
}
15581568
}
15591569
$wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);
15601570
}
@@ -1583,15 +1593,33 @@ private function getFilterPartForSearch($search, $searchAttributes, $fallbackAtt
15831593
}
15841594
}
15851595

1586-
$search = $this->prepareSearchTerm($search);
1596+
$preparedSearch = $this->prepareSearchTerm($search);
1597+
$converterHub = ConverterHub::getDefaultConverterHub();
15871598
if (!\is_array($searchAttributes) || \count($searchAttributes) === 0) {
15881599
if ($fallbackAttribute === '') {
15891600
return '';
15901601
}
1591-
$filter[] = "$fallbackAttribute=$search";
1602+
if ($converterHub->hasConverter($fallbackAttribute)) {
1603+
try {
1604+
$filter[] = "$fallbackAttribute=" . $this->prepareSearchTerm($converterHub->str2filter($fallbackAttribute, $search));
1605+
} catch (ConverterException $e) {
1606+
// if failed to convert, then do no convert
1607+
$filter[] = "$fallbackAttribute=$preparedSearch";
1608+
}
1609+
} else {
1610+
$filter[] = "$fallbackAttribute=$preparedSearch";
1611+
}
15921612
} else {
15931613
foreach ($searchAttributes as $attribute) {
1594-
$filter[] = "$attribute=$search";
1614+
if ($converterHub->hasConverter($attribute)) {
1615+
try {
1616+
$filter[] = "$attribute=" . $this->prepareSearchTerm($converterHub->str2filter($attribute, $search));
1617+
} catch (ConverterException $e) {
1618+
$filter[] = "$attribute=$preparedSearch";
1619+
}
1620+
} else {
1621+
$filter[] = "$attribute=$preparedSearch";
1622+
}
15951623
}
15961624
}
15971625
if (\count($filter) === 1) {

lib/User/UserEntry.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,19 @@ public function getAvatarImage() {
314314
* @return string[]
315315
*/
316316
public function getSearchTerms() {
317+
$converterHub = ConverterHub::getDefaultConverterHub();
317318
$rawAttributes = $this->connection->ldapAttributesForUserSearch;
318319
$attributes = empty($rawAttributes) ? [] : $rawAttributes;
319320
// Get from LDAP if we don't have it already
320321
$searchTerms = [];
321322
foreach ($attributes as $attr) {
322323
$attr = \strtolower($attr);
323324
if (isset($this->ldapEntry[$attr])) {
325+
$mustConvert = $converterHub->hasConverter($attr);
324326
foreach ($this->ldapEntry[$attr] as $value) {
327+
if ($mustConvert) {
328+
$value = $converterHub->bin2str($attr, $value);
329+
}
325330
$value = \trim($value);
326331
if (!empty($value)) {
327332
$searchTerms[\strtolower($value)] = true;

0 commit comments

Comments
 (0)