Completed
Push — master ( 34c17b...548f4e )
by Roeland
63:38 queued 45:49
created

ProviderUserAssignmentDao::deleteAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
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 Doctrine\DBAL\Exception\UniqueConstraintViolationException;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IDBConnection;
32
33
/**
34
 * Data access object to query and assign (provider_id, uid, enabled) tuples of
35
 * 2FA providers
36
 */
37
class ProviderUserAssignmentDao {
38
39
	const TABLE_NAME = 'twofactor_providers';
40
41
	/** @var IDBConnection */
42
	private $conn;
43
44
	public function __construct(IDBConnection $dbConn) {
45
		$this->conn = $dbConn;
46
	}
47
48
	/**
49
	 * Get all assigned provider IDs for the given user ID
50
	 *
51
	 * @return string[] where the array key is the provider ID (string) and the
52
	 *                  value is the enabled state (bool)
53
	 */
54
	public function getState(string $uid): array {
55
		$qb = $this->conn->getQueryBuilder();
56
57
		$query = $qb->select('provider_id', 'enabled')
58
			->from(self::TABLE_NAME)
59
			->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
60
		$result = $query->execute();
61
		$providers = [];
62
		foreach ($result->fetchAll() as $row) {
63
			$providers[$row['provider_id']] = 1 === (int)$row['enabled'];
64
		}
65
		$result->closeCursor();
66
67
		return $providers;
68
	}
69
70
	/**
71
	 * Persist a new/updated (provider_id, uid, enabled) tuple
72
	 */
73
	public function persist(string $providerId, string $uid, int $enabled) {
74
		$qb = $this->conn->getQueryBuilder();
75
76
		try {
77
			// Insert a new entry
78
			$insertQuery = $qb->insert(self::TABLE_NAME)->values([
79
				'provider_id' => $qb->createNamedParameter($providerId),
80
				'uid' => $qb->createNamedParameter($uid),
81
				'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT),
82
			]);
83
84
			$insertQuery->execute();
85
		} catch (UniqueConstraintViolationException $ex) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\...raintViolationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86
			// There is already an entry -> update it
87
			$updateQuery = $qb->update(self::TABLE_NAME)
88
				->set('enabled', $qb->createNamedParameter($enabled))
89
				->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
90
				->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
91
			$updateQuery->execute();
92
		}
93
94
	}
95
96 View Code Duplication
	public function deleteAll(string $providerId) {
97
		$qb = $this->conn->getQueryBuilder();
98
99
		$deleteQuery = $qb->delete(self::TABLE_NAME)
100
			->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)));
101
102
		$deleteQuery->execute();
103
	}
104
105
}
106