feat: add getUser and getUsers endpoints
Feishin calls getUser during login to determine roles and folder access.
This commit is contained in:
1 parent
656020f5c1
commit
43e726628e
4 files changed
+104
-2
No files matched your search
@@ -547,6 +547,8 @@ For every `/rest/*` call (except an optional health check):
|
||||
- `getOpenSubsonicExtensions`: `[{formPost, [1]}, {songLyrics, [1]}, {transcodeOffset, [1]}]`.
|
||||
- `startScan`: admin only; starts an async coroutine scan.
|
||||
- `getScanStatus`: `{"scanStatus": {"scanning": Boolean, "count": Long}}`.
|
||||
- `getUser(username)`: returns roles and visible folder IDs; users may only query themselves, admins may query anyone.
|
||||
- `getUsers`: admin only; lists all configured users.
|
||||
|
||||
### 7.3 Browsing & search
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ fun Route.subsonicRoutes(
|
||||
scanStatus: () -> ScanStatus,
|
||||
) {
|
||||
route("/rest") {
|
||||
systemRoutes(users, startScan, scanStatus)
|
||||
systemRoutes(users, library, startScan, scanStatus)
|
||||
browsingRoutes(users, library)
|
||||
mediaRoutes(users, library, coverArtService)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package win.taptap.musicmanager.subsonic.routes
|
||||
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.route
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.add
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.respondSubsonic
|
||||
@@ -13,6 +17,7 @@ import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
/** System endpoints from the MVP specification. */
|
||||
fun Route.systemRoutes(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
startScan: () -> Boolean,
|
||||
scanStatus: () -> ScanStatus,
|
||||
) {
|
||||
@@ -63,6 +68,66 @@ fun Route.systemRoutes(
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getUser") { call, currentUser ->
|
||||
val username = call.subsonicParams()["username"]
|
||||
if (username.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: username")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
if (username != currentUser.name && !currentUser.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val requested = users.byName(username)
|
||||
if (requested == null) {
|
||||
call.respondSubsonicError(70, "User not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
call.respondSubsonic(buildJsonObject { put("user", userJson(requested, library)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getUsers") { call, currentUser ->
|
||||
if (!currentUser.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("users", buildJsonObject {
|
||||
put("user", buildJsonArray {
|
||||
users.users.forEach { add(userJson(it, library)) }
|
||||
})
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun userJson(user: UserAccount, library: LibraryConfig) = buildJsonObject {
|
||||
put("username", user.name)
|
||||
put("scrobblingEnabled", true)
|
||||
put("adminRole", user.admin)
|
||||
put("settingsRole", user.admin)
|
||||
put("downloadRole", true)
|
||||
put("uploadRole", false)
|
||||
put("playlistRole", true)
|
||||
put("coverArtRole", true)
|
||||
put("commentRole", false)
|
||||
put("podcastRole", false)
|
||||
put("streamRole", true)
|
||||
put("jukeboxRole", false)
|
||||
put("shareRole", false)
|
||||
put("videoConversionRole", false)
|
||||
put("folder", folderIds(user.visibleFolderIds(library)))
|
||||
}
|
||||
|
||||
private fun folderIds(ids: Set<Int>): JsonArray = buildJsonArray {
|
||||
ids.sorted().forEach { add(JsonPrimitive(it)) }
|
||||
}
|
||||
|
||||
private fun extension(name: String, version: Int) = buildJsonObject {
|
||||
|
||||
@@ -114,6 +114,41 @@ class SystemRoutesTest {
|
||||
assertEquals(42, status["count"]!!.jsonPrimitive.int)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getUser returns roles and folder ids with admin restrictions`() = testApplication {
|
||||
setupApp()
|
||||
|
||||
val own = parse(client.get("/rest/getUser?${credentials()}&username=alice").bodyAsText())
|
||||
val alice = own["user"]!!.jsonObject
|
||||
assertEquals("alice", alice.string("username"))
|
||||
assertEquals("true", alice["adminRole"]!!.jsonPrimitive.content)
|
||||
assertEquals("true", alice["downloadRole"]!!.jsonPrimitive.content)
|
||||
assertEquals(listOf(1), alice["folder"]!!.jsonArray.map { it.jsonPrimitive.int })
|
||||
|
||||
val other = parse(client.get("/rest/getUser?${credentials()}&username=bob").bodyAsText())
|
||||
assertEquals("bob", other["user"]!!.jsonObject.string("username"))
|
||||
|
||||
val forbidden = parse(client.get("/rest/getUser?${credentials(user = "bob")}&username=alice").bodyAsText())
|
||||
assertEquals("failed", forbidden.string("status"))
|
||||
assertEquals(50, forbidden["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
|
||||
val missing = parse(client.get("/rest/getUser?${credentials()}").bodyAsText())
|
||||
assertEquals(10, missing["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getUsers is admin only`() = testApplication {
|
||||
setupApp()
|
||||
|
||||
val admin = parse(client.get("/rest/getUsers?${credentials()}").bodyAsText())
|
||||
val names = admin["users"]!!.jsonObject["user"]!!.jsonArray
|
||||
.map { it.jsonObject.string("username") }
|
||||
assertEquals(setOf("alice", "bob"), names.toSet())
|
||||
|
||||
val regular = parse(client.get("/rest/getUsers?${credentials(user = "bob")}").bodyAsText())
|
||||
assertEquals(50, regular["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
}
|
||||
|
||||
private fun ApplicationTestBuilder.setupApp(
|
||||
startScan: () -> Boolean = { false },
|
||||
scanStatus: () -> ScanStatus = { ScanStatus(false, 0) },
|
||||
|
||||
Reference in new issue
Block a user