1
0

CreateCalendar.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 OC\KnownUser\KnownUserService;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  11. use OCA\DAV\CalDAV\Sharing\Backend;
  12. use OCA\DAV\Connector\Sabre\Principal;
  13. use OCP\Accounts\IAccountManager;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\IConfig;
  16. use OCP\IDBConnection;
  17. use OCP\IGroupManager;
  18. use OCP\IUserManager;
  19. use Psr\Log\LoggerInterface;
  20. use Symfony\Component\Console\Command\Command;
  21. use Symfony\Component\Console\Input\InputArgument;
  22. use Symfony\Component\Console\Input\InputInterface;
  23. use Symfony\Component\Console\Output\OutputInterface;
  24. class CreateCalendar extends Command {
  25. public function __construct(
  26. protected IUserManager $userManager,
  27. private IGroupManager $groupManager,
  28. protected IDBConnection $dbConnection,
  29. ) {
  30. parent::__construct();
  31. }
  32. protected function configure(): void {
  33. $this
  34. ->setName('dav:create-calendar')
  35. ->setDescription('Create a dav calendar')
  36. ->addArgument('user',
  37. InputArgument::REQUIRED,
  38. 'User for whom the calendar will be created')
  39. ->addArgument('name',
  40. InputArgument::REQUIRED,
  41. 'Name of the calendar');
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. $user = $input->getArgument('user');
  45. if (!$this->userManager->userExists($user)) {
  46. throw new \InvalidArgumentException("User <$user> in unknown.");
  47. }
  48. $principalBackend = new Principal(
  49. $this->userManager,
  50. $this->groupManager,
  51. \OC::$server->get(IAccountManager::class),
  52. \OC::$server->getShareManager(),
  53. \OC::$server->getUserSession(),
  54. \OC::$server->getAppManager(),
  55. \OC::$server->query(ProxyMapper::class),
  56. \OC::$server->get(KnownUserService::class),
  57. \OC::$server->getConfig(),
  58. \OC::$server->getL10NFactory(),
  59. );
  60. $random = \OC::$server->getSecureRandom();
  61. $logger = \OC::$server->get(LoggerInterface::class);
  62. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  63. $config = \OC::$server->get(IConfig::class);
  64. $name = $input->getArgument('name');
  65. $caldav = new CalDavBackend(
  66. $this->dbConnection,
  67. $principalBackend,
  68. $this->userManager,
  69. $random,
  70. $logger,
  71. $dispatcher,
  72. $config,
  73. \OC::$server->get(Backend::class),
  74. );
  75. $caldav->createCalendar("principals/users/$user", $name, []);
  76. return self::SUCCESS;
  77. }
  78. }