fix(subsonic): fix JukeboxRole logic in GetUser and eliminate code duplication (#4170)

- Fix GetUser JukeboxRole to properly respect AdminOnly setting

- Extract buildUserResponse helper to eliminate duplication between GetUser and GetUsers

- Fix username field inconsistency (GetUsers was using loggedUser.Name instead of UserName)

- Add comprehensive tests covering jukebox role permissions and consistency between methods

Fixes #4160
This commit is contained in:
Deluan Quintão
2025-06-02 21:34:43 -04:00
committed by GitHub
parent a79e05b648
commit e3527f9c00
2 changed files with 121 additions and 20 deletions
+25 -20
View File
@@ -4,26 +4,40 @@ import (
"net/http"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/server/subsonic/responses"
)
// buildUserResponse creates a User response object from a User model
func buildUserResponse(user model.User) responses.User {
userResponse := responses.User{
Username: user.UserName,
AdminRole: user.IsAdmin,
Email: user.Email,
StreamRole: true,
ScrobblingEnabled: true,
DownloadRole: conf.Server.EnableDownloads,
ShareRole: conf.Server.EnableSharing,
}
if conf.Server.Jukebox.Enabled {
userResponse.JukeboxRole = !conf.Server.Jukebox.AdminOnly || user.IsAdmin
}
return userResponse
}
// TODO This is a placeholder. The real one has to read this info from a config file or the database
func (api *Router) GetUser(r *http.Request) (*responses.Subsonic, error) {
loggedUser, ok := request.UserFrom(r.Context())
if !ok {
return nil, newError(responses.ErrorGeneric, "Internal error")
}
response := newResponse()
response.User = &responses.User{}
response.User.Username = loggedUser.UserName
response.User.AdminRole = loggedUser.IsAdmin
response.User.Email = loggedUser.Email
response.User.StreamRole = true
response.User.ScrobblingEnabled = true
response.User.DownloadRole = conf.Server.EnableDownloads
response.User.ShareRole = conf.Server.EnableSharing
response.User.JukeboxRole = conf.Server.Jukebox.Enabled
user := buildUserResponse(loggedUser)
response.User = &user
return response, nil
}
@@ -32,17 +46,8 @@ func (api *Router) GetUsers(r *http.Request) (*responses.Subsonic, error) {
if !ok {
return nil, newError(responses.ErrorGeneric, "Internal error")
}
user := responses.User{}
user.Username = loggedUser.Name
user.AdminRole = loggedUser.IsAdmin
user.Email = loggedUser.Email
user.StreamRole = true
user.ScrobblingEnabled = true
user.DownloadRole = conf.Server.EnableDownloads
user.ShareRole = conf.Server.EnableSharing
if conf.Server.Jukebox.Enabled {
user.JukeboxRole = !conf.Server.Jukebox.AdminOnly || loggedUser.IsAdmin
}
user := buildUserResponse(loggedUser)
response := newResponse()
response.Users = &responses.Users{User: []responses.User{user}}
return response, nil