Files
minstrel/internal/subsonic/user.go
T
bvandeusenandClaude Opus 5.5 d191a28217 docs: server comments say what the code does, not how it came to be (#6277)
Comment blocks in cmd/minstrel and 30 smaller internal packages, rewritten
to rule 32's standard with each citation read in context: task, milestone
and rule ids go, and so do the stories they were attached to ("used to",
"Before #N", measurements from one install, quotes, pointers to Fabled
Librarian's records and to docs/superpowers files that are not in the
repo). Where a story wrapped a real constraint, the constraint stays as a
present-tense fact.

Comments that had gone stale are corrected:
- taste.Config: the tuned values are overlaid by internal/recsettings;
  it is no longer "a later follow-up".
- StorageConfig: the data dir holds art, caches and the stream secret,
  not only playlist covers.
- the Subsonic album list and getUser comments describe what they do
  today rather than a milestone that would change it.

Checked: the non-comment token stream of every changed file matches
HEAD, and gofmt -l is empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:15:05 -04:00

80 lines
3.1 KiB
Go

package subsonic
import (
"encoding/xml"
"net/http"
"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"`
}
// handleGetUser returns the authenticated user when no username is provided
// or when the requested username matches; admins may look up any user. Per
// spec a non-admin asking about another user gets ErrNotAuthorized.
func handleGetUser(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 && !caller.IsAdmin {
WriteFail(w, r, ErrNotAuthorized, "Not authorized to view other users")
return
}
// No per-user lookup: an admin asking for another username is answered
// with the caller's own roles.
Write(w, r, UserResponse{
Envelope: NewEnvelope("ok"),
User: userPayload(caller),
})
}
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,
}
}