InvitationResponseServer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\InvitationResponse;
  7. use OCA\DAV\AppInfo\PluginManager;
  8. use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
  9. use OCA\DAV\CalDAV\Auth\PublicPrincipalPlugin;
  10. use OCA\DAV\CalDAV\DefaultCalendarValidator;
  11. use OCA\DAV\CalDAV\Publishing\PublishPlugin;
  12. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  13. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  14. use OCA\DAV\Connector\Sabre\CachingTree;
  15. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  16. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  17. use OCA\DAV\Connector\Sabre\LockPlugin;
  18. use OCA\DAV\Connector\Sabre\MaintenancePlugin;
  19. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  20. use OCA\DAV\RootCollection;
  21. use OCA\Theming\ThemingDefaults;
  22. use OCP\EventDispatcher\IEventDispatcher;
  23. use OCP\IConfig;
  24. use OCP\Server;
  25. use Psr\Log\LoggerInterface;
  26. use Sabre\VObject\ITip\Message;
  27. class InvitationResponseServer {
  28. /** @var \OCA\DAV\Connector\Sabre\Server */
  29. public $server;
  30. /**
  31. * InvitationResponseServer constructor.
  32. */
  33. public function __construct(bool $public = true) {
  34. $baseUri = \OC::$WEBROOT . '/remote.php/dav/';
  35. $logger = Server::get(LoggerInterface::class);
  36. $dispatcher = Server::get(IEventDispatcher::class);
  37. $root = new RootCollection();
  38. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  39. // Add maintenance plugin
  40. $this->server->addPlugin(new MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  41. // Set URL explicitly due to reverse-proxy situations
  42. $this->server->httpRequest->setUrl($baseUri);
  43. $this->server->setBaseUri($baseUri);
  44. $this->server->addPlugin(new BlockLegacyClientPlugin(
  45. Server::get(IConfig::class),
  46. Server::get(ThemingDefaults::class),
  47. ));
  48. $this->server->addPlugin(new AnonymousOptionsPlugin());
  49. // allow custom principal uri option
  50. if ($public) {
  51. $this->server->addPlugin(new PublicPrincipalPlugin());
  52. } else {
  53. $this->server->addPlugin(new CustomPrincipalPlugin());
  54. }
  55. // allow setup of additional auth backends
  56. $event = new SabrePluginAuthInitEvent($this->server);
  57. $dispatcher->dispatchTyped($event);
  58. $this->server->addPlugin(new ExceptionLoggerPlugin('webdav', $logger));
  59. $this->server->addPlugin(new LockPlugin());
  60. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  61. // acl
  62. $acl = new DavAclPlugin();
  63. $acl->principalCollectionSet = [
  64. 'principals/users', 'principals/groups'
  65. ];
  66. $this->server->addPlugin($acl);
  67. // calendar plugins
  68. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  69. $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
  70. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DefaultCalendarValidator::class)));
  71. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  72. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  73. //$this->server->addPlugin(new \OCA\DAV\DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
  74. $this->server->addPlugin(new PublishPlugin(
  75. \OC::$server->getConfig(),
  76. \OC::$server->getURLGenerator()
  77. ));
  78. // wait with registering these until auth is handled and the filesystem is setup
  79. $this->server->on('beforeMethod:*', function () use ($root): void {
  80. // register plugins from apps
  81. $pluginManager = new PluginManager(
  82. \OC::$server,
  83. \OC::$server->getAppManager()
  84. );
  85. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  86. $this->server->addPlugin($appPlugin);
  87. }
  88. foreach ($pluginManager->getAppCollections() as $appCollection) {
  89. $root->addChild($appCollection);
  90. }
  91. });
  92. }
  93. /**
  94. * @param Message $iTipMessage
  95. * @return void
  96. */
  97. public function handleITipMessage(Message $iTipMessage) {
  98. /** @var \OCA\DAV\CalDAV\Schedule\Plugin $schedulingPlugin */
  99. $schedulingPlugin = $this->server->getPlugin('caldav-schedule');
  100. $schedulingPlugin->scheduleLocalDelivery($iTipMessage);
  101. }
  102. public function isExternalAttendee(string $principalUri): bool {
  103. /** @var \Sabre\DAVACL\Plugin $aclPlugin */
  104. $aclPlugin = $this->getServer()->getPlugin('acl');
  105. return $aclPlugin->getPrincipalByUri($principalUri) === null;
  106. }
  107. public function getServer(): \OCA\DAV\Connector\Sabre\Server {
  108. return $this->server;
  109. }
  110. }