Skip to content

Commit 66b7f2e

Browse files
pboguslawskiCarlSchwan
authored andcommitted
Extract cs:allowed-sharing-modes into it's own plugin
So that it's still there when we disable the PublishPlugin And disable sharing calendars via link when sharik via link is disabled This mod disallows sharing calendars via link when `shareapi_allow_links` is disabled. Signed-off-by: Thomas Citharel <tcit@tcit.fr> Signed-off-by: Carl Schwan <carlschwan@kde.org> Signed-off-by: Pawel Boguslawski <pawel.boguslawski@ib.pl>
1 parent 0302d60 commit 66b7f2e

10 files changed

Lines changed: 101 additions & 61 deletions

File tree

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
'OCA\\DAV\\CalDAV\\TimeZoneFactory' => $baseDir . '/../lib/CalDAV/TimeZoneFactory.php',
142142
'OCA\\DAV\\CalDAV\\TimezoneService' => $baseDir . '/../lib/CalDAV/TimezoneService.php',
143143
'OCA\\DAV\\CalDAV\\TipBroker' => $baseDir . '/../lib/CalDAV/TipBroker.php',
144+
'OCA\\DAV\\CalDAV\\SharingPlugin' => $baseDir . '/../lib/CalDAV/SharingPlugin.php',
144145
'OCA\\DAV\\CalDAV\\Trashbin\\DeletedCalendarObject' => $baseDir . '/../lib/CalDAV/Trashbin/DeletedCalendarObject.php',
145146
'OCA\\DAV\\CalDAV\\Trashbin\\DeletedCalendarObjectsCollection' => $baseDir . '/../lib/CalDAV/Trashbin/DeletedCalendarObjectsCollection.php',
146147
'OCA\\DAV\\CalDAV\\Trashbin\\Plugin' => $baseDir . '/../lib/CalDAV/Trashbin/Plugin.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class ComposerStaticInitDAV
156156
'OCA\\DAV\\CalDAV\\TimeZoneFactory' => __DIR__ . '/..' . '/../lib/CalDAV/TimeZoneFactory.php',
157157
'OCA\\DAV\\CalDAV\\TimezoneService' => __DIR__ . '/..' . '/../lib/CalDAV/TimezoneService.php',
158158
'OCA\\DAV\\CalDAV\\TipBroker' => __DIR__ . '/..' . '/../lib/CalDAV/TipBroker.php',
159+
'OCA\\DAV\\CalDAV\\SharingPlugin' => __DIR__ . '/..' . '/../lib/CalDAV/SharingPlugin.php',
159160
'OCA\\DAV\\CalDAV\\Trashbin\\DeletedCalendarObject' => __DIR__ . '/..' . '/../lib/CalDAV/Trashbin/DeletedCalendarObject.php',
160161
'OCA\\DAV\\CalDAV\\Trashbin\\DeletedCalendarObjectsCollection' => __DIR__ . '/..' . '/../lib/CalDAV/Trashbin/DeletedCalendarObjectsCollection.php',
161162
'OCA\\DAV\\CalDAV\\Trashbin\\Plugin' => __DIR__ . '/..' . '/../lib/CalDAV/Trashbin/Plugin.php',

apps/dav/lib/CalDAV/InvitationResponse/InvitationResponseServer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\DAV\CalDAV\Auth\PublicPrincipalPlugin;
1313
use OCA\DAV\CalDAV\DefaultCalendarValidator;
1414
use OCA\DAV\CalDAV\Publishing\PublishPlugin;
15+
use OCA\DAV\CalDAV\SharingPlugin;
1516
use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
1617
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
1718
use OCA\DAV\Connector\Sabre\CachingTree;
@@ -24,15 +25,15 @@
2425
use OCA\Theming\ThemingDefaults;
2526
use OCP\App\IAppManager;
2627
use OCP\EventDispatcher\IEventDispatcher;
28+
use OCP\IAppConfig;
2729
use OCP\IConfig;
2830
use OCP\IURLGenerator;
2931
use OCP\Server;
3032
use Psr\Log\LoggerInterface;
3133
use Sabre\VObject\ITip\Message;
3234

