|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Christoph Wurst <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
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 |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\TwoFactorBackupCodes\Db; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\AppFramework\Db\Mapper; |
|
25
|
|
|
use OCP\IDBConnection; |
|
26
|
|
|
use OCP\IUser; |
|
27
|
|
|
|
|
28
|
|
|
class BackupCodeMapper extends Mapper { |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(IDBConnection $db) { |
|
31
|
|
|
parent::__construct($db, 'twofactor_backup_codes'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param IUser $user |
|
36
|
|
|
* @return BackupCode[] |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getBackupCodes(IUser $user) { |
|
39
|
|
|
/* @var IQueryBuilder $qb */ |
|
40
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
41
|
|
|
|
|
42
|
|
|
$qb->select('id', 'user_id', 'code', 'used') |
|
43
|
|
|
->from('twofactor_backup_codes') |
|
44
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); |
|
45
|
|
|
$result = $qb->execute(); |
|
46
|
|
|
|
|
47
|
|
|
$rows = $result->fetchAll(); |
|
48
|
|
|
$result->closeCursor(); |
|
49
|
|
|
|
|
50
|
|
|
return array_map(function ($row) { |
|
51
|
|
|
return BackupCode::fromRow($row); |
|
52
|
|
|
}, $rows); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param IUser $user |
|
57
|
|
|
*/ |
|
58
|
|
|
public function deleteCodes(IUser $user) { |
|
59
|
|
|
$this->deleteCodesByUserId($user->getUID()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $uid |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function deleteCodesByUserId($uid) { |
|
|
|
|
|
|
66
|
|
|
/* @var IQueryBuilder $qb */ |
|
67
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
68
|
|
|
|
|
69
|
|
|
$qb->delete('twofactor_backup_codes') |
|
70
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid))); |
|
71
|
|
|
$qb->execute(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.