Skip to content

Commit 975b8e5

Browse files
provokateurinbackportbot[bot]
authored andcommitted
feat(Sharing): Implement API kill switch
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 2901699 commit 975b8e5

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

apps/sharing/lib/Capabilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99

1010
namespace OCA\Sharing;
1111

12+
use NCU\Sharing\ISharingManager;
1213
use NCU\Sharing\ISharingRegistry;
1314
use NCU\Sharing\Permission\ISharePermissionPreset;
1415
use NCU\Sharing\Source\IShareSourceType;
1516
use OCA\Sharing\AppInfo\Application;
1617
use OCP\Capabilities\ICapability;
1718
use OCP\L10N\IFactory;
18-
use OCP\Server;
19-
use OCP\Share\IManager;
2019

2120
/**
2221
* @psalm-import-type SharingSourceType from ResponseDefinitions
@@ -26,6 +25,7 @@
2625
public function __construct(
2726
private IFactory $l10nFactory,
2827
private ISharingRegistry $registry,
28+
private ISharingManager $manager,
2929
) {
3030
}
3131

@@ -40,7 +40,7 @@ public function __construct(
4040
*/
4141
#[\Override]
4242
public function getCapabilities(): array {
43-
if (!Server::get(IManager::class)->shareApiEnabled()) {
43+
if (!$this->manager->isApiEnabled()) {
4444
return [];
4545
}
4646

apps/sharing/lib/Middleware/ShareApiEnabledMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
namespace OCA\Sharing\Middleware;
1111

12+
use NCU\Sharing\ISharingManager;
1213
use OCP\AppFramework\Controller;
1314
use OCP\AppFramework\Http;
1415
use OCP\AppFramework\Middleware;
1516
use OCP\AppFramework\OCS\OCSException;
1617
use OCP\Server;
17-
use OCP\Share\IManager;
1818

1919
final class ShareApiEnabledMiddleware extends Middleware {
2020
#[\Override]
2121
public function beforeController(Controller $controller, string $methodName): void {
22-
if (!Server::get(IManager::class)->shareApiEnabled()) {
23-
throw new OCSException('The Share API is not enabled.', Http::STATUS_NOT_IMPLEMENTED);
22+
if (!Server::get(ISharingManager::class)->isApiEnabled()) {
23+
throw new OCSException('The Unified Sharing API is not enabled.', Http::STATUS_NOT_IMPLEMENTED);
2424
}
2525
}
2626
}

apps/sharing/tests/CapabilitiesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use NCU\Sharing\ISharingRegistry;
1313
use OCA\Sharing\AppInfo\Application;
1414
use OCA\Sharing\Capabilities;
15+
use OCP\IConfig;
1516
use OCP\Server;
1617
use PHPUnit\Framework\Attributes\Group;
1718
use Test\Sharing\TestSharePermissionPreset1;
@@ -44,6 +45,9 @@ protected function tearDown(): void {
4445
}
4546

4647
public function testGetCapabilities(): void {
48+
$config = Server::get(IConfig::class);
49+
$config->setSystemValue('sharing.unified_api_enable', true);
50+
4751
$this->registry->registerSourceType(new TestShareSourceType1([]));
4852
$this->registry->registerSourceType(new TestShareSourceType2([]));
4953
$this->registry->registerPermissionPreset(new TestSharePermissionPreset1());
@@ -77,5 +81,16 @@ public function testGetCapabilities(): void {
7781
],
7882
$this->capabilities->getCapabilities(),
7983
);
84+
85+
$config->deleteSystemValue('sharing.unified_api_enable');
86+
}
87+
88+
public function testGetCapabilitiesDisableUnifiedSharingApi(): void {
89+
$config = Server::get(IConfig::class);
90+
$config->setSystemValue('sharing.unified_api_enable', false);
91+
92+
$this->assertEquals([], $this->capabilities->getCapabilities());
93+
94+
$config->deleteSystemValue('sharing.unified_api_enable');
8095
}
8196
}

lib/private/Sharing/SharingManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\EventDispatcher\Event;
3434
use OCP\EventDispatcher\IEventDispatcher;
3535
use OCP\EventDispatcher\IEventListener;
36+
use OCP\IConfig;
3637
use OCP\IDBConnection;
3738
use OCP\IL10N;
3839
use OCP\Interaction\Actions\ShareAction;
@@ -41,6 +42,7 @@
4142
use OCP\IUserManager;
4243
use OCP\L10N\IFactory;
4344
use OCP\Security\ISecureRandom;
45+
use OCP\Share\IManager;
4446
use OCP\Snowflake\ISnowflakeGenerator;
4547
use OCP\User\Events\BeforeUserDeletedEvent;
4648
use Psr\Clock\ClockInterface;
@@ -71,6 +73,8 @@ public function __construct(
7173
private ISharingRegistry $registry,
7274
private ISharingBackend $backend,
7375
private ClockInterface $clock,
76+
private IManager $legacySharingManager,
77+
private IConfig $config,
7478
) {
7579
$this->randomizer = new Randomizer();
7680
$this->l10n = $l10nFactory->get('sharing');
@@ -147,6 +151,12 @@ public function getTime(): \DateTimeImmutable {
147151
return $this->clock->now();
148152
}
149153

154+
#[\Override]
155+
public function isApiEnabled(): bool {
156+
// TODO: Enable Unified Sharing API by default
157+
return $this->legacySharingManager->shareApiEnabled() && $this->config->getSystemValueBool('sharing.unified_api_enable');
158+
}
159+
150160
#[\Override]
151161
public function createShare(ShareAccessContext $accessContext): Share {
152162
if (!($currentUser = $accessContext->currentUser) instanceof IUser) {

lib/unstable/Sharing/ISharingManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function generateSecret(): string;
5454
*/
5555
public function getTime(): \DateTimeImmutable;
5656

57+
/**
58+
* @experimental 35.0.0
59+
*/
60+
public function isApiEnabled(): bool;
61+
5762
/**
5863
* Create a new share.
5964
*

0 commit comments

Comments
 (0)