Skip to content

Commit

Permalink
Take medial search config into account while searching for remote users
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Oct 1, 2019
1 parent af2e241 commit e55f4ba
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/CardDAV/AddressBookImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getDisplayName() {
* @since 5.0.0
*/
public function search($pattern, $searchProperties, $options, $limit = null, $offset = null) {
$results = $this->backend->search($this->getKey(), $pattern, $searchProperties, $limit, $offset);
$results = $this->backend->searchEx($this->getKey(), $pattern, $searchProperties, $options, $limit, $offset);

$vCards = [];
foreach ($results as $result) {
Expand Down
40 changes: 39 additions & 1 deletion apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,52 @@ public function updateShares(IShareable $shareable, $add, $remove) {
* @return array an array of contacts which are arrays of key-value-pairs
*/
public function search($addressBookId, $pattern, $searchProperties, $limit = 100, $offset = 0) {
return $this->searchEx($addressBookId, $pattern, $searchProperties, [], $limit, $offset);
}

/**
* search contact with options
*
* @param int $addressBookId
* @param string $pattern which should match within the $searchProperties
* @param array $searchProperties defines the properties within the query pattern should match
* @param array $options
* available options:
* 'matchMode'
* - 'ANY' (default) - pattern can be anywhere in property value
* - 'START' - property value should start with pattern
* - 'END' - property value should end with pattern
* - 'EXACT' - property value should match the pattern exactly
* @param int $limit
* @param int $offset
* @return array an array of contacts which are arrays of key-value-pairs
*/
public function searchEx($addressBookId, $pattern, $searchProperties, $options, $limit = 100, $offset = 0) {
$query = $this->db->getQueryBuilder();
$query2 = $this->db->getQueryBuilder();
$query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp');

$matchMode = $options['matchMode'] ?? 'any';
switch ($matchMode) {
case 'START':
$searchPattern = $this->db->escapeLikeParameter($pattern) . '%';
break;
case 'END':
$searchPattern = '%' . $this->db->escapeLikeParameter($pattern);
break;
case 'EXACT':
$searchPattern = $this->db->escapeLikeParameter($pattern);
break;
case 'ANY':
default:
$searchPattern = '%' . $this->db->escapeLikeParameter($pattern) . '%';
}

foreach ($searchProperties as $property) {
$query2->orWhere(
$query2->expr()->andX(
$query2->expr()->eq('cp.name', $query->createNamedParameter($property)),
$query2->expr()->iLike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))
$query2->expr()->iLike('cp.value', $query->createNamedParameter($searchPattern))
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/dav/tests/unit/CardDAV/AddressBookImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public function testSearch() {
$pattern = 'pattern';
$searchProperties = ['properties'];

$this->backend->expects($this->once())->method('search')
->with($this->addressBookInfo['id'], $pattern, $searchProperties, 10, 0)
$this->backend->expects($this->once())->method('searchEx')
->with($this->addressBookInfo['id'], $pattern, $searchProperties, [], 10, 0)
->willReturn(
[
['uri' => 'foo.vcf', 'carddata' => 'cardData1'],
Expand Down
11 changes: 10 additions & 1 deletion apps/files_sharing/lib/Controller/ShareesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,16 @@ protected function getRemote($search) {
// Fetch remote search properties from app config
$searchProperties = \explode(',', $this->config->getAppValue('dav', 'remote_search_properties', 'CLOUD,FN'));
// Search in contacts
$addressBookContacts = $this->contactsManager->search($search, $searchProperties, [], $this->limit, $this->offset);
$matchMode = $this->config->getSystemValue('accounts.enable_medial_search', true) === true
? 'ANY'
: 'START';
$addressBookContacts = $this->contactsManager->search(
$search,
$searchProperties,
[ 'matchMode' => $matchMode ],
$this->limit,
$this->offset
);
$foundRemoteById = false;
foreach ($addressBookContacts as $contact) {
if (isset($contact['isLocalSystemBook'])) {
Expand Down
12 changes: 8 additions & 4 deletions apps/files_sharing/tests/API/ShareesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,10 +1426,14 @@ public function testGetRemote($searchTerm, $contacts, $shareeEnumeration, $exact
$this->invokePrivate($this->sharees, 'limit', [2]);
$this->invokePrivate($this->sharees, 'offset', [0]);

$configMap = [
['trusted_domains', [], ['trusted.domain.tld', 'trusted2.domain.tld']],
['accounts.enable_medial_search', true, true]
];

$this->config->expects($this->any())
->method('getSystemValue')
->with('trusted_domains')
->willReturn(['trusted.domain.tld', 'trusted2.domain.tld']);
->will($this->returnValueMap($configMap));
$this->userSearch->expects($this->any())
->method('isSearchable')
->willReturn($isSearchable);
Expand All @@ -1448,7 +1452,7 @@ public function testGetRemote($searchTerm, $contacts, $shareeEnumeration, $exact
$this->invokePrivate($this->sharees, 'shareeEnumeration', [$shareeEnumeration]);
$this->contactsManager->expects($this->any())
->method('search')
->with($searchTerm, ['EMAIL', 'CLOUD', 'FN'], [], 2, 0)
->with($searchTerm, ['EMAIL', 'CLOUD', 'FN'], ['matchMode' => 'ANY'], 2, 0)
->willReturn($contacts);

$this->invokePrivate($this->sharees, 'getRemote', [$searchTerm]);
Expand Down Expand Up @@ -1961,7 +1965,7 @@ public function testGetUserWithSearchAttributes() {

$exactExpected = [['label' => 'Bob', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'testBob']]];
$expected = [];

$this->invokePrivate($this->sharees, 'getUsers', [$searchTerm]);
$result = $this->invokePrivate($this->sharees, 'result');
$this->assertEquals($exactExpected, $result['exact']['users']);
Expand Down

0 comments on commit e55f4ba

Please sign in to comment.