Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
105 / 175 |
|
14.29% |
2 / 14 |
CRAP | |
0.00% |
0 / 1 |
| Reset | |
60.00% |
105 / 175 |
|
14.29% |
2 / 14 |
225.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| configure | |
100.00% |
70 / 70 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
43.90% |
18 / 41 |
|
0.00% |
0 / 1 |
116.39 | |||
| resetNotifications | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| resetActivity | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| resetIdentifyMethods | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetSignRequest | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetFileElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetCrl | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetUserElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| resetConfig | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
5.12 | |||
| resetPolicy | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Command\Developer; |
| 10 | |
| 11 | use OC\Core\Command\Base; |
| 12 | use OCA\Libresign\AppInfo\Application; |
| 13 | use OCP\IAppConfig; |
| 14 | use OCP\IConfig; |
| 15 | use OCP\IDBConnection; |
| 16 | use Psr\Log\LoggerInterface; |
| 17 | use Symfony\Component\Console\Input\InputInterface; |
| 18 | use Symfony\Component\Console\Input\InputOption; |
| 19 | use Symfony\Component\Console\Output\OutputInterface; |
| 20 | |
| 21 | class Reset extends Base { |
| 22 | public function __construct( |
| 23 | private IConfig $config, |
| 24 | private IAppConfig $appConfig, |
| 25 | private IDBConnection $db, |
| 26 | private LoggerInterface $logger, |
| 27 | ) { |
| 28 | parent::__construct(); |
| 29 | } |
| 30 | |
| 31 | #[\Override] |
| 32 | public function isEnabled(): bool { |
| 33 | return $this->config->getSystemValue('debug', false) === true; |
| 34 | } |
| 35 | |
| 36 | #[\Override] |
| 37 | protected function configure(): void { |
| 38 | $this |
| 39 | ->setName('libresign:developer:reset') |
| 40 | ->setDescription('Clean all LibreSign data') |
| 41 | ->addOption( |
| 42 | name: 'all', |
| 43 | shortcut: null, |
| 44 | mode: InputOption::VALUE_NONE, |
| 45 | description: 'Reset all' |
| 46 | ) |
| 47 | ->addOption( |
| 48 | name: 'notifications', |
| 49 | shortcut: null, |
| 50 | mode: InputOption::VALUE_OPTIONAL, |
| 51 | description: 'Reset notifications' |
| 52 | ) |
| 53 | ->addOption( |
| 54 | name: 'activity', |
| 55 | shortcut: null, |
| 56 | mode: InputOption::VALUE_OPTIONAL, |
| 57 | description: 'Reset activity' |
| 58 | ) |
| 59 | ->addOption( |
| 60 | name: 'identify', |
| 61 | shortcut: null, |
| 62 | mode: InputOption::VALUE_NONE, |
| 63 | description: 'Reset identify' |
| 64 | ) |
| 65 | ->addOption( |
| 66 | name: 'signrequest', |
| 67 | shortcut: null, |
| 68 | mode: InputOption::VALUE_NONE, |
| 69 | description: 'Reset sign request' |
| 70 | ) |
| 71 | ->addOption( |
| 72 | name: 'file', |
| 73 | shortcut: null, |
| 74 | mode: InputOption::VALUE_NONE, |
| 75 | description: 'Reset file' |
| 76 | ) |
| 77 | ->addOption( |
| 78 | name: 'fileelement', |
| 79 | shortcut: null, |
| 80 | mode: InputOption::VALUE_NONE, |
| 81 | description: 'Reset file element' |
| 82 | ) |
| 83 | ->addOption( |
| 84 | name: 'crl', |
| 85 | shortcut: null, |
| 86 | mode: InputOption::VALUE_NONE, |
| 87 | description: 'Reset crl', |
| 88 | ) |
| 89 | ->addOption( |
| 90 | name: 'userelement', |
| 91 | shortcut: null, |
| 92 | mode: InputOption::VALUE_NONE, |
| 93 | description: 'Reset user element' |
| 94 | ) |
| 95 | ->addOption( |
| 96 | name: 'config', |
| 97 | shortcut: null, |
| 98 | mode: InputOption::VALUE_NONE, |
| 99 | description: 'Reset config' |
| 100 | ) |
| 101 | ->addOption( |
| 102 | name: 'policy', |
| 103 | shortcut: null, |
| 104 | mode: InputOption::VALUE_NONE, |
| 105 | description: 'Reset policy data' |
| 106 | ) |
| 107 | ; |
| 108 | } |
| 109 | |
| 110 | #[\Override] |
| 111 | protected function execute(InputInterface $input, OutputInterface $output): int { |
| 112 | $ok = false; |
| 113 | |
| 114 | try { |
| 115 | $all = $input->getOption('all'); |
| 116 | if ($input->getOption('notifications') || $all) { |
| 117 | $this->resetNotifications((string)$input->getOption('notifications')); |
| 118 | $ok = true; |
| 119 | } |
| 120 | if ($input->getOption('activity') || $all) { |
| 121 | $this->resetActivity((string)$input->getOption('activity')); |
| 122 | $ok = true; |
| 123 | } |
| 124 | if ($input->getOption('identify') || $all) { |
| 125 | $this->resetIdentifyMethods(); |
| 126 | $ok = true; |
| 127 | } |
| 128 | if ($input->getOption('signrequest') || $all) { |
| 129 | $this->resetSignRequest(); |
| 130 | $ok = true; |
| 131 | } |
| 132 | if ($input->getOption('file') || $all) { |
| 133 | $this->resetFile(); |
| 134 | $ok = true; |
| 135 | } |
| 136 | if ($input->getOption('fileelement') || $all) { |
| 137 | $this->resetFileElement(); |
| 138 | $ok = true; |
| 139 | } |
| 140 | if ($input->getOption('crl') || $all) { |
| 141 | $this->resetCrl(); |
| 142 | $ok = true; |
| 143 | } |
| 144 | if ($input->getOption('userelement') || $all) { |
| 145 | $this->resetUserElement(); |
| 146 | $ok = true; |
| 147 | } |
| 148 | if ($input->getOption('config') || $all) { |
| 149 | $this->resetConfig(); |
| 150 | $ok = true; |
| 151 | } |
| 152 | if ($input->getOption('policy') || $all) { |
| 153 | $this->resetPolicy(); |
| 154 | $ok = true; |
| 155 | } |
| 156 | } catch (\Exception $e) { |
| 157 | $this->logger->error($e->getMessage()); |
| 158 | throw $e; |
| 159 | } |
| 160 | |
| 161 | if (!$ok) { |
| 162 | $output->writeln('<error>Please inform what you want to reset</error>'); |
| 163 | $output->writeln('<error>--all to all</error>'); |
| 164 | $output->writeln('<error>--help to check the available options</error>'); |
| 165 | return 1; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | private function resetNotifications(string $user): void { |
| 171 | try { |
| 172 | $delete = $this->db->getQueryBuilder(); |
| 173 | $delete->delete('notifications') |
| 174 | ->where($delete->expr()->eq('app', $delete->createNamedParameter(Application::APP_ID))); |
| 175 | if ($user) { |
| 176 | $delete->andWhere($delete->expr()->eq('user', $delete->createNamedParameter($user))); |
| 177 | } |
| 178 | $delete->executeStatement(); |
| 179 | } catch (\Throwable) { |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private function resetActivity(string $user): void { |
| 184 | try { |
| 185 | $delete = $this->db->getQueryBuilder(); |
| 186 | $delete->delete('activity_mq') |
| 187 | ->where($delete->expr()->eq('amq_appid', $delete->createNamedParameter(Application::APP_ID))); |
| 188 | if ($user) { |
| 189 | $delete->andWhere($delete->expr()->eq('amq_affecteduser', $delete->createNamedParameter($user))); |
| 190 | } |
| 191 | $delete->executeStatement(); |
| 192 | |
| 193 | $delete = $this->db->getQueryBuilder(); |
| 194 | $delete->delete('activity') |
| 195 | ->where($delete->expr()->eq('app', $delete->createNamedParameter(Application::APP_ID))); |
| 196 | if ($user) { |
| 197 | $delete->andWhere($delete->expr()->eq('user', $delete->createNamedParameter($user))); |
| 198 | } |
| 199 | $delete->executeStatement(); |
| 200 | } catch (\Throwable) { |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private function resetIdentifyMethods(): void { |
| 205 | try { |
| 206 | $delete = $this->db->getQueryBuilder(); |
| 207 | $delete->delete('libresign_identify_method') |
| 208 | ->executeStatement(); |
| 209 | } catch (\Throwable) { |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private function resetSignRequest(): void { |
| 214 | try { |
| 215 | $delete = $this->db->getQueryBuilder(); |
| 216 | $delete->delete('libresign_sign_request') |
| 217 | ->executeStatement(); |
| 218 | } catch (\Throwable) { |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private function resetFile(): void { |
| 223 | try { |
| 224 | $delete = $this->db->getQueryBuilder(); |
| 225 | $delete->delete('libresign_file') |
| 226 | ->executeStatement(); |
| 227 | } catch (\Throwable) { |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private function resetFileElement(): void { |
| 232 | try { |
| 233 | $delete = $this->db->getQueryBuilder(); |
| 234 | $delete->delete('libresign_file_element') |
| 235 | ->executeStatement(); |
| 236 | } catch (\Throwable) { |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private function resetCrl(): void { |
| 241 | try { |
| 242 | $delete = $this->db->getQueryBuilder(); |
| 243 | $delete->delete('libresign_crl') |
| 244 | ->executeStatement(); |
| 245 | } catch (\Throwable) { |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private function resetUserElement(): void { |
| 250 | try { |
| 251 | $delete = $this->db->getQueryBuilder(); |
| 252 | $delete->delete('libresign_user_element') |
| 253 | ->executeStatement(); |
| 254 | } catch (\Throwable) { |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private function resetConfig(): void { |
| 259 | try { |
| 260 | $keys = $this->appConfig->getKeys(Application::APP_ID); |
| 261 | foreach ($keys as $key) { |
| 262 | if ($key === 'enabled' || $key === 'installed_version') { |
| 263 | continue; |
| 264 | } |
| 265 | $this->appConfig->deleteKey(Application::APP_ID, $key); |
| 266 | } |
| 267 | } catch (\Throwable) { |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private function resetPolicy(): void { |
| 272 | try { |
| 273 | $delete = $this->db->getQueryBuilder(); |
| 274 | $delete->delete('libresign_permission_set_binding') |
| 275 | ->executeStatement(); |
| 276 | |
| 277 | $delete = $this->db->getQueryBuilder(); |
| 278 | $delete->delete('libresign_permission_set') |
| 279 | ->executeStatement(); |
| 280 | |
| 281 | $delete = $this->db->getQueryBuilder(); |
| 282 | $delete->delete('preferences') |
| 283 | ->where($delete->expr()->eq('appid', $delete->createNamedParameter(Application::APP_ID))) |
| 284 | ->andWhere($delete->expr()->like('configkey', $delete->createNamedParameter('policy.%'))) |
| 285 | ->executeStatement(); |
| 286 | } catch (\Throwable) { |
| 287 | } |
| 288 | } |
| 289 | } |