ListCalendars.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Georg Ehrke
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Thomas Citharel <tcit@tcit.fr>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Command;
  24. use OCA\DAV\CalDAV\BirthdayService;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCA\DAV\Connector\Sabre\Principal;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  29. use OCP\IGroupManager;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use OCP\Share\IManager;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Helper\Table;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. class ListCalendars extends Command {
  39. /** @var IUserManager */
  40. protected $userManager;
  41. /** @var CalDavBackend */
  42. private $caldav;
  43. /**
  44. * @param IUserManager $userManager
  45. * @param CalDavBackend $caldav
  46. */
  47. function __construct(IUserManager $userManager, CalDavBackend $caldav) {
  48. parent::__construct();
  49. $this->userManager = $userManager;
  50. $this->caldav = $caldav;
  51. }
  52. protected function configure() {
  53. $this
  54. ->setName('dav:list-calendars')
  55. ->setDescription('List all calendars of a user')
  56. ->addArgument('uid',
  57. InputArgument::REQUIRED,
  58. 'User for whom all calendars will be listed');
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output) {
  61. $user = $input->getArgument('uid');
  62. if (!$this->userManager->userExists($user)) {
  63. throw new \InvalidArgumentException("User <$user> is unknown.");
  64. }
  65. $calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
  66. $calendarTableData = [];
  67. foreach($calendars as $calendar) {
  68. // skip birthday calendar
  69. if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  70. continue;
  71. }
  72. $readOnly = false;
  73. $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
  74. if (isset($calendar[$readOnlyIndex])) {
  75. $readOnly = $calendar[$readOnlyIndex];
  76. }
  77. $calendarTableData[] = [
  78. $calendar['uri'],
  79. $calendar['{DAV:}displayname'],
  80. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
  81. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
  82. $readOnly ? ' x ' : ' ✓ ',
  83. ];
  84. }
  85. if (count($calendarTableData) > 0) {
  86. $table = new Table($output);
  87. $table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
  88. ->setRows($calendarTableData);
  89. $table->render();
  90. } else {
  91. $output->writeln("<info>User <$user> has no calendars</info>");
  92. }
  93. }
  94. }