diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index f959ab93..f9fc82a4 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,6 +28,7 @@ use OCA\Registration\Events\ShowFormEvent; use OCA\Registration\Events\ValidateFormEvent; use OCA\TermsOfService\Checker; +use OCA\TermsOfService\Dav\CheckPlugin; use OCA\TermsOfService\Filesystem\StorageWrapper; use OCA\TermsOfService\Listener\RegistrationIntegration; use OCA\TermsOfService\Listener\UserDeletedListener; @@ -74,19 +75,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']); diff --git a/lib/Checker.php b/lib/Checker.php index 217d57ba..0c28dc3a 100644 --- a/lib/Checker.php +++ b/lib/Checker.php @@ -27,6 +27,7 @@ use OCP\IConfig; use OCP\IRequest; use OCP\ISession; +use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; use OCP\IL10N; @@ -55,6 +56,8 @@ class Checker { private $logger; /** @var array */ private $termsCache = []; + /** @var IURLGenerator */ + private $url; public function __construct( ?string $userId, @@ -66,7 +69,8 @@ public function __construct( CountryDetector $countryDetector, IConfig $config, IL10N $l10n, - LoggerInterface $logger + LoggerInterface $logger, + IURLGenerator $url ) { $this->userId = $userId; $this->request = $request; @@ -78,10 +82,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(); } /** diff --git a/lib/Dav/CheckPlugin.php b/lib/Dav/CheckPlugin.php index bb66bdb6..03bded56 100644 --- a/lib/Dav/CheckPlugin.php +++ b/lib/Dav/CheckPlugin.php @@ -28,12 +28,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 */ @@ -68,9 +69,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; } diff --git a/lib/TermsNotSignedException.php b/lib/TermsNotSignedException.php new file mode 100644 index 00000000..f14ae8b4 --- /dev/null +++ b/lib/TermsNotSignedException.php @@ -0,0 +1,15 @@ +