2020-01-19 14:21:44 -09:00
|
|
|
package subsonic
|
2016-03-03 14:44:39 -09:00
|
|
|
|
2020-01-07 10:56:26 -09:00
|
|
|
import (
|
|
|
|
|
"net/http"
|
2016-03-03 14:44:39 -09:00
|
|
|
|
2023-01-23 19:53:04 -09:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2025-06-02 17:34:43 -08:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-10-27 14:19:56 -08:00
|
|
|
"github.com/navidrome/navidrome/model/request"
|
2020-01-23 15:44:08 -09:00
|
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
2025-12-10 14:30:26 -09:00
|
|
|
"github.com/navidrome/navidrome/utils/req"
|
2025-07-29 13:59:58 -08:00
|
|
|
"github.com/navidrome/navidrome/utils/slice"
|
2020-01-07 10:56:26 -09:00
|
|
|
)
|
|
|
|
|
|
2025-06-02 17:34:43 -08:00
|
|
|
// 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,
|
2025-07-29 13:59:58 -08:00
|
|
|
Folder: slice.Map(user.Libraries, func(lib model.Library) int32 { return int32(lib.ID) }),
|
2025-06-02 17:34:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.Server.Jukebox.Enabled {
|
|
|
|
|
userResponse.JukeboxRole = !conf.Server.Jukebox.AdminOnly || user.IsAdmin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userResponse
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 08:57:56 -09:00
|
|
|
func (api *Router) GetUser(r *http.Request) (*responses.Subsonic, error) {
|
2020-10-27 14:19:56 -08:00
|
|
|
loggedUser, ok := request.UserFrom(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, newError(responses.ErrorGeneric, "Internal error")
|
2020-01-07 10:56:26 -09:00
|
|
|
}
|
2025-12-10 14:30:26 -09:00
|
|
|
username, err := req.Params(r).String("username")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if username != loggedUser.UserName {
|
|
|
|
|
return nil, newError(responses.ErrorAuthorizationFail)
|
|
|
|
|
}
|
2020-08-13 18:11:18 -08:00
|
|
|
response := newResponse()
|
2025-06-02 17:34:43 -08:00
|
|
|
user := buildUserResponse(loggedUser)
|
|
|
|
|
response.User = &user
|
2020-01-07 10:56:26 -09:00
|
|
|
return response, nil
|
2016-03-03 14:44:39 -09:00
|
|
|
}
|
2020-10-27 14:19:56 -08:00
|
|
|
|
2022-11-21 08:57:56 -09:00
|
|
|
func (api *Router) GetUsers(r *http.Request) (*responses.Subsonic, error) {
|
2020-10-27 14:19:56 -08:00
|
|
|
loggedUser, ok := request.UserFrom(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, newError(responses.ErrorGeneric, "Internal error")
|
|
|
|
|
}
|
2025-06-02 17:34:43 -08:00
|
|
|
|
|
|
|
|
user := buildUserResponse(loggedUser)
|
2020-10-27 14:19:56 -08:00
|
|
|
response := newResponse()
|
|
|
|
|
response.Users = &responses.Users{User: []responses.User{user}}
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|