refactor: migrate OC_EventSource to dependency injection

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Daniel Kesselberg 2023-05-21 23:05:40 +02:00
parent 2fd7febffd
commit a2afc7b6a9
No known key found for this signature in database
GPG Key ID: 36E3664E099D0614
10 changed files with 144 additions and 28 deletions

View File

@ -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

View File

@ -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',

View File

@ -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',

View File

@ -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);
}
}

View File

@ -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();
}
@ -1928,16 +1931,6 @@ class Server extends ServerContainer implements IServerContainer {
return $this->get(IClientService::class);
}
/**
* Create a new event source
*
* @return \OCP\IEventSource
* @deprecated 20.0.0
*/
public function createEventSource() {
return new \OC_EventSource();
}
/**
* Get the active event logger
*

View File

@ -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();

View File

@ -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;
}

View File

@ -395,15 +395,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
*

View File

@ -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);
}
}

View File

@ -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');