|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2018 Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author 2018 Christoph Wurst <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OC\Authentication\TwoFactorAuth\Db; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
30
|
|
|
use OCP\IDBConnection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Data access object to query and assign (provider_id, uid, enabled) tuples of |
|
34
|
|
|
* 2FA providers |
|
35
|
|
|
*/ |
|
36
|
|
|
class ProviderUserAssignmentDao { |
|
37
|
|
|
|
|
38
|
|
|
const TABLE_NAME = 'twofactor_providers'; |
|
39
|
|
|
|
|
40
|
|
|
/** @var IDBConnection */ |
|
41
|
|
|
private $conn; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(IDBConnection $dbConn) { |
|
44
|
|
|
$this->conn = $dbConn; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get all assigned provider IDs for the given user ID |
|
49
|
|
|
* |
|
50
|
|
|
* @return string[] where the array key is the provider ID (string) and the |
|
51
|
|
|
* value is the enabled state (bool) |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getState(string $uid): array { |
|
54
|
|
|
$qb = $this->conn->getQueryBuilder(); |
|
55
|
|
|
|
|
56
|
|
|
$query = $qb->select('provider_id', 'enabled') |
|
57
|
|
|
->from(self::TABLE_NAME) |
|
58
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
59
|
|
|
$result = $query->execute(); |
|
60
|
|
|
$providers = []; |
|
61
|
|
|
foreach ($result->fetchAll() as $row) { |
|
62
|
|
|
$providers[$row['provider_id']] = 1 === (int) $row['enabled']; |
|
63
|
|
|
} |
|
64
|
|
|
$result->closeCursor(); |
|
65
|
|
|
|
|
66
|
|
|
return $providers; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Persist a new/updated (provider_id, uid, enabled) tuple |
|
71
|
|
|
*/ |
|
72
|
|
|
public function persist(string $providerId, string $uid, int $enabled) { |
|
73
|
|
|
$qb = $this->conn->getQueryBuilder(); |
|
74
|
|
|
|
|
75
|
|
|
// TODO: concurrency? What if (providerId, uid) private key is inserted |
|
76
|
|
|
// twice at the same time? |
|
77
|
|
|
$query = $qb->insert(self::TABLE_NAME)->values([ |
|
78
|
|
|
'provider_id' => $qb->createNamedParameter($providerId), |
|
79
|
|
|
'uid' => $qb->createNamedParameter($uid), |
|
80
|
|
|
'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT), |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
$query->execute(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|