|
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 OCP\IDBConnection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Note: Despite the name, this mapper and the related database table are |
|
21
|
|
|
* used both for Subsonic and Ampache users. Also, this isn't really |
|
22
|
|
|
* a mapper either, since this does not extend OCP\AppFramework\Db\Mapper. |
|
23
|
|
|
*/ |
|
24
|
|
|
class AmpacheUserMapper { |
|
25
|
|
|
private $db; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(IDBConnection $db) { |
|
28
|
|
|
$this->db = $db; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string[] Array keys are row IDs and values are hashes |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getPasswordHashes(string $userId) : array { |
|
35
|
|
|
$sql = 'SELECT `id`, `hash` FROM `*PREFIX*music_ampache_users` WHERE `user_id` = ?'; |
|
36
|
|
|
$params = [$userId]; |
|
37
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
38
|
|
|
$rows = $result->fetchAll(); |
|
39
|
|
|
|
|
40
|
|
|
$hashes = []; |
|
41
|
|
|
foreach ($rows as $value) { |
|
42
|
|
|
$hashes[$value['id']] = $value['hash']; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $hashes; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getPasswordHash(int $id) : ?string { |
|
49
|
|
|
$sql = 'SELECT `hash` FROM `*PREFIX*music_ampache_users` WHERE `id` = ?'; |
|
50
|
|
|
$params = [$id]; |
|
51
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
52
|
|
|
$row = $result->fetch(); |
|
53
|
|
|
|
|
54
|
|
|
if ($row === false) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $row['hash']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $user Username, case-insensitive |
|
63
|
|
|
* @return ?string Case-sensitively correct username, if the user has any API key(s) |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getProperUserId(string $user) : ?string { |
|
66
|
|
|
$sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE LOWER(`user_id`) = LOWER(?)'; |
|
67
|
|
|
$params = [$user]; |
|
68
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
69
|
|
|
$row = $result->fetch(); |
|
70
|
|
|
|
|
71
|
|
|
if ($row === false) { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $row['user_id']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getUserId(int $id) : ?string { |
|
79
|
|
|
$sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE `id` = ?'; |
|
80
|
|
|
$params = [$id]; |
|
81
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
82
|
|
|
$row = $result->fetch(); |
|
83
|
|
|
|
|
84
|
|
|
if ($row === false) { |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $row['user_id']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return ?int ID of the added key on null on failure (which is highly unexpected) |
|
93
|
|
|
*/ |
|
94
|
|
|
public function addUserKey(string $userId, string $hash, ?string $description) : ?int { |
|
95
|
|
|
$sql = 'INSERT INTO `*PREFIX*music_ampache_users` |
|
96
|
|
|
(`user_id`, `hash`, `description`) VALUES (?, ?, ?)'; |
|
97
|
|
|
$params = [$userId, $hash, $description]; |
|
98
|
|
|
$this->db->executeUpdate($sql, $params); |
|
99
|
|
|
|
|
100
|
|
|
$sql = 'SELECT `id` FROM `*PREFIX*music_ampache_users` |
|
101
|
|
|
WHERE `user_id` = ? AND `hash` = ?'; |
|
102
|
|
|
$params = [$userId, $hash]; |
|
103
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
104
|
|
|
$row = $result->fetch(); |
|
105
|
|
|
|
|
106
|
|
|
if ($row === false) { |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return (int)$row['id']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function removeUserKey(string $userId, int $id) : void { |
|
114
|
|
|
$sql = 'DELETE FROM `*PREFIX*music_ampache_users` |
|
115
|
|
|
WHERE `user_id` = ? AND `id` = ?'; |
|
116
|
|
|
$params = [$userId, $id]; |
|
117
|
|
|
$this->db->executeUpdate($sql, $params); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getAll(string $userId) : array { |
|
121
|
|
|
$sql = 'SELECT `id`, `hash`, `description` FROM `*PREFIX*music_ampache_users` |
|
122
|
|
|
WHERE `user_id` = ?'; |
|
123
|
|
|
$params = [$userId]; |
|
124
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
125
|
|
|
$rows = $result->fetchAll(); |
|
126
|
|
|
|
|
127
|
|
|
return $rows; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|