Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search terms for binary fields will be converted if possible #815

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions lib/User/UserEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,19 @@ 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
$searchTerms = [];
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;
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/User/UserEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,23 @@ public function testGetSearchTerms() {
self::assertEquals(['[email protected]', '[email protected]', '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')
Expand Down