Files
minstrel/internal/subsonic/user.go
T
bvandeusenandClaude Opus 5.5 09e78962b2 fix(subsonic): getUser answers an admin's lookup with the requested user (#6292)
An admin asking for another username got their own name and roles back.
The handler now looks the user up by exact username and answers with
that user's payload; an unknown username is ErrDataNotFound. A
non-admin asking about someone else is still ErrNotAuthorized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:58:09 -04:00

99 lines
3.6 KiB
Go

package subsonic
import (
"context"
"encoding/xml"
"errors"
"net/http"
"github.com/jackc/pgx/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// UserResponse wraps the User payload Subsonic clients fetch right after
// login to discover what the authenticated identity can do. Feishin uses
// this to gate UI affordances; without it the login flow stalls.
type UserResponse struct {
Envelope
User User `json:"user" xml:"user"`
}
// User mirrors the role-bag from the Subsonic spec. We expose the full set
// even though Minstrel doesn't model fine-grained ACLs yet — admins get
// every role, non-admins get the "play music" subset. This keeps clients
// from disabling features they could otherwise use.
type User struct {
XMLName xml.Name `json:"-" xml:"user"`
Username string `json:"username" xml:"username,attr"`
Email string `json:"email,omitempty" xml:"email,attr,omitempty"`
ScrobblingEnabled bool `json:"scrobblingEnabled" xml:"scrobblingEnabled,attr"`
AdminRole bool `json:"adminRole" xml:"adminRole,attr"`
SettingsRole bool `json:"settingsRole" xml:"settingsRole,attr"`
DownloadRole bool `json:"downloadRole" xml:"downloadRole,attr"`
UploadRole bool `json:"uploadRole" xml:"uploadRole,attr"`
PlaylistRole bool `json:"playlistRole" xml:"playlistRole,attr"`
CoverArtRole bool `json:"coverArtRole" xml:"coverArtRole,attr"`
CommentRole bool `json:"commentRole" xml:"commentRole,attr"`
PodcastRole bool `json:"podcastRole" xml:"podcastRole,attr"`
StreamRole bool `json:"streamRole" xml:"streamRole,attr"`
JukeboxRole bool `json:"jukeboxRole" xml:"jukeboxRole,attr"`
ShareRole bool `json:"shareRole" xml:"shareRole,attr"`
VideoConversionRole bool `json:"videoConversionRole" xml:"videoConversionRole,attr"`
}
// userHandlers serves getUser. lookup finds a user by exact username; it is
// a field so tests can answer without a database.
type userHandlers struct {
lookup func(ctx context.Context, username string) (dbq.User, error)
}
// getUser returns the authenticated user when no username is provided or
// when the requested username matches; admins may look up any user, and an
// unknown username is ErrDataNotFound. Per spec a non-admin asking about
// another user gets ErrNotAuthorized.
func (h *userHandlers) getUser(w http.ResponseWriter, r *http.Request) {
caller, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
requested := r.URL.Query().Get("username")
if requested == "" || requested == caller.Username {
Write(w, r, UserResponse{Envelope: NewEnvelope("ok"), User: userPayload(caller)})
return
}
if !caller.IsAdmin {
WriteFail(w, r, ErrNotAuthorized, "Not authorized to view other users")
return
}
target, err := h.lookup(r.Context(), requested)
if errors.Is(err, pgx.ErrNoRows) {
WriteFail(w, r, ErrDataNotFound, "User not found")
return
}
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to look up user")
return
}
Write(w, r, UserResponse{Envelope: NewEnvelope("ok"), User: userPayload(target)})
}
func userPayload(u dbq.User) User {
return User{
Username: u.Username,
ScrobblingEnabled: true,
AdminRole: u.IsAdmin,
SettingsRole: u.IsAdmin,
DownloadRole: true,
UploadRole: false,
PlaylistRole: true,
CoverArtRole: true,
CommentRole: false,
PodcastRole: false,
StreamRole: true,
JukeboxRole: false,
ShareRole: false,
}
}