Passed
Push — master ( acedf0...2adfe5 )
by Pauli
04:02
created

AmpacheUserMapper::addUserKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 17
rs 9.9332
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2018 - 2024
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCA\Music\Utility\Util;
18
19
use OCP\IDBConnection;
20
21
/**
22
 * Note: Despite the name, this mapper and the related database table are
23
 *       used both for Subsonic and Ampache users. Also, this isn't really
24
 *       a mapper either, since this does not extend OCP\AppFramework\Db\Mapper.
25
 */
26
class AmpacheUserMapper {
27
	private $db;
28
29
	public function __construct(IDBConnection $db) {
30
		$this->db = $db;
31
	}
32
33
	/**
34
	 * @return string[] Array keys are row IDs and values are hashes
35
	 */
36
	public function getPasswordHashes(string $userId) : array {
37
		$sql = 'SELECT `id`, `hash` FROM `*PREFIX*music_ampache_users` WHERE `user_id` = ?';
38
		$params = [$userId];
39
		$result = $this->db->executeQuery($sql, $params);
40
		$rows = $result->fetchAll();
41
42
		return \array_column($rows, 'hash', 'id');
43
	}
44
45
	public function getPasswordHash(int $id) : ?string {
46
		$sql = 'SELECT `hash` FROM `*PREFIX*music_ampache_users` WHERE `id` = ?';
47
		$params = [$id];
48
		$result = $this->db->executeQuery($sql, $params);
49
		$row = $result->fetch();
50
51
		if ($row === false) {
52
			return null;
53
		}
54
55
		return $row['hash'];
56
	}
57
58
	/**
59
	 * @param string $hash Password hash
60
	 * @return ?string User ID matching the $hash or null if not found
61
	 */
62
	public function getUserByPasswordHash(string $hash) : ?string {
63
		$sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE `hash` = ?';
64
		$params = [$hash];
65
		$result = $this->db->executeQuery($sql, $params);
66
		$row = $result->fetch();
67
68
		if ($row === false) {
69
			return null;
70
		}
71
72
		return $row['user_id'];
73
	}
74
75
	/**
76
	 * @return array of rows like ['user_id' => string, 'hash' => string] with row IDs as keys
77
	 */
78
	public function getUsersAndPasswordHashes() : array {
79
		$sql = 'SELECT `id`, `user_id`, `hash` FROM `*PREFIX*music_ampache_users`';
80
		$result = $this->db->executeQuery($sql);
81
		$rows = $result->fetchAll();
82
83
		return Util::arrayColumns($rows, ['user_id', 'hash'], 'id');
84
	}
85
86
	/**
87
	 * @param string $user Username, case-insensitive
88
	 * @return ?string Case-sensitively correct username, if the user has any API key(s)
89
	 */
90
	public function getProperUserId(string $user) : ?string {
91
		$sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE LOWER(`user_id`) = LOWER(?)';
92
		$params = [$user];
93
		$result = $this->db->executeQuery($sql, $params);
94
		$row = $result->fetch();
95
96
		if ($row === false) {
97
			return null;
98
		}
99
100
		return $row['user_id'];
101
	}
102
103
	public function getUserId(int $id) : ?string {
104
		$sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE `id` = ?';
105
		$params = [$id];
106
		$result = $this->db->executeQuery($sql, $params);
107
		$row = $result->fetch();
108
109
		if ($row === false) {
110
			return null;
111
		}
112
113
		return $row['user_id'];
114
	}
115
116
	/**
117
	 * @return ?int ID of the added key on null on failure (which is highly unexpected)
118
	 */
119
	public function addUserKey(string $userId, string $hash, ?string $description) : ?int {
120
		$sql = 'INSERT INTO `*PREFIX*music_ampache_users`
121
				(`user_id`, `hash`, `description`) VALUES (?, ?, ?)';
122
		$params = [$userId, $hash, $description];
123
		$this->db->executeUpdate($sql, $params);
124
125
		$sql = 'SELECT `id` FROM `*PREFIX*music_ampache_users`
126
				WHERE `user_id` = ? AND `hash` = ?';
127
		$params = [$userId, $hash];
128
		$result = $this->db->executeQuery($sql, $params);
129
		$row = $result->fetch();
130
131
		if ($row === false) {
132
			return null;
133
		}
134
135
		return (int)$row['id'];
136
	}
137
138
	public function removeUserKey(string $userId, int $id) : void {
139
		$sql = 'DELETE FROM `*PREFIX*music_ampache_users`
140
				WHERE `user_id` = ? AND `id` = ?';
141
		$params = [$userId, $id];
142
		$this->db->executeUpdate($sql, $params);
143
	}
144
145
	public function getAll(string $userId) : array {
146
		$sql = 'SELECT `id`, `hash`, `description` FROM `*PREFIX*music_ampache_users`
147
				WHERE `user_id` = ?';
148
		$params = [$userId];
149
		$result = $this->db->executeQuery($sql, $params);
150
		$rows = $result->fetchAll();
151
152
		return $rows;
153
	}
154
}
155