[stable9] Fix decrypt message stable9 (#25188)

* Fix Decrypt message via occ

* Comments fixed

* Fixed reviews

* Originally:
commit 2304e4bda027e61ff1302c55c2f70f8e4c8f47d0
Author: Joas Schilling <nickvergessen@owncloud.com>
Date:   Tue Jun 7 09:13:11 2016 +0200

    Allow to decrypt user '0' files only

* Fix uid comparison
This commit is contained in:
Thomas Müller 2016-07-01 11:30:39 +02:00 committed by GitHub
parent 4a4103b923
commit 4ac256ea6c
3 changed files with 54 additions and 27 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
* @author Björn Schießle <bjoern@schiessle.org>
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
@ -111,7 +111,8 @@ class DecryptAll extends Command {
$this->addArgument(
'user',
InputArgument::OPTIONAL,
'user for which you want to decrypt all files (optional)'
'user for which you want to decrypt all files (optional)',
''
);
}
@ -127,8 +128,15 @@ class DecryptAll extends Command {
return;
}
$uid = $input->getArgument('user');
if ($uid === '') {
$message = 'your ownCloud';
} else {
$message = "$uid's account";
}
$output->writeln("\n");
$output->writeln('You are about to start to decrypt all files stored in your ownCloud.');
$output->writeln("You are about to start to decrypt all files stored in $message.");
$output->writeln('It will depend on the encryption module and your setup if this is possible.');
$output->writeln('Depending on the number and size of your files this can take some time');
$output->writeln('Please make sure that no user access his files during this process!');
@ -140,6 +148,7 @@ class DecryptAll extends Command {
$result = $this->decryptAll->decryptAll($input, $output, $user);
if ($result === false) {
$output->writeln(' aborted.');
$output->writeln('Server side encryption remains enabled');
$this->config->setAppValue('core', 'encryption_enabled', 'yes');
}
$this->resetSingleUserAndTrashbin();

View file

@ -1,6 +1,6 @@
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
* @author Björn Schießle <bjoern@schiessle.org>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
@ -80,7 +80,7 @@ class DecryptAll {
$this->input = $input;
$this->output = $output;
if (!empty($user) && $this->userManager->userExists($user) === false) {
if ($user !== '' && $this->userManager->userExists($user) === false) {
$this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
return false;
}
@ -133,6 +133,7 @@ class DecryptAll {
/**
* iterate over all user and encrypt their files
*
* @param string $user which users files should be decrypted, default = all users
*/
protected function decryptAllUsersFiles($user = '') {
@ -140,7 +141,7 @@ class DecryptAll {
$this->output->writeln("\n");
$userList = [];
if (empty($user)) {
if ($user === '') {
$fetchUsersProgress = new ProgressBar($this->output);
$fetchUsersProgress->setFormat(" %message% \n [%bar%]");
@ -200,9 +201,9 @@ class DecryptAll {
$this->setupUserFS($uid);
$directories = array();
$directories[] = '/' . $uid . '/files';
$directories[] = '/' . $uid . '/files';
while($root = array_pop($directories)) {
while ($root = array_pop($directories)) {
$content = $this->rootView->getDirectoryContent($root);
foreach ($content as $file) {
$path = $root . '/' . $file['name'];
@ -213,9 +214,14 @@ class DecryptAll {
try {
$progress->setMessage("decrypt files for user $userCount: $path");
$progress->advance();
if ($this->decryptFile($path) === false) {
if ($file->isEncrypted() === false) {
$progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
$progress->advance();
} else {
if ($this->decryptFile($path) === false) {
$progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
$progress->advance();
}
}
} catch (\Exception $e) {
if (isset($this->failed[$uid])) {

View file

@ -26,6 +26,7 @@ namespace Test\Encryption;
use OC\Encryption\DecryptAll;
use OC\Encryption\Exceptions\DecryptionFailedException;
use OC\Encryption\Manager;
use OC\Files\FileInfo;
use OC\Files\View;
use OCP\IUserManager;
use Test\TestCase;
@ -85,13 +86,25 @@ class DecryptAllTest extends TestCase {
$this->invokePrivate($this->instance, 'output', [$this->outputInterface]);
}
/**
* @dataProvider dataTrueFalse
* @param bool $prepareResult
*/
public function testDecryptAll($prepareResult, $user) {
public function dataDecryptAll() {
return [
[true, 'user1', true],
[false, 'user1', true],
[true, '0', true],
[false, '0', true],
[true, '', false],
];
}
if (!empty($user)) {
/**
* @dataProvider dataDecryptAll
* @param bool $prepareResult
* @param string $user
* @param bool $userExistsChecked
*/
public function testDecryptAll($prepareResult, $user, $userExistsChecked) {
if ($userExistsChecked) {
$this->userManager->expects($this->once())->method('userExists')->willReturn(true);
} else {
$this->userManager->expects($this->never())->method('userExists');
@ -124,15 +137,6 @@ class DecryptAllTest extends TestCase {
$instance->decryptAll($this->inputInterface, $this->outputInterface, $user);
}
public function dataTrueFalse() {
return [
[true, 'user1'],
[false, 'user1'],
[true, ''],
[true, null]
];
}
/**
* test decrypt all call with a user who doesn't exists
*/
@ -146,8 +150,16 @@ class DecryptAllTest extends TestCase {
);
}
public function dataTrueFalse() {
return [
[true],
[false],
];
}
/**
* @dataProvider dataTrueFalse
* @param bool $success
*/
public function testPrepareEncryptionModules($success) {
@ -242,15 +254,15 @@ class DecryptAllTest extends TestCase {
$this->view->expects($this->at(0))->method('getDirectoryContent')
->with('/user1/files')->willReturn(
[
['name' => 'foo', 'type'=>'dir'],
['name' => 'bar', 'type'=>'file'],
new FileInfo('path', null, 'intPath', ['name' => 'foo', 'type'=>'dir'], null),
new FileInfo('path', null, 'intPath', ['name' => 'bar', 'type'=>'file', 'encrypted'=>true], null)
]
);
$this->view->expects($this->at(3))->method('getDirectoryContent')
->with('/user1/files/foo')->willReturn(
[
['name' => 'subfile', 'type'=>'file']
new FileInfo('path', null, 'intPath', ['name' => 'subfile', 'type'=>'file', 'encrypted'=>true], null)
]
);