Files
minstrel/internal/subsonic/user.go
T
bvandeusenandClaude Opus 5.5 201ad741ea
release / web (push) Successful in 1m33s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 2m53s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 3m32s
release / Build signed APK (push) Canceled after 3m3s
Revert the deliberate break; the password-cost check is an ordinary test (#8298, #8330)
Run 11288 failed every named test it reached. The api TestMain's cost
check exited the test binary, and the integration lane runs each package's
binary once with no tests to warm the build, so it stopped the lane there
before coverart's database tests ran. The check is now a test of its own:
a weakened cost fails one test and the rest of the lane still runs.

This reverts commit e717e4370d.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 14:19:14 -05:00

136 lines
4.6 KiB
Go

package subsonic
import (
"context"
"encoding/xml"
"errors"
"net/http"
"github.com/jackc/pgx/v5"
"github.com/Fabled-Sword/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 and getUsers. lookup finds a user by exact
// username and list lists every user; they are fields so tests can answer
// without a database.
type userHandlers struct {
lookup func(ctx context.Context, username string) (dbq.User, error)
list func(ctx context.Context) ([]dbq.ListUsersRow, error)
}
// UsersResponse is the body of getUsers.
type UsersResponse struct {
Envelope
Users UsersContainer `json:"users" xml:"users"`
}
type UsersContainer struct {
Users []User `json:"user" xml:"user"`
}
// getUsers lists every user, for an admin only; anyone else gets
// ErrNotAuthorized, as the spec has it.
func (h *userHandlers) getUsers(w http.ResponseWriter, r *http.Request) {
caller, ok := requireUser(w, r)
if !ok {
return
}
if !caller.IsAdmin {
WriteFail(w, r, ErrNotAuthorized, "Only an admin can list users")
return
}
rows, err := h.list(r.Context())
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to list users")
return
}
users := make([]User, 0, len(rows))
for _, row := range rows {
users = append(users, rolePayload(row.Username, row.IsAdmin))
}
Write(w, r, UsersResponse{Envelope: NewEnvelope("ok"), Users: UsersContainer{Users: users}})
}
// 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 := requireUser(w, r)
if !ok {
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 rolePayload(u.Username, u.IsAdmin) }
// rolePayload is the role bag for a user with this name and admin flag.
func rolePayload(username string, admin bool) User {
return User{
Username: username,
ScrobblingEnabled: true,
AdminRole: admin,
SettingsRole: admin,
DownloadRole: true,
UploadRole: false,
PlaylistRole: true,
CoverArtRole: true,
CommentRole: false,
PodcastRole: false,
StreamRole: true,
JukeboxRole: false,
ShareRole: false,
}
}