Skip to content
Permalink
Browse files Browse the repository at this point in the history
Hide revert button when no permission to revert
  • Loading branch information
Vincent Petry committed Jul 13, 2016
1 parent ac759d6 commit 2338308
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/files_versions/lib/storage.php
Expand Up @@ -287,8 +287,16 @@ public static function rollback($file, $revision) {
// add expected leading slash
$file = '/' . ltrim($file, '/');
list($uid, $filename) = self::getUidAndFilename($file);
if ($uid === null || trim($filename, '/') === '') {
return false;
}
$users_view = new \OC\Files\View('/'.$uid);
$files_view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');

if (!$files_view->isUpdatable($filename)) {
return false;
}

$versionCreated = false;

//first create a new version
Expand Down
64 changes: 64 additions & 0 deletions apps/files_versions/tests/versions.php
Expand Up @@ -580,6 +580,68 @@ public function testRestoreCrossStorage() {
$this->doTestRestore();
}

public function testRestoreNoPermission() {
$this->loginAsUser(self::TEST_VERSIONS_USER);

$userHome = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER);
$node = $userHome->newFolder('folder');
$file = $node->newFile('test.txt');

\OCP\Share::shareItem(
'folder',
$file->getId(),
\OCP\Share::SHARE_TYPE_USER,
self::TEST_VERSIONS_USER2,
\OCP\Constants::PERMISSION_READ
);

$versions = $this->createAndCheckVersions(
\OC\Files\Filesystem::getView(),
'folder/test.txt'
);

$file->putContent('test file');

$this->loginAsUser(self::TEST_VERSIONS_USER2);

$firstVersion = current($versions);

$this->assertFalse(\OCA\Files_Versions\Storage::rollback('folder/test.txt', $firstVersion['version']), 'Revert did not happen');

$this->loginAsUser(self::TEST_VERSIONS_USER);

$this->assertEquals('test file', $file->getContent(), 'File content has not changed');
}

/**
* @param string $hookName name of hook called
* @param string $params variable to recieve parameters provided by hook
*/
private function connectMockHooks($hookName, &$params) {
if ($hookName === null) {
return;
}

$eventHandler = $this->getMockBuilder('\stdclass')
->setMethods(['callback'])
->getMock();

$eventHandler->expects($this->any())
->method('callback')
->will($this->returnCallback(
function($p) use (&$params) {
$params = $p;
}
));

\OCP\Util::connectHook(
'\OCP\Versions',
$hookName,
$eventHandler,
'callback'
);
}

private function doTestRestore() {
$filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt';
$this->rootView->file_put_contents($filePath, 'test file');
Expand Down Expand Up @@ -739,6 +801,8 @@ private function createAndCheckVersions($view, $path) {
// note: we cannot predict how many versions are created due to
// test run timing
$this->assertGreaterThan(0, count($versions));

return $versions;
}

/**
Expand Down

0 comments on commit 2338308

Please sign in to comment.