CreateCalendar.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  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\CalDAV\CalDavBackend;
  24. use OCA\DAV\Connector\Sabre\Principal;
  25. use OCP\IDBConnection;
  26. use OCP\IGroupManager;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class CreateCalendar extends Command {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var IGroupManager $groupManager */
  36. private $groupManager;
  37. /** @var \OCP\IDBConnection */
  38. protected $dbConnection;
  39. /**
  40. * @param IUserManager $userManager
  41. * @param IGroupManager $groupManager
  42. * @param IDBConnection $dbConnection
  43. */
  44. function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection) {
  45. parent::__construct();
  46. $this->userManager = $userManager;
  47. $this->groupManager = $groupManager;
  48. $this->dbConnection = $dbConnection;
  49. }
  50. protected function configure() {
  51. $this
  52. ->setName('dav:create-calendar')
  53. ->setDescription('Create a dav calendar')
  54. ->addArgument('user',
  55. InputArgument::REQUIRED,
  56. 'User for whom the calendar will be created')
  57. ->addArgument('name',
  58. InputArgument::REQUIRED,
  59. 'Name of the calendar');
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output) {
  62. $user = $input->getArgument('user');
  63. if (!$this->userManager->userExists($user)) {
  64. throw new \InvalidArgumentException("User <$user> in unknown.");
  65. }
  66. $principalBackend = new Principal(
  67. $this->userManager,
  68. $this->groupManager
  69. );
  70. $random = \OC::$server->getSecureRandom();
  71. $dispatcher = \OC::$server->getEventDispatcher();
  72. $name = $input->getArgument('name');
  73. $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $random, $dispatcher);
  74. $caldav->createCalendar("principals/users/$user", $name, []);
  75. }
  76. }