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

[stable2.2] Fix: Autocompletetion for system address book (not just full matches) #8635

Merged
merged 2 commits into from
Sep 12, 2023
Merged
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
31 changes: 19 additions & 12 deletions lib/Service/ContactsIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getMatchingRecipient(string $userId, string $term): array {
$shareeEnumerationFullMatchUserId = $shareeEnumerationFullMatch && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes') === 'yes';
$shareeEnumerationFullMatchEmail = $shareeEnumerationFullMatch && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';

$result = $this->contactsManager->search($term, ['UID', 'FN', 'EMAIL'], ['enumeration' => $shareeEnumeration]);
$result = $this->contactsManager->search($term, ['UID', 'FN', 'EMAIL'], ['enumeration' => $shareeEnumeration, 'fullmatch' => $shareeEnumerationFullMatch]);
if (empty($result)) {
return [];
}
Expand Down Expand Up @@ -118,18 +118,25 @@ public function getMatchingRecipient(string $userId, string $term): array {
continue;
}
$lowerTerm = strtolower($term);
if (!$isSystemUser || $isInSameGroup || ($lowerTerm !== '' && (
($shareeEnumerationFullMatch && !empty($fn) && $lowerTerm === strtolower($fn)) ||
($shareeEnumerationFullMatchUserId && $lowerTerm === strtolower($id)) ||
($shareeEnumerationFullMatchEmail && $lowerTerm === strtolower($e))))) {
$receivers[] = [
'id' => $id,
// Show full name if possible or fall back to email
'label' => (empty($fn) ? $e : "$fn ($e)"),
'email' => $e,
'photo' => $photo,
];

if ($isSystemUser && $shareeEnumerationInGroupOnly && !$isInSameGroup) {
// Check for full match. If full match is disabled, matching results already filtered out
if (!($lowerTerm !== '' && (
($shareeEnumerationFullMatch && !empty($fn) && $lowerTerm === strtolower($fn)) ||
($shareeEnumerationFullMatchUserId && $lowerTerm === strtolower($id)) ||
($shareeEnumerationFullMatchEmail && $lowerTerm === strtolower($e))))) {
// Not a full Match
continue;
}
}

$receivers[] = [
'id' => $id,
// Show full name if possible or fall back to email
'label' => (empty($fn) ? $e : "$fn ($e)"),
'email' => $e,
'photo' => $photo,
];
}
}

Expand Down
179 changes: 93 additions & 86 deletions tests/Unit/Service/ContactsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testGetMatchingRecipientRestrictedToGroup() {
],
];

$this->common($term, $searchResult, true, true, false, false);
$this->common($term, $searchResult, true, true, false, false, false);
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
Expand Down Expand Up @@ -191,150 +191,157 @@ public function testGetMatchingRecipientRestrictedToGroup() {
$this->assertEquals($expected, $actual);
}

