diff options
author | Robin Appelman <robin@icewind.nl> | 2023-06-03 22:17:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-03 22:17:34 +0200 |
commit | f5932eb14490427dc69373db570b203dd7de83f3 (patch) | |
tree | 214512080370ac8ce227ab60a6f331163db045da | |
parent | 2fd7febffd0ade3f33c8833126c2b8eceabc5d14 (diff) | |
parent | a2afc7b6a9b42598cadcc3d3e9dde80e0a8a9ce4 (diff) | |
download | nextcloud-server-f5932eb14490427dc69373db570b203dd7de83f3.tar.gz nextcloud-server-f5932eb14490427dc69373db570b203dd7de83f3.zip |
Merge pull request #38386 from nextcloud/dept-use-di-for-event-source
refactor: migrate OC_EventSource to dependency injection
-rw-r--r-- | core/ajax/update.php | 7 | ||||
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | lib/private/EventSourceFactory.php | 46 | ||||
-rw-r--r-- | lib/private/Server.php | 13 | ||||
-rw-r--r-- | lib/private/legacy/OC_EventSource.php | 13 | ||||
-rw-r--r-- | lib/public/IEventSourceFactory.php | 38 | ||||
-rw-r--r-- | lib/public/IServerContainer.php | 9 | ||||
-rw-r--r-- | tests/lib/EventSourceFactoryTest.php | 37 | ||||
-rw-r--r-- | tests/lib/ServerTest.php | 5 |
10 files changed, 144 insertions, 28 deletions
diff --git a/core/ajax/update.php b/core/ajax/update.php index dae08ad0882..1678e30c02e 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -33,6 +33,7 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\IEventSource; +use OCP\IEventSourceFactory; use OCP\IL10N; use OCP\ILogger; use OC\DB\MigratorExecuteSqlEvent; @@ -43,6 +44,7 @@ use OC\Repair\Events\RepairInfoEvent; use OC\Repair\Events\RepairStartEvent; use OC\Repair\Events\RepairStepEvent; use OC\Repair\Events\RepairWarningEvent; +use OCP\L10N\IFactory; if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { @set_time_limit(0); @@ -50,9 +52,10 @@ if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { require_once '../../lib/base.php'; -$l = \OC::$server->getL10N('core'); +/** @var \OCP\IL10N $l */ +$l = \OC::$server->get(IFactory::class)->get('core'); -$eventSource = \OC::$server->createEventSource(); +$eventSource = \OC::$server->get(IEventSourceFactory::class)->create(); // need to send an initial message to force-init the event source, // which will then trigger its own CSRF check and produces its own CSRF error // message diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f35b9c94fbc..9f9ff8f60f3 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -453,6 +453,7 @@ return array( 'OCP\\IDateTimeZone' => $baseDir . '/lib/public/IDateTimeZone.php', 'OCP\\IEmojiHelper' => $baseDir . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => $baseDir . '/lib/public/IEventSource.php', + 'OCP\\IEventSourceFactory' => $baseDir . '/lib/public/IEventSourceFactory.php', 'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php', 'OCP\\IImage' => $baseDir . '/lib/public/IImage.php', @@ -1196,6 +1197,7 @@ return array( 'OC\\EventDispatcher\\GenericEventWrapper' => $baseDir . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php', + 'OC\\EventSourceFactory' => $baseDir . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 8de7ef99d02..546fa72c325 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -486,6 +486,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\IDateTimeZone' => __DIR__ . '/../../..' . '/lib/public/IDateTimeZone.php', 'OCP\\IEmojiHelper' => __DIR__ . '/../../..' . '/lib/public/IEmojiHelper.php', 'OCP\\IEventSource' => __DIR__ . '/../../..' . '/lib/public/IEventSource.php', + 'OCP\\IEventSourceFactory' => __DIR__ . '/../../..' . '/lib/public/IEventSourceFactory.php', 'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php', 'OCP\\IImage' => __DIR__ . '/../../..' . '/lib/public/IImage.php', @@ -1229,6 +1230,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\EventDispatcher\\GenericEventWrapper' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php', + 'OC\\EventSourceFactory' => __DIR__ . '/../../..' . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/private/EventSourceFactory.php b/lib/private/EventSourceFactory.php new file mode 100644 index 00000000000..197c8bf9e6c --- /dev/null +++ b/lib/private/EventSourceFactory.php @@ -0,0 +1,46 @@ +<?php +/** + * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de> + * + * @author Daniel Kesselberg <mail@danielkesselberg.de> + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC; + +use OCP\IEventSource; +use OCP\IEventSourceFactory; +use OCP\IRequest; + +class EventSourceFactory implements IEventSourceFactory { + private IRequest $request; + + + public function __construct(IRequest $request) { + $this->request = $request; + } + + /** + * Create a new event source + * + * @return IEventSource + * @since 28.0.0 + */ + public function create(): IEventSource { + return new \OC_EventSource($this->request); + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 84860f8c494..f98ee051a32 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -211,6 +211,7 @@ use OCP\IBinaryFinder; use OCP\IDateTimeFormatter; use OCP\IDateTimeZone; use OCP\IDBConnection; +use OCP\IEventSourceFactory; use OCP\IGroupManager; use OCP\IInitialStateService; use OCP\IL10N; @@ -1467,6 +1468,8 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class); + $this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class); + $this->connectDispatcher(); } @@ -1929,16 +1932,6 @@ class Server extends ServerContainer implements IServerContainer { } /** - * Create a new event source - * - * @return \OCP\IEventSource - * @deprecated 20.0.0 - */ - public function createEventSource() { - return new \OC_EventSource(); - } - - /** * Get the active event logger * * The returned logger only logs data when debug mode is enabled diff --git a/lib/private/legacy/OC_EventSource.php b/lib/private/legacy/OC_EventSource.php index c733316050f..cd72ba1f2d5 100644 --- a/lib/private/legacy/OC_EventSource.php +++ b/lib/private/legacy/OC_EventSource.php @@ -1,4 +1,7 @@ <?php + +use OCP\IRequest; + /** * @copyright Copyright (c) 2016, ownCloud, Inc. * @@ -42,6 +45,12 @@ class OC_EventSource implements \OCP\IEventSource { */ private $started = false; + private IRequest $request; + + public function __construct(IRequest $request) { + $this->request = $request; + } + protected function init() { if ($this->started) { return; @@ -71,11 +80,11 @@ class OC_EventSource implements \OCP\IEventSource { } else { header("Content-Type: text/event-stream"); } - if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { + if (!$this->request->passesStrictCookieCheck()) { header('Location: '.\OC::$WEBROOT); exit(); } - if (!\OC::$server->getRequest()->passesCSRFCheck()) { + if (!$this->request->passesCSRFCheck()) { $this->send('error', 'Possible CSRF attack. Connection will be closed.'); $this->close(); exit(); diff --git a/lib/public/IEventSourceFactory.php b/lib/public/IEventSourceFactory.php new file mode 100644 index 00000000000..5876189cebb --- /dev/null +++ b/lib/public/IEventSourceFactory.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de> + * + * @author Daniel Kesselberg <mail@danielkesselberg.de> + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCP; + +/** + * @since 28.0.0 + */ +interface IEventSourceFactory { + /** + * Create a new event source + * + * @return IEventSource + * @since 28.0.0 + */ + public function create(): IEventSource; +} diff --git a/lib/public/IServerContainer.php b/lib/public/IServerContainer.php index c69dab8b3c6..f438838b98e 100644 --- a/lib/public/IServerContainer.php +++ b/lib/public/IServerContainer.php @@ -396,15 +396,6 @@ interface IServerContainer extends ContainerInterface, IContainer { public function getCertificateManager(); /** - * Create a new event source - * - * @return \OCP\IEventSource - * @since 8.0.0 - * @deprecated 20.0.0 have it injected or fetch it through \Psr\Container\ContainerInterface::get - */ - public function createEventSource(); - - /** * Returns an instance of the HTTP client service * * @return \OCP\Http\Client\IClientService diff --git a/tests/lib/EventSourceFactoryTest.php b/tests/lib/EventSourceFactoryTest.php new file mode 100644 index 00000000000..67bc900bd4c --- /dev/null +++ b/tests/lib/EventSourceFactoryTest.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de> + * + * @author Daniel Kesselberg <mail@danielkesselberg.de> + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Test; + +use OC\EventSourceFactory; +use OCP\IEventSource; +use OCP\IRequest; + +class EventSourceFactoryTest extends TestCase { + public function testCreate(): void { + $request = $this->createMock(IRequest::class); + $factory = new EventSourceFactory($request); + + $instance = $factory->create(); + $this->assertInstanceOf(IEventSource::class, $instance); + } +} diff --git a/tests/lib/ServerTest.php b/tests/lib/ServerTest.php index cbaa0e0762a..d9f95e9eab1 100644 --- a/tests/lib/ServerTest.php +++ b/tests/lib/ServerTest.php @@ -179,11 +179,6 @@ class ServerTest extends \Test\TestCase { $this->assertInstanceOf('\OCP\ICertificateManager', $this->server->getCertificateManager(), 'service returned by "getCertificateManager" did not return the right class'); } - public function testCreateEventSource() { - $this->assertInstanceOf('\OC_EventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class'); - $this->assertInstanceOf('\OCP\IEventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class'); - } - public function testOverwriteDefaultCommentsManager() { $config = $this->server->getConfig(); $defaultManagerFactory = $config->getSystemValue('comments.managerFactory', '\OC\Comments\ManagerFactory'); |