Skip to content

Commit

Permalink
refactor: migrate OC_EventSource to dependency injection
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Jun 1, 2023
1 parent 1a3bb23 commit 581e7d8
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 28 deletions.
7 changes: 5 additions & 2 deletions core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,16 +44,18 @@
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);
}

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
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@
'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',
Expand Down Expand Up @@ -1196,6 +1197,7 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
46 changes: 46 additions & 0 deletions lib/private/EventSourceFactory.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 3 additions & 10 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
use OCP\IDateTimeFormatter;
use OCP\IDateTimeZone;
use OCP\IDBConnection;
use OCP\IEventSourceFactory;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
Expand Down Expand Up @@ -1467,6 +1468,8 @@ public function __construct($webRoot, \OC\Config $config) {

$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);

$this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class);

$this->connectDispatcher();
}

Expand Down Expand Up @@ -1928,16 +1931,6 @@ public function getHTTPClientService() {
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
*
Expand Down
13 changes: 11 additions & 2 deletions lib/private/legacy/OC_EventSource.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use OCP\IRequest;

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -71,11 +80,11 @@ protected function init() {
} 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();
Expand Down
38 changes: 38 additions & 0 deletions lib/public/IEventSourceFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 0 additions & 9 deletions lib/public/IServerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,6 @@ public function getSearch();
*/
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
*
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/EventSourceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 1 addition & 5 deletions tests/lib/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OC\App\AppStore\Fetcher\AppFetcher;
use OC\App\AppStore\Fetcher\CategoryFetcher;
use OCP\IEventSource;

/**
* Class Server
Expand Down Expand Up @@ -179,11 +180,6 @@ public function testGetCertificateManager() {
$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');
Expand Down

0 comments on commit 581e7d8

Please sign in to comment.