public function testGetMatchingRecipientRestrictedToFullMatch() {
$term = 'jo'; // searching for: John Doe
public function testGetMatchingRecipientRestrictedToGroupFullMatchUserId() {
$term = 'jf'; // searching for: Jonathan Frakes
$searchResult = [
[
// Simple match
'UID' => 'jf',
'FN' => 'Jonathan Frakes',
'EMAIL' => 'jonathan@frakes.com',
'isLocalSystemBook' => true,
],
[
'UID' => 'jd',
'FN' => 'John Doe',
'EMAIL' => [
'john@doe.info',
'doe@john.info',
],
'isLocalSystemBook' => true,
],
[
'UID' => 'js',
'FN' => 'Johann Strauss II',
'EMAIL' => 'johann@strauss.com',
],
];
$this->common($term, $searchResult, true, false, true);

$this->common($term, $searchResult, true, true, true);
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->with('auser')
->will($this->returnValue($user));
$this->groupManager->expects($this->once())
->method('getUserGroupIds')
->will($this->returnValue(['agroup']));
$this->groupManager->expects(self::exactly(1))
->method('isInGroup')
->withConsecutive(['jf', 'agroup'])
->willReturnOnConsecutiveCalls(false);

$expected = [
[
'id' => 'js',
'label' => 'Johann Strauss II (johann@strauss.com)',
'email' => 'johann@strauss.com',
'id' => 'jf',
'label' => 'Jonathan Frakes (jonathan@frakes.com)',
'email' => 'jonathan@frakes.com',
'photo' => null,
],
];

$actual = $this->contactsIntegration->getMatchingRecipient("", $term);
$actual = $this->contactsIntegration->getMatchingRecipient("auser", $term);

$this->assertEquals($expected, $actual);
}

public function testGetMatchingRecipientRestrictedToFullMatchFullName() {
$term = 'john doe'; // searching for: John Doe
public function testGetMatchingRecipientRestrictedToGroupFullMatchFullName() {
$term = 'Jonathan Frakes'; // searching for: Jonathan Frakes
$searchResult = [
[
// Array of addresses
'UID' => 'jd',
'FN' => 'John Doe',
'EMAIL' => [
'john@doe.info',
'doe@john.info',
],
// Simple match
'UID' => 'jf',
'FN' => 'Jonathan Frakes',
'EMAIL' => 'jonathan@frakes.com',
'isLocalSystemBook' => true,
]
],
];

$this->common($term, $searchResult, true, false, true);
$this->common($term, $searchResult, true, true, true);
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->with('auser')
->will($this->returnValue($user));
$this->groupManager->expects($this->once())
->method('getUserGroupIds')
->will($this->returnValue(['agroup']));
$this->groupManager->expects(self::exactly(1))
->method('isInGroup')
->withConsecutive(['jf', 'agroup'])
->willReturnOnConsecutiveCalls(false);

$expected = [
[
'id' => 'jd',
'label' => 'John Doe (john@doe.info)',
'email' => 'john@doe.info',
'photo' => null,
],
[
'id' => 'jd',
'label' => 'John Doe (doe@john.info)',
'email' => 'doe@john.info',
'id' => 'jf',
'label' => 'Jonathan Frakes (jonathan@frakes.com)',
'email' => 'jonathan@frakes.com',
'photo' => null,
],
];

$actual = $this->contactsIntegration->getMatchingRecipient("", $term);
$actual = $this->contactsIntegration->getMatchingRecipient("auser", $term);

$this->assertEquals($expected, $actual);
}

public function testGetMatchingRecipientRestrictedToFullMatchUserId() {
$term = 'jd'; // searching for: John Doe
public function testGetMatchingRecipientRestrictedToGroupFullMatchEmail() {
$term = 'jonathan@frakes.com'; // searching for: Jonathan Frakes
$searchResult = [
[
// Array of addresses
'UID' => 'jd',
'FN' => 'John Doe',
'EMAIL' => [
'john@doe.info',
'doe@john.info',
],
// Simple match
'UID' => 'jf',
'FN' => 'Jonathan Frakes',
'EMAIL' => 'jonathan@frakes.com',
'isLocalSystemBook' => true,
]
],
];

$this->common($term, $searchResult, true, false, true);
$this->common($term, $searchResult, true, true, true);
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->with('auser')
->will($this->returnValue($user));
$this->groupManager->expects($this->once())
->method('getUserGroupIds')
->will($this->returnValue(['agroup']));
$this->groupManager->expects(self::exactly(1))
->method('isInGroup')
->withConsecutive(['jf', 'agroup'])
->willReturnOnConsecutiveCalls(false);

$expected = [
[
'id' => 'jd',
'label' => 'John Doe (john@doe.info)',
'email' => 'john@doe.info',
'photo' => null,
],
[
'id' => 'jd',
'label' => 'John Doe (doe@john.info)',
'email' => 'doe@john.info',
'id' => 'jf',
'label' => 'Jonathan Frakes (jonathan@frakes.com)',
'email' => 'jonathan@frakes.com',
'photo' => null,
],
];

$actual = $this->contactsIntegration->getMatchingRecipient("", $term);
$actual = $this->contactsIntegration->getMatchingRecipient("auser", $term);

$this->assertEquals($expected, $actual);
}

public function testGetMatchingRecipientRestrictedToGroupFullMatchFalse() {
$term = 'jf'; // searching for: Jonathan Frakes


public function testGetMatchingRecipientRestrictedToFullMatchEmail() {
$term = 'doe@john.info'; // searching for: John Doe
$searchResult = [
[
// Array of addresses
'UID' => 'jd',
'FN' => 'John Doe',
'EMAIL' => [
'john@doe.info',
'doe@john.info',
],
// Simple match
'UID' => 'jf',
'FN' => 'Jonathan Frakes',
'EMAIL' => 'jonathan@frakes.com',
'isLocalSystemBook' => true,
]
],
];

$this->common($term, $searchResult, true, false, true);
$this->common($term, $searchResult, true, true, false, false, false);

$expected = [
[
'id' => 'jd',
'label' => 'John Doe (doe@john.info)',
'email' => 'doe@john.info',
'photo' => null,
],
];
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->with('auser')
->will($this->returnValue($user));
$this->groupManager->expects($this->once())
->method('getUserGroupIds')
->will($this->returnValue(['agroup']));
$this->groupManager->expects(self::exactly(1))
->method('isInGroup')
->withConsecutive(['jf', 'agroup'])
->willReturnOnConsecutiveCalls(false);

$actual = $this->contactsIntegration->getMatchingRecipient("", $term);
$expected = [];

$actual = $this->contactsIntegration->getMatchingRecipient("auser", $term);
$this->assertEquals($expected, $actual);
}

Expand All @@ -359,7 +366,7 @@ public function common($term, $searchResult, $allowSystemUsers, $allowSystemUser
->will($this->returnValue(true));
$this->contactsManager->expects($this->once())
->method('search')
->with($term, ['UID', 'FN', 'EMAIL'], ['enumeration' => $allowSystemUsers])
->with($term, ['UID', 'FN', 'EMAIL'], ['enumeration' => $allowSystemUsers, 'fullmatch' => $shareeEnumerationFullMatch])
->will($this->returnValue($searchResult));
}

Expand Down
Loading