|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\TwoFactorBackupCodes\Migration; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
27
|
|
|
use OCP\IConfig; |
|
28
|
|
|
use OCP\IDBConnection; |
|
29
|
|
|
use OCP\Migration\IOutput; |
|
30
|
|
|
use OCP\Migration\IRepairStep; |
|
31
|
|
|
|
|
32
|
|
|
class CopyEntriesFromOldTable implements IRepairStep { |
|
33
|
|
|
|
|
34
|
|
|
/** @var IDBConnection */ |
|
35
|
|
|
protected $connection; |
|
36
|
|
|
|
|
37
|
|
|
/** @var IConfig */ |
|
38
|
|
|
protected $config; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param IDBConnection $connection |
|
42
|
|
|
* @param IConfig $config |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(IDBConnection $connection, IConfig $config) { |
|
45
|
|
|
$this->connection = $connection; |
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the step's name |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
* @since 9.1.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getName() { |
|
56
|
|
|
return 'Copy twofactor backup codes from legacy table'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Run repair step. |
|
61
|
|
|
* Must throw exception on error. |
|
62
|
|
|
* |
|
63
|
|
|
* @since 9.1.0 |
|
64
|
|
|
* @param IOutput $output |
|
65
|
|
|
* @throws \Exception in case of failure |
|
66
|
|
|
*/ |
|
67
|
|
|
public function run(IOutput $output) { |
|
68
|
|
|
$version = $this->config->getAppValue('twofactor_backupcodes', 'installed_version', '0.0.0'); |
|
69
|
|
|
if (version_compare($version, '1.1.1', '>=')) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!$this->connection->tableExists('twofactor_backup_codes')) { |
|
74
|
|
|
// Legacy table does not exist |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
79
|
|
|
$insert->insert('twofactor_backupcodes') |
|
80
|
|
|
->values([ |
|
81
|
|
|
// Inserting with id might fail: 'id' => $insert->createParameter('id'), |
|
|
|
|
|
|
82
|
|
|
'user_id' => $insert->createParameter('user_id'), |
|
83
|
|
|
'code' => $insert->createParameter('code'), |
|
84
|
|
|
'used' => $insert->createParameter('used'), |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
88
|
|
|
$query->select('*') |
|
89
|
|
|
->from('twofactor_backup_codes') |
|
90
|
|
|
->orderBy('id', 'ASC'); |
|
91
|
|
|
$result = $query->execute(); |
|
92
|
|
|
|
|
93
|
|
|
$output->startProgress(); |
|
94
|
|
|
while ($row = $result->fetch()) { |
|
95
|
|
|
$output->advance(); |
|
96
|
|
|
|
|
97
|
|
|
$insert |
|
98
|
|
|
// Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT) |
|
|
|
|
|
|
99
|
|
|
->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR) |
|
100
|
|
|
->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR) |
|
101
|
|
|
->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT) |
|
102
|
|
|
->execute(); |
|
103
|
|
|
} |
|
104
|
|
|
$output->finishProgress(); |
|
105
|
|
|
|
|
106
|
|
|
$this->connection->dropTable('twofactor_backup_codes'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.