createaddressbook.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\DAV\Command;
  23. use OCA\DAV\CardDAV\CardDavBackend;
  24. use OCA\DAV\Connector\Sabre\Principal;
  25. use OCP\IConfig;
  26. use OCP\IDBConnection;
  27. use OCP\IGroupManager;
  28. use OCP\ILogger;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class CreateAddressBook extends Command {
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var \OCP\IDBConnection */
  38. protected $dbConnection;
  39. /** @var ILogger */
  40. private $logger;
  41. /** @var IGroupManager $groupManager */
  42. private $groupManager;
  43. /**
  44. * @param IUserManager $userManager
  45. * @param IDBConnection $dbConnection
  46. * @param IConfig $config
  47. * @param ILogger $logger
  48. */
  49. function __construct(IUserManager $userManager,
  50. IGroupManager $groupManager,
  51. IDBConnection $dbConnection,
  52. ILogger $logger
  53. ) {
  54. parent::__construct();
  55. $this->userManager = $userManager;
  56. $this->groupManager = $groupManager;
  57. $this->dbConnection = $dbConnection;
  58. $this->logger = $logger;
  59. }
  60. protected function configure() {
  61. $this
  62. ->setName('dav:create-addressbook')
  63. ->setDescription('Create a dav addressbook')
  64. ->addArgument('user',
  65. InputArgument::REQUIRED,
  66. 'User for whom the addressbook will be created')
  67. ->addArgument('name',
  68. InputArgument::REQUIRED,
  69. 'Name of the addressbook');
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output) {
  72. $user = $input->getArgument('user');
  73. if (!$this->userManager->userExists($user)) {
  74. throw new \InvalidArgumentException("User <$user> in unknown.");
  75. }
  76. $principalBackend = new Principal(
  77. $this->userManager,
  78. $this->groupManager
  79. );
  80. $name = $input->getArgument('name');
  81. $carddav = new CardDavBackend($this->dbConnection, $principalBackend, $this->logger);
  82. $carddav->createAddressBook("principals/users/$user", $name, []);
  83. }
  84. }