Skip to content

Commit 05df385

Browse files
authored
Merge pull request #22236 from nextcloud/bugfix/noid/dav_sharing_backend_canWrite_isOwner
CalDAV: Add ability to limit sharing to owner
2 parents 92b6740 + 14755d8 commit 05df385

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

apps/dav/lib/CalDAV/Publishing/PublishPlugin.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ public function propFind(PropFind $propFind, INode $node) {
133133
$canShare = (!$node->isSubscription() && $node->canWrite());
134134
$canPublish = (!$node->isSubscription() && $node->canWrite());
135135

136-
return new AllowedSharingModes($canShare, $canPublish);
136+
if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') {
137+
$canShare &= ($node->getOwner() === $node->getPrincipalURI());
138+
$canPublish &= ($node->getOwner() === $node->getPrincipalURI());
139+
}
140+
141+
return new AllowedSharingModes((bool)$canShare, (bool)$canPublish);
137142
});
138143
}
139144
}
@@ -190,7 +195,14 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
190195

191196
// If there's no ACL support, we allow everything
192197
if ($acl) {
198+
/** @var \Sabre\DAVACL\Plugin $acl */
193199
$acl->checkPrivileges($path, '{DAV:}write');
200+
201+
$limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
202+
$isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
203+
if ($limitSharingToOwner && !$isOwner) {
204+
return;
205+
}
194206
}
195207

196208
$node->setPublishStatus(true);
@@ -218,7 +230,14 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
218230

219231
// If there's no ACL support, we allow everything
220232
if ($acl) {
233+
/** @var \Sabre\DAVACL\Plugin $acl */
221234
$acl->checkPrivileges($path, '{DAV:}write');
235+
236+
$limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
237+
$isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
238+
if ($limitSharingToOwner && !$isOwner) {
239+
return;
240+
}
222241
}
223242

224243
$node->setPublishStatus(false);

apps/dav/lib/DAV/Sharing/Plugin.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\DAV\Connector\Sabre\Auth;
2828
use OCA\DAV\DAV\Sharing\Xml\Invite;
2929
use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
30+
use OCP\IConfig;
3031
use OCP\IRequest;
3132
use Sabre\DAV\Exception\NotFound;
3233
use Sabre\DAV\INode;
@@ -46,15 +47,20 @@ class Plugin extends ServerPlugin {
4647
/** @var IRequest */
4748
private $request;
4849

50+
/** @var IConfig */
51+
private $config;
52+
4953
/**
5054
* Plugin constructor.
5155
*
5256
* @param Auth $authBackEnd
5357
* @param IRequest $request
58+
* @param IConfig $config
5459
*/
55-
public function __construct(Auth $authBackEnd, IRequest $request) {
60+
public function __construct(Auth $authBackEnd, IRequest $request, IConfig $config) {
5661
$this->auth = $authBackEnd;
5762
$this->request = $request;
63+
$this->config = $config;
5864
}
5965

6066
/**
@@ -164,6 +170,12 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
164170
if ($acl) {
165171
/** @var \Sabre\DAVACL\Plugin $acl */
166172
$acl->checkPrivileges($path, '{DAV:}write');
173+
174+
$limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
175+
$isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
176+
if ($limitSharingToOwner && !$isOwner) {
177+
return;
178+
}
167179
}
168180

169181
$node->updateShares($message->set, $message->remove);

apps/dav/lib/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function __construct(IRequest $request, $baseUri) {
162162
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
163163

164164
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
165-
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
165+
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
166166
$this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
167167
\OC::$server->getConfig(),
168168
\OC::$server->getURLGenerator()
@@ -171,7 +171,7 @@ public function __construct(IRequest $request, $baseUri) {
171171

172172
// addressbook plugins
173173
if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
174-
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
174+
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
175175
$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
176176
$this->server->addPlugin(new VCFExportPlugin());
177177
$this->server->addPlugin(new MultiGetExportPlugin());

apps/dav/tests/unit/CardDAV/Sharing/PluginTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\DAV\Connector\Sabre\Auth;
3131
use OCA\DAV\DAV\Sharing\IShareable;
3232
use OCA\DAV\DAV\Sharing\Plugin;
33+
use OCP\IConfig;
3334
use OCP\IRequest;
3435
use Sabre\DAV\Server;
3536
use Sabre\DAV\SimpleCollection;
@@ -55,7 +56,8 @@ protected function setUp(): void {
5556

5657
/** @var IRequest $request */
5758
$request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
58-
$this->plugin = new Plugin($authBackend, $request);
59+
$config = $this->createMock(IConfig::class);
60+
$this->plugin = new Plugin($authBackend, $request, $config);
5961

6062
$root = new SimpleCollection('root');
6163
$this->server = new \Sabre\DAV\Server($root);

apps/dav/tests/unit/DAV/Sharing/PluginTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\DAV\Connector\Sabre\Auth;
3131
use OCA\DAV\DAV\Sharing\IShareable;
3232
use OCA\DAV\DAV\Sharing\Plugin;
33+
use OCP\IConfig;
3334
use OCP\IRequest;
3435
use Sabre\DAV\Server;
3536
use Sabre\DAV\SimpleCollection;
@@ -55,7 +56,8 @@ protected function setUp(): void {
5556

5657
/** @var IRequest $request */
5758
$request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
58-
$this->plugin = new Plugin($authBackend, $request);
59+
$config = $this->createMock(IConfig::class);
60+
$this->plugin = new Plugin($authBackend, $request, $config);
5961

6062
$root = new SimpleCollection('root');
6163
$this->server = new \Sabre\DAV\Server($root);

0 commit comments

Comments
 (0)