ListCalendars.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Georg Ehrke
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Command;
  27. use OCA\DAV\CalDAV\BirthdayService;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\Table;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class ListCalendars extends Command {
  36. /** @var IUserManager */
  37. protected $userManager;
  38. /** @var CalDavBackend */
  39. private $caldav;
  40. /**
  41. * @param IUserManager $userManager
  42. * @param CalDavBackend $caldav
  43. */
  44. public function __construct(IUserManager $userManager, CalDavBackend $caldav) {
  45. parent::__construct();
  46. $this->userManager = $userManager;
  47. $this->caldav = $caldav;
  48. }
  49. protected function configure() {
  50. $this
  51. ->setName('dav:list-calendars')
  52. ->setDescription('List all calendars of a user')
  53. ->addArgument('uid',
  54. InputArgument::REQUIRED,
  55. 'User for whom all calendars will be listed');
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output) {
  58. $user = $input->getArgument('uid');
  59. if (!$this->userManager->userExists($user)) {
  60. throw new \InvalidArgumentException("User <$user> is unknown.");
  61. }
  62. $calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
  63. $calendarTableData = [];
  64. foreach ($calendars as $calendar) {
  65. // skip birthday calendar
  66. if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  67. continue;
  68. }
  69. $readOnly = false;
  70. $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
  71. if (isset($calendar[$readOnlyIndex])) {
  72. $readOnly = $calendar[$readOnlyIndex];
  73. }
  74. $calendarTableData[] = [
  75. $calendar['uri'],
  76. $calendar['{DAV:}displayname'],
  77. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
  78. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
  79. $readOnly ? ' x ' : ' ✓ ',
  80. ];
  81. }
  82. if (count($calendarTableData) > 0) {
  83. $table = new Table($output);
  84. $table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
  85. ->setRows($calendarTableData);
  86. $table->render();
  87. } else {
  88. $output->writeln("<info>User <$user> has no calendars</info>");
  89. }
  90. }
  91. }