CreateCalendar.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Command;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  30. use OCA\DAV\Connector\Sabre\Principal;
  31. use OCP\IDBConnection;
  32. use OCP\IGroupManager;
  33. use OCP\IUserManager;
  34. use Symfony\Component\Console\Command\Command;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. class CreateCalendar extends Command {
  39. /** @var IUserManager */
  40. protected $userManager;
  41. /** @var IGroupManager $groupManager */
  42. private $groupManager;
  43. /** @var \OCP\IDBConnection */
  44. protected $dbConnection;
  45. /**
  46. * @param IUserManager $userManager
  47. * @param IGroupManager $groupManager
  48. * @param IDBConnection $dbConnection
  49. */
  50. public function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection) {
  51. parent::__construct();
  52. $this->userManager = $userManager;
  53. $this->groupManager = $groupManager;
  54. $this->dbConnection = $dbConnection;
  55. }
  56. protected function configure() {
  57. $this
  58. ->setName('dav:create-calendar')
  59. ->setDescription('Create a dav calendar')
  60. ->addArgument('user',
  61. InputArgument::REQUIRED,
  62. 'User for whom the calendar will be created')
  63. ->addArgument('name',
  64. InputArgument::REQUIRED,
  65. 'Name of the calendar');
  66. }
  67. protected function execute(InputInterface $input, OutputInterface $output) {
  68. $user = $input->getArgument('user');
  69. if (!$this->userManager->userExists($user)) {
  70. throw new \InvalidArgumentException("User <$user> in unknown.");
  71. }
  72. $principalBackend = new Principal(
  73. $this->userManager,
  74. $this->groupManager,
  75. \OC::$server->getShareManager(),
  76. \OC::$server->getUserSession(),
  77. \OC::$server->getAppManager(),
  78. \OC::$server->query(ProxyMapper::class),
  79. \OC::$server->getConfig()
  80. );
  81. $random = \OC::$server->getSecureRandom();
  82. $logger = \OC::$server->getLogger();
  83. $dispatcher = \OC::$server->getEventDispatcher();
  84. $name = $input->getArgument('name');
  85. $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $this->groupManager, $random, $logger, $dispatcher);
  86. $caldav->createCalendar("principals/users/$user", $name, []);
  87. }
  88. }