|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2022 Thomas Citharel <[email protected]> |
| 4 | + * |
| 5 | + * @author Thomas Citharel <[email protected]> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 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 |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | +namespace OCA\DAV\CalDAV; |
| 24 | + |
| 25 | +use OCP\IConfig; |
| 26 | +use Sabre\CalDAV\Xml\Property\AllowedSharingModes; |
| 27 | +use Sabre\DAV\INode; |
| 28 | +use Sabre\DAV\PropFind; |
| 29 | +use Sabre\DAV\Server; |
| 30 | +use Sabre\DAV\ServerPlugin; |
| 31 | + |
| 32 | +class SharingPlugin extends ServerPlugin { |
| 33 | + public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
| 34 | + |
| 35 | + protected Server $server; |
| 36 | + protected IConfig $config; |
| 37 | + |
| 38 | + public function __construct(IConfig $config) { |
| 39 | + $this->config = $config; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * This method should return a list of server-features. |
| 44 | + * |
| 45 | + * This is for example 'versioning' and is added to the DAV: header |
| 46 | + * in an OPTIONS response. |
| 47 | + * |
| 48 | + * @return string[] |
| 49 | + */ |
| 50 | + public function getFeatures(): array { |
| 51 | + // May have to be changed to be detected |
| 52 | + return ['calendarserver-sharing']; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns a plugin name. |
| 57 | + * |
| 58 | + * Using this name other plugins will be able to access other plugins |
| 59 | + * using Sabre\DAV\Server::getPlugin |
| 60 | + * |
| 61 | + * @return string |
| 62 | + */ |
| 63 | + public function getPluginName(): string { |
| 64 | + return 'oc-calendar-sharing'; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * This initializes the plugin. |
| 69 | + * |
| 70 | + * This function is called by Sabre\DAV\Server, after |
| 71 | + * addPlugin is called. |
| 72 | + * |
| 73 | + * This method should set up the required event subscriptions. |
| 74 | + * |
| 75 | + * @param Server $server |
| 76 | + */ |
| 77 | + public function initialize(Server $server) { |
| 78 | + $this->server = $server; |
| 79 | + |
| 80 | + $this->server->on('propFind', [$this, 'propFind']); |
| 81 | + } |
| 82 | + |
| 83 | + public function propFind(PropFind $propFind, INode $node) { |
| 84 | + if ($node instanceof Calendar) { |
| 85 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function () use ($node) { |
| 86 | + $canShare = (!$node->isSubscription() && $node->canWrite()); |
| 87 | + $canPublish = (!$node->isSubscription() && $node->canWrite()); |
| 88 | + |
| 89 | + if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') { |
| 90 | + $canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI()); |
| 91 | + $canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI()); |
| 92 | + } |
| 93 | + |
| 94 | + if ($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
| 95 | + $canPublish = false; |
| 96 | + } |
| 97 | + |
| 98 | + return new AllowedSharingModes($canShare, $canPublish); |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments