Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions apps/admin_audit/lib/Actions/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
namespace OCA\AdminAudit\Actions;

class Versions extends Action {
public function rollback(array $params): void {
$this->log('Version "%s" of "%s" was restored.',
[
'version' => $params['revision'],
'path' => $params['path']
],
['version', 'path']
);
}

public function delete(array $params): void {
$this->log('Version "%s" was deleted.',
['path' => $params['path']],
Expand Down
3 changes: 2 additions & 1 deletion apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\AdminAudit\Listener\SecurityEventListener;
use OCA\AdminAudit\Listener\SharingEventListener;
use OCA\AdminAudit\Listener\UserManagementEventListener;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCP\App\Events\AppDisableEvent;
use OCP\App\Events\AppEnableEvent;
use OCP\App\Events\AppUpdateEvent;
Expand Down Expand Up @@ -110,6 +111,7 @@ public function register(IRegistrationContext $context): void {

// File events
$context->registerEventListener(BeforePreviewFetchedEvent::class, FileEventListener::class);
$context->registerEventListener(VersionRestoredEvent::class, FileEventListener::class);

// Security events
$context->registerEventListener(TwoFactorProviderChallengePassed::class, SecurityEventListener::class);
Expand Down Expand Up @@ -220,7 +222,6 @@ function (BeforeNodeDeletedEvent $event) use ($fileActions): void {

private function versionsHooks(IAuditLogger $logger): void {
$versionsActions = new Versions($logger);
Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has no event equivalent yet I suppose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

\OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);

}

Expand Down
19 changes: 18 additions & 1 deletion apps/admin_audit/lib/Listener/FileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\AdminAudit\Listener;

use OCA\AdminAudit\Actions\Action;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\InvalidPathException;
Expand All @@ -19,12 +20,14 @@
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<BeforePreviewFetchedEvent>
* @template-implements IEventListener<BeforePreviewFetchedEvent|VersionRestoredEvent>
*/
class FileEventListener extends Action implements IEventListener {
public function handle(Event $event): void {
if ($event instanceof BeforePreviewFetchedEvent) {
$this->beforePreviewFetched($event);
} elseif ($event instanceof VersionRestoredEvent) {
$this->versionRestored($event);
}
}

Expand Down Expand Up @@ -54,4 +57,18 @@ private function beforePreviewFetched(BeforePreviewFetchedEvent $event): void {
return;
}
}

/**
* Logs when a version is restored
*/
private function versionRestored(VersionRestoredEvent $event): void {
$version = $event->getVersion();
$this->log('Version "%s" of "%s" was restored.',
[
'version' => $version->getRevisionId(),
'path' => $version->getVersionPath()
],
['version', 'path']
);
}
}
18 changes: 10 additions & 8 deletions apps/files_versions/tests/VersioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use OCA\Files_Sharing\AppInfo\Application;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCA\Files_Versions\Storage;
use OCA\Files_Versions\Versions\IVersionManager;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IMimeTypeLoader;
use OCP\IConfig;
use OCP\IUser;
Expand Down Expand Up @@ -804,22 +806,22 @@ private function doTestRestore() {
$this->assertEquals('test file', $this->rootView->file_get_contents($filePath));
$info1 = $this->rootView->getFileInfo($filePath);

$params = [];
$this->connectMockHooks('rollback', $params);
$eventDispatcher = Server::get(IEventDispatcher::class);
$eventFired = false;
$eventDispatcher->addListener(VersionRestoredEvent::class, function ($event) use (&$eventFired, $t2) {
$eventFired = true;
$this->assertEquals('/sub/test.txt', $event->getVersion()->getVersionPath());
$this->assertTrue($event->getVersion()->getRevisionId() > 0);
});

$versionManager = Server::get(IVersionManager::class);
$versions = $versionManager->getVersionsForFile($this->user1, $info1);
$version = array_filter($versions, function ($version) use ($t2) {
return $version->getRevisionId() === $t2;
});
$this->assertTrue($versionManager->rollback(current($version)));
$expectedParams = [
'path' => '/sub/test.txt',
];

$this->assertEquals($expectedParams['path'], $params['path']);
$this->assertTrue(array_key_exists('revision', $params));
$this->assertTrue($params['revision'] > 0);
$this->assertTrue($eventFired, 'VersionRestoredEvent was not fired');

$this->assertEquals('version2', $this->rootView->file_get_contents($filePath));
$info2 = $this->rootView->getFileInfo($filePath);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Preview/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace OC\Preview;

use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\Files\Node;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function postWrite(Node $node) {
$this->deleteNode($node);
}

protected function deleteNode(Node $node) {
protected function deleteNode(FileInfo $node) {
// We only handle files
if ($node instanceof Folder) {
return;
Expand Down
32 changes: 11 additions & 21 deletions lib/private/Preview/WatcherConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,33 @@
namespace OC\Preview;

use OC\SystemConfig;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Node;

class WatcherConnector {
/** @var IRootFolder */
private $root;

/** @var SystemConfig */
private $config;

/**
* WatcherConnector constructor.
*
* @param IRootFolder $root
* @param SystemConfig $config
*/
public function __construct(IRootFolder $root,
SystemConfig $config) {
$this->root = $root;
$this->config = $config;
public function __construct(
private IRootFolder $root,
private SystemConfig $config,
private IEventDispatcher $dispatcher,
) {
}

/**
* @return Watcher
*/
private function getWatcher(): Watcher {
return \OCP\Server::get(Watcher::class);
}

public function connectWatcher() {
public function connectWatcher(): void {
// Do not connect if we are not setup yet!
if ($this->config->getValue('instanceid', null) !== null) {
$this->root->listen('\OC\Files', 'postWrite', function (Node $node) {
$this->getWatcher()->postWrite($node);
});

\OC_Hook::connect('\OCP\Versions', 'rollback', $this->getWatcher(), 'versionRollback');
$this->dispatcher->addListener(VersionRestoredEvent::class, function (VersionRestoredEvent $event) {
$this->getWatcher()->versionRollback(['node' => $event->getVersion()->getSourceFile()]);
});
}
}
}
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ public function __construct($webRoot, \OC\Config $config) {

$previewConnector = new \OC\Preview\WatcherConnector(
$root,
$c->get(SystemConfig::class)
$c->get(SystemConfig::class),
$this->get(IEventDispatcher::class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->get(IEventDispatcher::class)
$c->get(IEventDispatcher::class)

Other lines from the same file seem to use $c for this, but not sure what’s the difference 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't go into the nuances either, but before this initialization, when creating root, it is $this->get(IEventDispatcher::class) that is used

);
$previewConnector->connectWatcher();

Expand Down
Loading