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 May 26, 2023
1 parent 3e293b7 commit 22e5898
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

$l = \OC::$server->getL10N('core');

$eventSource = \OC::$server->createEventSource();
$eventSource = \OC::$server->get(IEventSource::class);
// 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
17 changes: 7 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\IEventSource;
use OCP\IGroupManager;
use OCP\IInitialStateService;
use OCP\IL10N;
Expand Down Expand Up @@ -1467,6 +1468,12 @@ public function __construct($webRoot, \OC\Config $config) {

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

$this->registerService(IEventSource::class, function (ContainerInterface $c) {
return new \OC_EventSource(
$c->get(IRequest::class)
);
}, false);

$this->connectDispatcher();
}

Expand Down Expand Up @@ -1928,16 +1935,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
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
16 changes: 11 additions & 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 All @@ -195,4 +191,14 @@ public function testOverwriteDefaultCommentsManager() {

$config->setSystemValue('comments.managerFactory', $defaultManagerFactory);
}

public function testEventSource(): void {
$a = $this->server->get(IEventSource::class);
$b = $this->server->get(IEventSource::class);

$this->assertInstanceOf(IEventSource::class, $a);
$this->assertInstanceOf(IEventSource::class, $b);

$this->assertNotSame($a, $b);
}
}

0 comments on commit 22e5898

Please sign in to comment.