Skip to content

Commit

Permalink
Merge pull request #1018 from nextcloud/revert-510-bugfix/noid/disabl…
Browse files Browse the repository at this point in the history
…e-sabre-plugin

Revert "Temporarily disable the DAV plugin until clients work"
  • Loading branch information
GretaD authored Jan 30, 2025
2 parents f7e9c60 + 53f22a5 commit c88d675
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 21 deletions.
27 changes: 14 additions & 13 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Registration\Events\ValidateFormEvent;
use OCA\TermsOfService\PublicCapabilities;
use OCA\TermsOfService\Checker;
use OCA\TermsOfService\Dav\CheckPlugin;
use OCA\TermsOfService\Filesystem\StorageWrapper;
use OCA\TermsOfService\Listener\RegistrationIntegration;
use OCA\TermsOfService\Listener\UserDeletedListener;
Expand All @@ -29,6 +30,7 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\IManager;
use OCP\SabrePluginEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\UserFirstTimeLoggedInEvent;
use OCP\Util;
Expand Down Expand Up @@ -58,19 +60,18 @@ public function register(IRegistrationContext $context): void {
public function boot(IBootContext $context): void {
Util::connectHook('OC_Filesystem', 'preSetup', $this, 'addStorageWrapper');

// FIXME currently disabled until we made sure all clients (Talk and files on Android and iOS, as well as desktop) handle this gracefully
// $eventDispatcher = $context->getServerContainer()->get(IEventDispatcher::class);
// $eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($context) {
// $eventServer = $event->getServer();
//
// if ($eventServer !== null) {
// // We have to register the CheckPlugin here and not info.xml,
// // because info.xml plugins are loaded, after the
// // beforeMethod:* hook has already been emitted.
// $plugin = $context->getAppContainer()->get(CheckPlugin::class);
// $eventServer->addPlugin($plugin);
// }
// });
$eventDispatcher = $context->getServerContainer()->get(IEventDispatcher::class);
$eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($context) {
$eventServer = $event->getServer();

if ($eventServer !== null) {
// We have to register the CheckPlugin here and not info.xml,
// because info.xml plugins are loaded, after the
// beforeMethod:* hook has already been emitted.
$plugin = $context->getAppContainer()->get(CheckPlugin::class);
$eventServer->addPlugin($plugin);
}
});

$context->injectFn([$this, 'registerNotifier']);
$context->injectFn([$this, 'createNotificationOnFirstLogin']);
Expand Down
9 changes: 7 additions & 2 deletions lib/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\IL10N;
Expand All @@ -38,6 +39,8 @@ class Checker {
private $logger;
/** @var array */
private $termsCache = [];
/** @var IURLGenerator */
private $url;

public function __construct(
IRequest $request,
Expand All @@ -48,7 +51,8 @@ public function __construct(
CountryDetector $countryDetector,
IConfig $config,
IL10N $l10n,
LoggerInterface $logger
LoggerInterface $logger,
IURLGenerator $url
) {
$this->request = $request;
$this->userSession = $userSession;
Expand All @@ -59,10 +63,11 @@ public function __construct(
$this->config = $config;
$this->l10n = $l10n;
$this->logger = $logger;
$this->url = $url;
}

public function getForbiddenMessage(): string {
return $this->l10n->t('Terms of service are not signed');
return $this->url->getBaseUrl();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/Dav/CheckPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Exception\Forbidden;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

use OCA\TermsOfService\AppInfo\Application;
use OCA\TermsOfService\Checker;
use OCA\TermsOfService\TermsNotSignedException;


class CheckPlugin extends ServerPlugin {
/** @var Server */
Expand Down Expand Up @@ -51,9 +52,9 @@ public function initialize(Server $server) {
*/
public function checkToS(RequestInterface $request, ResponseInterface $response) {
// we instantiate the checker here to make sure sabre auth backend was triggered
$checker = \OC::$server->get(Checker::class);
$checker = \OCP\Server::get(Checker::class);
if (!$checker->currentUserHasSigned()) {
throw new Forbidden($checker->getForbiddenMessage());
throw new TermsNotSignedException($checker->getForbiddenMessage());
}
return true;
}
Expand Down
15 changes: 15 additions & 0 deletions lib/TermsNotSignedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\TermsOfService;

use Sabre\DAV\Exception\Forbidden;

class TermsNotSignedException extends Forbidden {
}
1 change: 1 addition & 0 deletions vendor-bin/psalm/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"require-dev": {
"nextcloud/ocp": "dev-stable28",
"sabre/dav": "^4.1",
"vimeo/psalm": "^5.6"
}
}
Loading

0 comments on commit c88d675

Please sign in to comment.