From 5e23800a95290f5e4ca4d5eac8fa222b1ca34a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Wed, 20 Oct 2021 10:47:23 +0200 Subject: [PATCH] Do not remove current user on findOne MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- .../Contacts/ContactsMenu/ContactsStore.php | 28 +++++++++---------- .../ContactsMenu/ContactsStoreTest.php | 27 +++++++++--------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/private/Contacts/ContactsMenu/ContactsStore.php b/lib/private/Contacts/ContactsMenu/ContactsStore.php index a4a53bf877448..eb7e752a87a60 100644 --- a/lib/private/Contacts/ContactsMenu/ContactsStore.php +++ b/lib/private/Contacts/ContactsMenu/ContactsStore.php @@ -113,9 +113,18 @@ public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offs $options ); + $userId = $user->getUID(); + $contacts = array_filter($allContacts, function ($contact) use ($userId) { + // When searching for multiple results, we strip out the current user + if (array_key_exists('UID', $contact)) { + return $contact['UID'] !== $userId; + } + return true; + }); + $entries = array_map(function (array $contact) { return $this->contactArrayToEntry($contact); - }, $allContacts); + }, $contacts); return $this->filterContacts( $user, $entries, @@ -125,12 +134,11 @@ public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offs /** * Filters the contacts. Applied filters: - * 1. filter the current user - * 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is + * 1. if the `shareapi_allow_share_dialog_user_enumeration` config option is * enabled it will filter all local users - * 3. if the `shareapi_exclude_groups` config option is enabled and the + * 2. if the `shareapi_exclude_groups` config option is enabled and the * current user is in an excluded group it will filter all local users. - * 4. if the `shareapi_only_share_with_group_members` config option is + * 3. if the `shareapi_only_share_with_group_members` config option is * enabled it will filter all users which doens't have a common group * with the current user. * @@ -171,10 +179,6 @@ private function filterContacts( $selfUID = $self->getUID(); return array_values(array_filter($entries, function (IEntry $entry) use ($skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $restrictEnumerationGroup, $restrictEnumerationPhone, $allowEnumerationFullMatch, $filter) { - if ($entry->getProperty('UID') === $selfUID) { - return false; - } - if ($entry->getProperty('isLocalSystemBook')) { if ($skipLocal) { return false; @@ -266,11 +270,7 @@ public function findOne(IUser $user, $shareType, $shareWith) { return null; } - $userId = $user->getUID(); - $allContacts = $this->contactsManager->search($shareWith, $filter); - $contacts = array_filter($allContacts, function ($contact) use ($userId) { - return $contact['UID'] !== $userId; - }); + $contacts = $this->contactsManager->search($shareWith, $filter); $match = null; foreach ($contacts as $contact) { diff --git a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php index 13cc7575f43a8..98a31254c3e1f 100644 --- a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php +++ b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php @@ -99,7 +99,7 @@ public function testGetContactsWithoutFilter() { ], ], ]); - $user->expects($this->once()) + $user->expects($this->exactly(2)) ->method('getUID') ->willReturn('user123'); @@ -129,7 +129,7 @@ public function testGetContactsHidesOwnEntry() { ], ], ]); - $user->expects($this->once()) + $user->expects($this->exactly(2)) ->method('getUID') ->willReturn('user123'); @@ -157,7 +157,7 @@ public function testGetContactsWithoutBinaryImage() { 'PHOTO' => base64_encode('photophotophoto'), ], ]); - $user->expects($this->once()) + $user->expects($this->exactly(2)) ->method('getUID') ->willReturn('user123'); @@ -186,7 +186,7 @@ public function testGetContactsWithoutAvatarURI() { 'PHOTO' => 'VALUE=uri:https://photo', ], ]); - $user->expects($this->once()) + $user->expects($this->exactly(2)) ->method('getUID') ->willReturn('user123'); @@ -210,7 +210,7 @@ public function testGetContactsWhenUserIsInExcludeGroups() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -253,7 +253,7 @@ public function testGetContactsOnlyShareIfInTheSameGroup() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -332,7 +332,7 @@ public function testGetContactsOnlyEnumerateIfInTheSameGroup() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -411,7 +411,7 @@ public function testGetContactsOnlyEnumerateIfPhoneBookMatch() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -469,7 +469,7 @@ public function testGetContactsOnlyEnumerateIfPhoneBookMatchWithOwnGroupsOnly() /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -555,7 +555,7 @@ public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroup() { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -624,7 +624,7 @@ public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroupInOwnGroupsOnl /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); - $currentUser->expects($this->once()) + $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); @@ -963,9 +963,8 @@ public function testFindOneNoMatches() { 'isLocalSystemBook' => false ], ]); - $user->expects($this->once()) - ->method('getUID') - ->willReturn('user123'); + $user->expects($this->never()) + ->method('getUID'); $entry = $this->contactsStore->findOne($user, 0, 'a567');