Repository navigation
fix(contactsinteraction): skip recent contacts visible in system addr… #65812
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
Open
hamza221
wants to merge
1
commit into
master
Choose a base branch
from
fix/sab-recently-contacted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
apps/contactsinteraction/tests/Db/CardSearchDaoTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContactsInteraction\Tests\Db; | ||
|
|
||
| use OCA\ContactsInteraction\Db\CardSearchDao; | ||
| use OCA\DAV\CardDAV\CardDavBackend; | ||
| use OCA\DAV\CardDAV\SystemAddressBookVisibility; | ||
| use OCP\IDBConnection; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\Server; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Sabre\VObject\Component\VCard; | ||
| use Sabre\VObject\UUIDUtil; | ||
| use Test\TestCase; | ||
|
|
||
| #[\PHPUnit\Framework\Attributes\Group(name: 'DB')] | ||
| class CardSearchDaoTest extends TestCase { | ||
| private CardDavBackend $backend; | ||
| private IUserManager&MockObject $userManager; | ||
| private SystemAddressBookVisibility&MockObject $visibility; | ||
| private CardSearchDao $dao; | ||
| private IUser&MockObject $actor; | ||
| private int $systemAddressBookId; | ||
| private bool $createdSystemAddressBook = false; | ||
| private ?int $userAddressBookId = null; | ||
| /** @var list<array{int, string}> */ | ||
| private array $createdCards = []; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->backend = Server::get(CardDavBackend::class); | ||
| $this->userManager = $this->createMock(IUserManager::class); | ||
| $this->visibility = $this->createMock(SystemAddressBookVisibility::class); | ||
| $this->dao = new CardSearchDao(Server::get(IDBConnection::class), $this->userManager, $this->visibility); | ||
|
|
||
| $this->actor = $this->createMock(IUser::class); | ||
| $this->actor->method('getUID')->willReturn('card-search-dao-actor'); | ||
|
|
||
| $systemAddressBook = $this->backend->getAddressBooksByUri('principals/system/system', 'system'); | ||
| if ($systemAddressBook === null) { | ||
| $this->systemAddressBookId = $this->backend->createAddressBook('principals/system/system', 'system', []); | ||
| $this->createdSystemAddressBook = true; | ||
| } else { | ||
| $this->systemAddressBookId = (int)$systemAddressBook['id']; | ||
| } | ||
| } | ||
|
|
||
| protected function tearDown(): void { | ||
| foreach ($this->createdCards as [$addressBookId, $uri]) { | ||
| $this->backend->deleteCard($addressBookId, $uri); | ||
| } | ||
| if ($this->userAddressBookId !== null) { | ||
| $this->backend->deleteAddressBook($this->userAddressBookId); | ||
| } | ||
| if ($this->createdSystemAddressBook) { | ||
| $this->backend->deleteAddressBook($this->systemAddressBookId); | ||
| } | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| private function createCard(int $addressBookId, string $uid, string $email): string { | ||
| $cardData = (new VCard([ | ||
| 'UID' => $uid, | ||
| 'FN' => $uid, | ||
| 'EMAIL' => $email, | ||
| ]))->serialize(); | ||
| $uri = UUIDUtil::getUUID() . '.vcf'; | ||
| $this->backend->createCard($addressBookId, $uri, $cardData); | ||
| $this->createdCards[] = [$addressBookId, $uri]; | ||
| return $cardData; | ||
| } | ||
|
|
||
| private function mockTargetUser(string $uid): IUser&MockObject { | ||
| $target = $this->createMock(IUser::class); | ||
| $target->method('getUID')->willReturn($uid); | ||
| $this->userManager->method('get')->with($uid)->willReturn($target); | ||
| return $target; | ||
| } | ||
|
|
||
| public function testFindExistingInOwnAddressBook(): void { | ||
| $this->userAddressBookId = $this->backend->createAddressBook('principals/users/card-search-dao-actor', 'contacts', []); | ||
| $uid = UUIDUtil::getUUID(); | ||
| $cardData = $this->createCard($this->userAddressBookId, $uid, $uid . '@example.com'); | ||
| $this->visibility->expects(self::never())->method('isUserCardVisible'); | ||
|
|
||
| $this->assertSame($cardData, $this->dao->findExisting($this->actor, null, $uid . '@example.com', null)); | ||
| } | ||
|
|
||
| public function testFindExistingInSystemAddressBookWhenVisible(): void { | ||
| $uid = UUIDUtil::getUUID(); | ||
| $cardData = $this->createCard($this->systemAddressBookId, $uid, $uid . '@example.com'); | ||
| $target = $this->mockTargetUser($uid); | ||
| $this->visibility->expects(self::once()) | ||
| ->method('isUserCardVisible') | ||
| ->with($this->actor, $target) | ||
| ->willReturn(true); | ||
|
|
||
| $this->assertSame($cardData, $this->dao->findExisting($this->actor, $uid, null, null)); | ||
| } | ||
|
|
||
| public function testFindExistingIgnoresSystemAddressBookCardWhenNotVisible(): void { | ||
| $uid = UUIDUtil::getUUID(); | ||
| $this->createCard($this->systemAddressBookId, $uid, $uid . '@example.com'); | ||
| $target = $this->mockTargetUser($uid); | ||
| $this->visibility->expects(self::once()) | ||
| ->method('isUserCardVisible') | ||
| ->with($this->actor, $target) | ||
| ->willReturn(false); | ||
|
|
||
| $this->assertNull($this->dao->findExisting($this->actor, null, $uid . '@example.com', null)); | ||
| } | ||
|
|
||
| public function testFindExistingIgnoresSystemAddressBookCardOfUnknownUser(): void { | ||
| $uid = UUIDUtil::getUUID(); | ||
| $this->createCard($this->systemAddressBookId, $uid, $uid . '@example.com'); | ||
| $this->userManager->method('get')->with($uid)->willReturn(null); | ||
| $this->visibility->expects(self::never())->method('isUserCardVisible'); | ||
|
|
||
| $this->assertNull($this->dao->findExisting($this->actor, $uid, null, null)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||
| <?php | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| /** | ||||||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||||||
| */ | ||||||
|
|
||||||
| namespace OCA\DAV\CardDAV; | ||||||
|
|
||||||
| use OCA\DAV\AppInfo\Application; | ||||||
| use OCA\DAV\ConfigLexicon; | ||||||
| use OCP\IAppConfig; | ||||||
| use OCP\IGroupManager; | ||||||
| use OCP\IUser; | ||||||
| use function array_intersect; | ||||||
|
|
||||||
| /** | ||||||
| * Applies the access rules of {@see SystemAddressbook} for a given viewer | ||||||
| * outside of a DAV request. | ||||||
| */ | ||||||
| class SystemAddressBookVisibility { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To stay in sync with the SystemAddressbook class. |
||||||
| public function __construct( | ||||||
| private IAppConfig $appConfig, | ||||||
| private IGroupManager $groupManager, | ||||||
| ) { | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Whether $viewer can see the system address book card of $target | ||||||
| * This depends on things such as the system address book being exposed, | ||||||
| * the viewer and target not being guests, and the share enumeration settings. | ||||||
| */ | ||||||
| public function isUserCardVisible(IUser $viewer, IUser $target): bool { | ||||||
| if (!$this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED)) { | ||||||
| return false; | ||||||
| } | ||||||
| if ($viewer->getUID() === $target->getUID()) { | ||||||
| return true; | ||||||
| } | ||||||
| if ($viewer->getBackendClassName() === 'Guests' || $target->getBackendClassName() === 'Guests') { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| $shareEnumeration = $this->appConfig->getValueString('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; | ||||||
| $shareEnumerationGroup = $this->appConfig->getValueString('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; | ||||||
| $shareEnumerationPhone = $this->appConfig->getValueString('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; | ||||||
| if (!$shareEnumeration || (!$shareEnumerationGroup && $shareEnumerationPhone)) { | ||||||
| return false; | ||||||
| } | ||||||
| if ($shareEnumerationGroup) { | ||||||
| return array_intersect( | ||||||
| $this->groupManager->getUserGroupIds($viewer), | ||||||
| $this->groupManager->getUserGroupIds($target), | ||||||
| ) !== []; | ||||||
| } | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would prefer SystemAddressbookAccess 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually visibility ( visibility in contacts app) you can have access but not be visible in contacts app