CreateAddressBook.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Command;
  8. use OCA\DAV\CardDAV\CardDavBackend;
  9. use OCP\IUserManager;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class CreateAddressBook extends Command {
  15. public function __construct(
  16. private IUserManager $userManager,
  17. private CardDavBackend $cardDavBackend,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure(): void {
  22. $this
  23. ->setName('dav:create-addressbook')
  24. ->setDescription('Create a dav addressbook')
  25. ->addArgument('user',
  26. InputArgument::REQUIRED,
  27. 'User for whom the addressbook will be created')
  28. ->addArgument('name',
  29. InputArgument::REQUIRED,
  30. 'Name of the addressbook');
  31. }
  32. protected function execute(InputInterface $input, OutputInterface $output): int {
  33. $user = $input->getArgument('user');
  34. if (!$this->userManager->userExists($user)) {
  35. throw new \InvalidArgumentException("User <$user> in unknown.");
  36. }
  37. $name = $input->getArgument('name');
  38. $this->cardDavBackend->createAddressBook("principals/users/$user", $name, []);
  39. return self::SUCCESS;
  40. }
  41. }