-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(carddav): add command to list address books #47788
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
Merged
Merged
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
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,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\Command; | ||
|
|
||
| use OCA\DAV\CardDAV\CardDavBackend; | ||
| use OCA\DAV\CardDAV\SystemAddressbook; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\Table; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class ListAddressbooks extends Command { | ||
| public function __construct( | ||
| protected IUserManager $userManager, | ||
| private CardDavBackend $cardDavBackend, | ||
| ) { | ||
| parent::__construct('dav:list-addressbooks'); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $this | ||
| ->setDescription('List all addressbooks of a user') | ||
| ->addArgument('uid', | ||
| InputArgument::REQUIRED, | ||
| 'User for whom all addressbooks will be listed'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $user = $input->getArgument('uid'); | ||
| if (!$this->userManager->userExists($user)) { | ||
| throw new \InvalidArgumentException("User <$user> is unknown."); | ||
| } | ||
|
|
||
| $addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$user"); | ||
|
|
||
| $addressBookTableData = []; | ||
| foreach ($addressBooks as $book) { | ||
| // skip system / contacts integration address book | ||
| if ($book['uri'] === SystemAddressbook::URI_SHARED) { | ||
| continue; | ||
| } | ||
|
|
||
| $readOnly = false; | ||
| $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; | ||
| if (isset($book[$readOnlyIndex])) { | ||
| $readOnly = $book[$readOnlyIndex]; | ||
| } | ||
|
|
||
| $addressBookTableData[] = [ | ||
| $book['uri'], | ||
| $book['{DAV:}displayname'], | ||
| $book['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'] ?? $book['principaluri'], | ||
| $book['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'], | ||
| $readOnly ? ' x ' : ' ✓ ', | ||
| ]; | ||
| } | ||
|
|
||
| if (count($addressBookTableData) > 0) { | ||
| $table = new Table($output); | ||
| $table->setHeaders(['Database ID', 'URI', 'Displayname', 'Owner principal', 'Owner displayname', 'Writable']) | ||
| ->setRows($addressBookTableData); | ||
|
|
||
| $table->render(); | ||
| } else { | ||
| $output->writeln("<info>User <$user> has no addressbooks</info>"); | ||
| } | ||
| return self::SUCCESS; | ||
| } | ||
| } | ||
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,110 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\Tests\Command; | ||
|
|
||
| use OCA\DAV\CardDAV\CardDavBackend; | ||
| use OCA\DAV\Command\ListAddressbooks; | ||
| use OCP\IUserManager; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Symfony\Component\Console\Tester\CommandTester; | ||
| use Test\TestCase; | ||
|
|
||
| /** | ||
| * Class ListCalendarsTest | ||
| * | ||
| * @package OCA\DAV\Tests\Command | ||
| */ | ||
| class ListAddressbooksTest extends TestCase { | ||
|
|
||
| private \OCP\IUserManager|MockObject $userManager; | ||
| private CardDavBackend|MockObject $cardDavBackend; | ||
| private ListAddressbooks $command; | ||
|
|
||
| public const USERNAME = 'username'; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->userManager = $this->createMock(IUserManager::class); | ||
| $this->cardDavBackend = $this->createMock(CardDavBackend::class); | ||
|
|
||
| $this->command = new ListAddressbooks( | ||
| $this->userManager, | ||
| $this->cardDavBackend | ||
| ); | ||
| } | ||
|
|
||
| public function testWithBadUser(): void { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
|
|
||
| $this->userManager->expects($this->once()) | ||
| ->method('userExists') | ||
| ->with(self::USERNAME) | ||
| ->willReturn(false); | ||
|
|
||
| $commandTester = new CommandTester($this->command); | ||
| $commandTester->execute([ | ||
| 'uid' => self::USERNAME, | ||
| ]); | ||
| $this->assertStringContainsString('User <' . self::USERNAME . '> in unknown', $commandTester->getDisplay()); | ||
| } | ||
|
|
||
| public function testWithCorrectUserWithNoCalendars(): void { | ||
| $this->userManager->expects($this->once()) | ||
| ->method('userExists') | ||
| ->with(self::USERNAME) | ||
| ->willReturn(true); | ||
|
|
||
| $this->cardDavBackend->expects($this->once()) | ||
| ->method('getAddressBooksForUser') | ||
| ->with('principals/users/' . self::USERNAME) | ||
| ->willReturn([]); | ||
|
|
||
| $commandTester = new CommandTester($this->command); | ||
| $commandTester->execute([ | ||
| 'uid' => self::USERNAME, | ||
| ]); | ||
| $this->assertStringContainsString('User <' . self::USERNAME . "> has no addressbooks\n", $commandTester->getDisplay()); | ||
| } | ||
|
|
||
| public function dataExecute() { | ||
| return [ | ||
| [false, '✓'], | ||
| [true, 'x'] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider dataExecute | ||
| */ | ||
| public function testWithCorrectUser(bool $readOnly, string $output): void { | ||
| $this->userManager->expects($this->once()) | ||
| ->method('userExists') | ||
| ->with(self::USERNAME) | ||
| ->willReturn(true); | ||
|
|
||
| $this->cardDavBackend->expects($this->once()) | ||
| ->method('getAddressBooksForUser') | ||
| ->with('principals/users/' . self::USERNAME) | ||
| ->willReturn([ | ||
| [ | ||
| '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly, | ||
| 'uri' => 'test', | ||
| '{DAV:}displayname' => 'dp', | ||
| '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal', | ||
| '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp', | ||
| ] | ||
| ]); | ||
|
|
||
| $commandTester = new CommandTester($this->command); | ||
| $commandTester->execute([ | ||
| 'uid' => self::USERNAME, | ||
| ]); | ||
| $this->assertStringContainsString($output, $commandTester->getDisplay()); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.