Merge pull request #37744 from nextcloud/backport/36690/stable25

[stable25] fix: Make sure that rollback hook is triggered on all version backends
This commit is contained in:
Arthur Schiwon 2023-07-10 17:16:04 +02:00 committed by GitHub
commit a4b7bb7c78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 10 deletions

View file

@ -404,12 +404,6 @@ class Storage {
$node = $userFolder->get($file);
// TODO: move away from those legacy hooks!
\OC_Hook::emit('\OCP\Versions', 'rollback', [
'path' => $filename,
'revision' => $revision,
'node' => $node,
]);
return true;
} elseif ($versionCreated) {
self::deleteVersion($users_view, $version);

View file

@ -58,6 +58,14 @@ class LegacyVersionsBackend implements IVersionBackend {
if ($storage->instanceOfStorage(SharedStorage::class)) {
$owner = $storage->getOwner('');
$user = $this->userManager->get($owner);
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$nodes = $userFolder->getById($file->getId());
$file = array_pop($nodes);
if (!$file) {
throw new NotFoundException("version file not found for share owner");
}
}
$userFolder = $this->rootFolder->getUserFolder($user->getUID());

View file

@ -99,7 +99,16 @@ class VersionManager implements IVersionManager {
public function rollback(IVersion $version) {
$backend = $version->getBackend();
return self::handleAppLocks(fn(): ?bool => $backend->rollback($version));
$result = self::handleAppLocks(fn(): ?bool => $backend->rollback($version));
// rollback doesn't have a return type yet and some implementations don't return anything
if ($result === null || $result === true) {
\OC_Hook::emit('\OCP\Versions', 'rollback', [
'path' => $version->getVersionPath(),
'revision' => $version->getRevisionId(),
'node' => $version->getSourceFile(),
]);
}
return $result;
}
public function read(IVersion $version) {

View file

@ -38,6 +38,8 @@ use OC\Files\Storage\Temporary;
use OCP\IConfig;
use OCP\IUser;
use OCP\Share\IShare;
use OCA\Files_Versions\Versions\IVersionManager;
use Test\Traits\MountProviderTrait;
/**
* Class Test_Files_versions
@ -49,6 +51,7 @@ class VersioningTest extends \Test\TestCase {
public const TEST_VERSIONS_USER = 'test-versions-user';
public const TEST_VERSIONS_USER2 = 'test-versions-user2';
public const USERS_VERSIONS_ROOT = '/test-versions-user/files_versions';
use MountProviderTrait;
/**
* @var \OC\Files\View
@ -646,7 +649,8 @@ class VersioningTest extends \Test\TestCase {
public function testRestoreCrossStorage() {
$storage2 = new Temporary([]);
\OC\Files\Filesystem::mount($storage2, [], self::TEST_VERSIONS_USER . '/files/sub');
$this->registerMount(self::TEST_VERSIONS_USER, $storage2, self::TEST_VERSIONS_USER . '/files/sub');
$this->loginAsUser(self::TEST_VERSIONS_USER);
$this->doTestRestore();
}
@ -789,7 +793,12 @@ class VersioningTest extends \Test\TestCase {
$params = [];
$this->connectMockHooks('rollback', $params);
$this->assertTrue(\OCA\Files_Versions\Storage::rollback('sub/test.txt', $t2, $this->user1));
$versionManager = \OCP\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',
];

View file

@ -326,7 +326,7 @@ class Folder extends Node implements \OCP\Files\Folder {
* @param int $id
* @return array
*/
protected function getByIdInRootMount(int $id): array {
protected function getByIdInRootMount(int $id): array {
if (!method_exists($this->root, 'createNode')) {
// Always expected to be false. Being a method of Folder, this is
// always implemented. For it is an internal method and should not