CreateCalendar.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Command;
  27. use OC\KnownUser\KnownUserService;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  30. use OCA\DAV\Connector\Sabre\Principal;
  31. use OCP\Accounts\IAccountManager;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\IConfig;
  34. use OCP\IDBConnection;
  35. use OCP\IGroupManager;
  36. use OCP\IUserManager;
  37. use Psr\Log\LoggerInterface;
  38. use Symfony\Component\Console\Command\Command;
  39. use Symfony\Component\Console\Input\InputArgument;
  40. use Symfony\Component\Console\Input\InputInterface;
  41. use Symfony\Component\Console\Output\OutputInterface;
  42. class CreateCalendar extends Command {
  43. /** @var IUserManager */
  44. protected $userManager;
  45. /** @var IGroupManager $groupManager */
  46. private $groupManager;
  47. /** @var \OCP\IDBConnection */
  48. protected $dbConnection;
  49. /**
  50. * @param IUserManager $userManager
  51. * @param IGroupManager $groupManager
  52. * @param IDBConnection $dbConnection
  53. */
  54. public function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection) {
  55. parent::__construct();
  56. $this->userManager = $userManager;
  57. $this->groupManager = $groupManager;
  58. $this->dbConnection = $dbConnection;
  59. }
  60. protected function configure() {
  61. $this
  62. ->setName('dav:create-calendar')
  63. ->setDescription('Create a dav calendar')
  64. ->addArgument('user',
  65. InputArgument::REQUIRED,
  66. 'User for whom the calendar will be created')
  67. ->addArgument('name',
  68. InputArgument::REQUIRED,
  69. 'Name of the calendar');
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  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. \OC::$server->get(IAccountManager::class),
  80. \OC::$server->getShareManager(),
  81. \OC::$server->getUserSession(),
  82. \OC::$server->getAppManager(),
  83. \OC::$server->query(ProxyMapper::class),
  84. \OC::$server->get(KnownUserService::class),
  85. \OC::$server->getConfig(),
  86. \OC::$server->getL10NFactory(),
  87. );
  88. $random = \OC::$server->getSecureRandom();
  89. $logger = \OC::$server->get(LoggerInterface::class);
  90. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  91. $config = \OC::$server->get(IConfig::class);
  92. $name = $input->getArgument('name');
  93. $caldav = new CalDavBackend(
  94. $this->dbConnection,
  95. $principalBackend,
  96. $this->userManager,
  97. $this->groupManager,
  98. $random,
  99. $logger,
  100. $dispatcher,
  101. $config
  102. );
  103. $caldav->createCalendar("principals/users/$user", $name, []);
  104. return 0;
  105. }
  106. }