3335
class InvitationResponseServer {
34-
/** @var \OCA\DAV\Connector\Sabre\Server */
35-
public $server;
36+
public \OCA\DAV\Connector\Sabre\Server $server;
3637

3738
/**
3839
* InvitationResponseServer constructor.
@@ -87,6 +88,7 @@ public function __construct(bool $public = true) {
8788
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
8889
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
8990
//$this->server->addPlugin(new \OCA\DAV\DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
91+
$this->server->addPlugin(new SharingPlugin(Server::get(IAppConfig::class)));
9092
$this->server->addPlugin(new PublishPlugin(
9193
Server::get(IConfig::class),
9294
Server::get(IURLGenerator::class)

apps/dav/lib/CalDAV/PublicCalendar.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class PublicCalendar extends Calendar {
1414
/**
1515
* @param string $name
1616
* @throws NotFound
17-
* @return PublicCalendarObject
1817
*/
1918
#[\Override]
20-
public function getChild($name) {
19+
public function getChild($name): PublicCalendarObject {
2120
$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
2221

2322
if (!$obj) {
@@ -35,7 +34,7 @@ public function getChild($name) {
3534
* @return PublicCalendarObject[]
3635
*/
3736
#[\Override]
38-
public function getChildren() {
37+
public function getChildren(): array {
3938
$objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
4039
$children = [];
4140
foreach ($objs as $obj) {
@@ -53,7 +52,7 @@ public function getChildren() {
5352
* @return PublicCalendarObject[]
5453
*/
5554
#[\Override]
56-
public function getMultipleChildren(array $paths) {
55+
public function getMultipleChildren(array $paths): array {
5756
$objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
5857
$children = [];
5958
foreach ($objs as $obj) {
@@ -67,11 +66,10 @@ public function getMultipleChildren(array $paths) {
6766
}
6867

6968
/**
70-
* public calendars are always shared
71-
* @return bool
69+
* Public calendars are always shared
7270
*/
7371
#[\Override]
74-
public function isShared() {
72+
public function isShared(): bool {
7573
return true;
7674
}
7775
}

apps/dav/lib/CalDAV/PublicCalendarRoot.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,39 @@
88

99
namespace OCA\DAV\CalDAV;
1010

11+
use OCP\IAppConfig;
1112
use OCP\IConfig;
1213
use OCP\IL10N;
1314
use Psr\Log\LoggerInterface;
1415
use Sabre\DAV\Collection;
1516

1617
class PublicCalendarRoot extends Collection {
17-
18-
/**
19-
* PublicCalendarRoot constructor.
20-
*
21-
* @param CalDavBackend $caldavBackend
22-
* @param IL10N $l10n
23-
* @param IConfig $config
24-
*/
2518
public function __construct(
2619
protected CalDavBackend $caldavBackend,
2720
protected IL10N $l10n,
21+
protected IAppConfig $appConfig,
2822
protected IConfig $config,
2923
private LoggerInterface $logger,
3024
) {
3125
}
3226

33-
/**
34-
* @inheritdoc
35-
*/
3627
#[\Override]
37-
public function getName() {
28+
public function getName(): string {
3829
return 'public-calendars';
3930
}
4031

41-
/**
42-
* @inheritdoc
43-
*/
4432
#[\Override]
45-
public function getChild($name) {
33+
public function getChild($name): PublicCalendar {
34+
// Sharing via link is allowed by default, but if the option is set it should be checked.
35+
if (!$this->appConfig->getValueBool('core', 'shareapi_allow_links', true)) {
36+
throw new \Sabre\DAV\Exception\Forbidden();
37+
}
4638
$calendar = $this->caldavBackend->getPublicCalendar($name);
4739
return new PublicCalendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
4840
}
4941

50-
/**
51-
* @inheritdoc
52-
*/
5342
#[\Override]
54-
public function getChildren() {
43+
public function getChildren(): array {
5544
return [];
5645
}
5746
}

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use OCP\AppFramework\Http;
1515
use OCP\IConfig;
1616
use OCP\IURLGenerator;
17-
use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
1817
use Sabre\DAV\Exception\NotFound;
1918
use Sabre\DAV\INode;
2019
use Sabre\DAV\PropFind;
@@ -26,12 +25,7 @@
2625
class PublishPlugin extends ServerPlugin {
2726
public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
2827

29-
/**
30-
* Reference to SabreDAV server object.
31-
*
32-
* @var \Sabre\DAV\Server
33-
*/
34-
protected $server;
28+
protected Server $server;
3529

3630
/**
3731
* PublishPlugin constructor.
@@ -60,9 +54,9 @@ public function __construct(
6054
* @return string[]
6155
*/
6256
#[\Override]
63-
public function getFeatures() {
57+
public function getFeatures(): array {
6458
// May have to be changed to be detected
65-
return ['oc-calendar-publishing', 'calendarserver-sharing'];
59+
return ['oc-calendar-publishing'];
6660
}
6761

6862
/**
@@ -74,7 +68,7 @@ public function getFeatures() {
7468
* @return string
7569
*/
7670
#[\Override]
77-
public function getPluginName() {
71+
public function getPluginName(): string {
7872
return 'oc-calendar-publishing';
7973
}
8074

@@ -121,18 +115,6 @@ public function propFind(PropFind $propFind, INode $node) {
121115
return new Publisher($publishUrl, true);
122116
}
123117
});
124-
125-
$propFind->handle('{' . self::NS_CALENDARSERVER . '}allowed-sharing-modes', function () use ($node) {
126-
$canShare = (!$node->isSubscription() && $node->canWrite());
127-
$canPublish = (!$node->isSubscription() && $node->canWrite());
128-
129-
if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') {
130-
$canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI());
131-
$canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI());
132-
}
133-
134-
return new AllowedSharingModes($canShare, $canPublish);
135-
});
136118
}
137119
}
138120

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
// SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
namespace OCA\DAV\CalDAV;
7+
8+
use OCP\IAppConfig;
9+
use OCP\IConfig;
10+
use Override;
11+
use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
12+
use Sabre\DAV\INode;
13+
use Sabre\DAV\PropFind;
14+
use Sabre\DAV\Server;
15+
use Sabre\DAV\ServerPlugin;
16+
17+
class SharingPlugin extends ServerPlugin {
18+
public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
19+
20+
protected Server $server;
21+
22+
public function __construct(
23+
private readonly IAppConfig $config,
24+
) {
25+
}
26+
27+
#[Override]
28+
public function getFeatures(): array {
29+
// May have to be changed to be detected
30+
return ['calendarserver-sharing'];
31+
}
32+
33+
#[Override]
34+
public function getPluginName(): string {
35+
return 'oc-calendar-sharing';
36+
}
37+
38+
#[Override]
39+
public function initialize(Server $server): void {
40+
$this->server = $server;
41+
42+
$this->server->on('propFind', $this->propFind(...));
43+
}
44+
45+
public function propFind(PropFind $propFind, INode $node): void {
46+
if ($node instanceof Calendar) {
47+
$propFind->handle('{' . self::NS_CALENDARSERVER . '}allowed-sharing-modes', function () use ($node) {
48+
$canShare = (!$node->isSubscription() && $node->canWrite());
49+
$canPublish = (!$node->isSubscription() && $node->canWrite());
50+
51+
if ($this->config->getValueBool('dav', 'limitAddressBookAndCalendarSharingToOwner')) {
52+
$canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI());
53+
$canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI());
54+
}
55+
56+
if (!$this->config->getValueBool('core', 'shareapi_allow_links', true)) {
57+
$canPublish = false;
58+
}
59+
60+
return new AllowedSharingModes($canShare, $canPublish);
61+
});
62+
}
63+
}
64+
}

apps/dav/lib/RootCollection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\Comments\ICommentsManager;
3838
use OCP\EventDispatcher\IEventDispatcher;
3939
use OCP\Files\IRootFolder;
40+
use OCP\IAppConfig;
4041
use OCP\ICacheFactory;
4142
use OCP\IConfig;
4243
use OCP\IDBConnection;
@@ -63,6 +64,7 @@ public function __construct() {
6364
$db = Server::get(IDBConnection::class);
6465
$dispatcher = Server::get(IEventDispatcher::class);
6566
$config = Server::get(IConfig::class);
67+
$appConfig = Server::get(IAppConfig::class);
6668
$proxyMapper = Server::get(ProxyMapper::class);
6769
$rootFolder = Server::get(IRootFolder::class);
6870
$federatedCalendarFactory = Server::get(FederatedCalendarFactory::class);
@@ -125,7 +127,7 @@ public function __construct() {
125127
$roomCalendarRoot = new CalendarRoot($calendarRoomPrincipalBackend, $caldavBackend, 'principals/calendar-rooms', $logger, $l10n, $config, $federatedCalendarFactory);
126128
$roomCalendarRoot->disableListing = $disableListing;
127129

128-
$publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $l10n, $config, $logger);
130+
$publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $l10n, $appConfig, $config, $logger);
129131

130132
$systemTagCollection = Server::get(SystemTagsByIdCollection::class);
131133
$systemTagRelationsCollection = new SystemTagsRelationsCollection(

apps/dav/lib/Server.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
use OCP\IAppConfig;
8888
use OCP\ICacheFactory;
8989
use OCP\IConfig;
90+
use OCP\IConfig;
9091
use OCP\IDateTimeZone;
9192
use OCP\IDBConnection;
9293
use OCP\IGroupManager;
@@ -209,15 +210,18 @@ public function __construct(
209210

210211
$this->server->addPlugin(\OCP\Server::get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
211212
$this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($this->request));
212-
if (\OCP\Server::get(IConfig::class)->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes') {
213+
if (\OCP\Server::get(IAppConfig::class)->getValueBool('dav', 'allow_calendar_link_subscriptions', true)) {
213214
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
214215
}
215216

216217
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
217-
$this->server->addPlugin(new PublishPlugin(
218-
\OCP\Server::get(IConfig::class),
219-
\OCP\Server::get(IURLGenerator::class)
220-
));
218+
$this->server->addPlugin(new \OCA\DAV\CalDAV\SharingPlugin(\OCP\Server::get(IAppConfig::class)));
219+
if (\OCP\Server::get(IAppConfig::class)->getValueBool('core', 'shareapi_allow_links', true)) {
220+
$this->server->addPlugin(new PublishPlugin(
221+
\OCP\Server::get(IConfig::class),
222+
\OCP\Server::get(IURLGenerator::class)
223+
));
224+
}
221225

222226
$this->server->addPlugin(\OCP\Server::get(RateLimitingPlugin::class));
223227
$this->server->addPlugin(\OCP\Server::get(CalDavValidatePlugin::class));
@@ -345,7 +349,7 @@ public function __construct(
345349
\OCP\Server::get(ICommentsManager::class),
346350
$userSession
347351
));
348-
if (\OCP\Server::get(IConfig::class)->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
352+
if (\OCP\Server::get(IAppConfig::class)->getValueBool('dav', 'sendInvitations', true)) {
349353
$this->server->addPlugin(new IMipPlugin(
350354
\OCP\Server::get(IAppConfig::class),
351355
\OCP\Server::get(IMailer::class),

build/psalm-baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@
313313
<DeprecatedMethod>
314314
<code><![CDATA[getAppValue]]></code>
315315
<code><![CDATA[getAppValue]]></code>
316-
<code><![CDATA[getAppValue]]></code>
317316
</DeprecatedMethod>
318317
</file>
319318
<file src="apps/dav/lib/CalDAV/Reminder/NotificationProvider/AbstractProvider.php">
@@ -1042,8 +1041,6 @@
10421041
<DeprecatedMethod>
10431042
<code><![CDATA[dispatch]]></code>
10441043
<code><![CDATA[dispatch]]></code>
1045-
<code><![CDATA[getAppValue]]></code>
1046-
<code><![CDATA[getAppValue]]></code>
10471044
<code><![CDATA[getL10N]]></code>
10481045
<code><![CDATA[getL10N]]></code>
10491046
<code><![CDATA[getUserFolder]]></code>

0 commit comments

Comments
 (0)