nc-music /
music
| 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 - 2025 |
||||
| 13 | */ |
||||
| 14 | |||||
| 15 | namespace OCA\Music\Db; |
||||
| 16 | |||||
| 17 | use OCA\Music\Utility\ArrayUtil; |
||||
| 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 IDBConnection $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 = [(string)$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 ?array{key_id: int, user_id: string} - null if not found |
||||
| 61 | */ |
||||
| 62 | public function getUserByPasswordHash(string $hash) : ?array { |
||||
| 63 | $sql = 'SELECT `id`, `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 [ |
||||
| 73 | 'key_id' => (int)$row['id'], |
||||
| 74 | 'user_id' => $row['user_id'] |
||||
| 75 | ]; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * @return array of rows like ['user_id' => string, 'hash' => string] with row IDs as keys |
||||
| 80 | */ |
||||
| 81 | public function getUsersAndPasswordHashes() : array { |
||||
| 82 | $sql = 'SELECT `id`, `user_id`, `hash` FROM `*PREFIX*music_ampache_users`'; |
||||
| 83 | $result = $this->db->executeQuery($sql); |
||||
| 84 | $rows = $result->fetchAll(); |
||||
| 85 | |||||
| 86 | return ArrayUtil::columns($rows, ['user_id', 'hash'], 'id'); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * @param string $user Username, case-insensitive |
||||
| 91 | * @return ?string Case-sensitively correct username, if the user has any API key(s) |
||||
| 92 | */ |
||||
| 93 | public function getProperUserId(string $user) : ?string { |
||||
| 94 | $sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE LOWER(`user_id`) = LOWER(?)'; |
||||
| 95 | $params = [$user]; |
||||
| 96 | $result = $this->db->executeQuery($sql, $params); |
||||
| 97 | $row = $result->fetch(); |
||||
| 98 | |||||
| 99 | if ($row === false) { |
||||
| 100 | return null; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | return $row['user_id']; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | public function getUserId(int $id) : ?string { |
||||
| 107 | $sql = 'SELECT `user_id` FROM `*PREFIX*music_ampache_users` WHERE `id` = ?'; |
||||
| 108 | $params = [(string)$id]; |
||||
| 109 | $result = $this->db->executeQuery($sql, $params); |
||||
| 110 | $row = $result->fetch(); |
||||
| 111 | |||||
| 112 | if ($row === false) { |
||||
| 113 | return null; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | return $row['user_id']; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @return ?int ID of the added key or null on failure (which is highly unexpected) |
||||
| 121 | */ |
||||
| 122 | public function addUserKey(string $userId, string $hash, ?string $description) : ?int { |
||||
| 123 | $sql = 'INSERT INTO `*PREFIX*music_ampache_users` |
||||
| 124 | (`user_id`, `hash`, `description`) VALUES (?, ?, ?)'; |
||||
| 125 | $params = [$userId, $hash, $description]; |
||||
| 126 | $affectedRows = $this->db->executeUpdate($sql, $params); |
||||
|
0 ignored issues
–
show
|
|||||
| 127 | |||||
| 128 | return ($affectedRows > 0) ? (int)$this->db->lastInsertId('*PREFIX*music_ampache_users') : null; |
||||
|
0 ignored issues
–
show
The function
OCP\IDBConnection::lastInsertId() has been deprecated: 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. Loading history...
|
|||||
| 129 | } |
||||
| 130 | |||||
| 131 | public function removeUserKey(string $userId, int $id) : void { |
||||
| 132 | $sql = 'DELETE FROM `*PREFIX*music_ampache_users` |
||||
| 133 | WHERE `user_id` = ? AND `id` = ?'; |
||||
| 134 | $params = [$userId, $id]; |
||||
| 135 | $this->db->executeUpdate($sql, $params); |
||||
|
0 ignored issues
–
show
The function
OCP\IDBConnection::executeUpdate() has been deprecated: 21.0.0 use executeStatement
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. Loading history...
|
|||||
| 136 | } |
||||
| 137 | |||||
| 138 | public function getAll(string $userId) : array { |
||||
| 139 | $sql = 'SELECT `id`, `hash`, `description` FROM `*PREFIX*music_ampache_users` |
||||
| 140 | WHERE `user_id` = ?'; |
||||
| 141 | $params = [$userId]; |
||||
| 142 | $result = $this->db->executeQuery($sql, $params); |
||||
| 143 | $rows = $result->fetchAll(); |
||||
| 144 | |||||
| 145 | return $rows; |
||||
| 146 | } |
||||
| 147 | } |
||||
| 148 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.