1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tom Needham <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OC\Core\Command\Encryption; |
23
|
|
|
|
24
|
|
|
use OCP\App\IAppManager; |
25
|
|
|
use OCP\Encryption\IManager; |
26
|
|
|
use OCP\IConfig; |
27
|
|
|
use Symfony\Component\Console\Command\Command; |
28
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
29
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
30
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
33
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
34
|
|
|
|
35
|
|
|
class ParallelDecryptAll extends Command { |
36
|
|
|
|
37
|
|
|
/** @var IManager */ |
38
|
|
|
protected $encryptionManager; |
39
|
|
|
|
40
|
|
|
/** @var IAppManager */ |
41
|
|
|
protected $appManager; |
42
|
|
|
|
43
|
|
|
/** @var IConfig */ |
44
|
|
|
protected $config; |
45
|
|
|
|
46
|
|
|
/** @var bool */ |
47
|
|
|
protected $wasSingleUserModeEnabled; |
48
|
|
|
|
49
|
|
|
/** @var \OC\Encryption\ParallelDecryptAll */ |
50
|
|
|
protected $parallelDecryptAll; |
51
|
|
|
|
52
|
|
|
/** @var OutputInterface */ |
53
|
|
|
protected $output; |
54
|
|
|
|
55
|
|
|
/** @var InputInterface */ |
56
|
|
|
protected $input; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param IManager $encryptionManager |
60
|
|
|
* @param IAppManager $appManager |
61
|
|
|
* @param IConfig $config |
62
|
|
|
* @param \OC\Encryption\DecryptAll $parallelDecryptAll |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function __construct( |
65
|
|
|
IManager $encryptionManager, |
66
|
|
|
IAppManager $appManager, |
67
|
|
|
IConfig $config, |
68
|
|
|
\OC\Encryption\ParallelDecryptAll $parallelDecryptAll |
69
|
|
|
) { |
70
|
|
|
parent::__construct(); |
71
|
|
|
$this->appManager = $appManager; |
72
|
|
|
$this->encryptionManager = $encryptionManager; |
73
|
|
|
$this->config = $config; |
74
|
|
|
$this->parallelDecryptAll = $parallelDecryptAll; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function configure() { |
78
|
|
|
parent::configure(); |
79
|
|
|
|
80
|
|
|
$this->setName('encryption:parallel-decrypt-all'); |
81
|
|
|
$this->setDescription('Decrypts all files for a user - can be used in parallel'); |
82
|
|
|
$this->addArgument( |
83
|
|
|
'user', |
84
|
|
|
InputArgument::REQUIRED, |
85
|
|
|
'user for which you want to decrypt all files' |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function checkSetup() { |
90
|
|
|
$this->output->writeln('Enable single user, disable trashbin, disable encryption...'); |
91
|
|
|
$this->config->setSystemValue('singleuser', true); |
92
|
|
|
$this->appManager->disableApp('files_trashbin'); |
93
|
|
|
$this->config->setAppValue('core', 'encryption_enabled', 'no'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
97
|
|
|
$this->output = $output; |
98
|
|
|
$this->input = $input; |
99
|
|
|
|
100
|
|
|
$this->parallelDecryptAll->setInOut($input, $output); |
101
|
|
|
|
102
|
|
|
$user = $input->getArgument('user'); |
103
|
|
|
|
104
|
|
|
$this->output->writeln('Preparing module for decryption...'); |
105
|
|
|
if ($this->parallelDecryptAll->prepareEncryptionModules($user) === false) { |
106
|
|
|
return false; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->checkSetup(); |
110
|
|
|
|
111
|
|
|
$output->writeln("Decrypting $user..."); |
112
|
|
|
|
113
|
|
|
// Run the decryption |
114
|
|
|
$progress = new ProgressBar($this->output); |
115
|
|
|
$progress->setFormat(" %message% \n [%bar%]"); |
116
|
|
|
$this->parallelDecryptAll->decryptUsersFiles($user, $progress, 1); |
117
|
|
|
|
118
|
|
|
return $this->parallelDecryptAll->checkForFailure($output); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.