profiles: add routes for viewing/modifying

most of Profile struct is now in ProfileDTO (which is embedded in the
former), admin can pull it from /profiles/raw/{name} GET, and replace it
with PUT. Will make a ui for this.
This commit is contained in:
Harvey Tindall
2025-11-28 12:06:17 +00:00
parent 836974e1b2
commit 815721adb2
4 changed files with 84 additions and 16 deletions
+1 -1
View File
@@ -148,7 +148,7 @@ SWAGGER_SRC = $(wildcard api*.go) $(wildcard *auth.go) views.go
SWAGGER_TARGET = docs/docs.go
$(SWAGGER_TARGET): $(SWAGGER_SRC)
$(SWAGINSTALL)
swag init -g main.go
swag init --parseDependency --parseInternal -g main.go
VARIANTS_SRC = $(wildcard html/*.html)
VARIANTS_TARGET = $(DATA)/html/admin.html
+61 -1
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"net/url"
"time"
"github.com/gin-gonic/gin"
@@ -69,6 +70,65 @@ func (app *appContext) GetProfiles(gc *gin.Context) {
gc.JSON(200, out)
}
// @Summary Get the raw values stored in a profile (Configuration, Policy, Jellyseerr/Ombi if applicable, etc.).
// @Produce json
// @Success 200 {object} ProfileDTO
// @Failure 400 {object} boolResponse
// @Param name path string true "name of profile (url encoded if necessary)"
// @Router /profiles/raw/{name} [get]
// @Security Bearer
// @tags Users
func (app *appContext) GetRawProfile(gc *gin.Context) {
escapedName := gc.Param("name")
name, err := url.QueryUnescape(escapedName)
if err != nil {
respondBool(400, false, gc)
return
}
if profile, ok := app.storage.GetProfileKey(name); ok {
gc.JSON(200, profile.ProfileDTO)
return
}
respondBool(400, false, gc)
}
// @Summary Update the raw data of a profile (Configuration, Policy, Jellyseerr/Ombi if applicable, etc.).
// @Produce json
// @Param ProfileDTO body ProfileDTO true "Raw profile data (all of it, do not omit anything)"
// @Success 200 {object} boolResponse
// @Failure 400 {object} boolResponse
// @Router /profiles/raw/{name} [put]
// @Security Bearer
// @tags Profiles & Settings
func (app *appContext) ReplaceRawProfile(gc *gin.Context) {
escapedName := gc.Param("name")
name, err := url.QueryUnescape(escapedName)
if err != nil {
respondBool(400, false, gc)
return
}
existingProfile, ok := app.storage.GetProfileKey(name)
if !ok {
respondBool(400, false, gc)
return
}
var req ProfileDTO
gc.BindJSON(&req)
existingProfile.ProfileDTO = req
if req.Name == "" {
req.Name = name
}
app.storage.SetProfileKey(req.Name, existingProfile)
if req.Name != name {
// Name change
app.storage.DeleteProfileKey(name)
if discordEnabled {
app.discord.UpdateCommands()
}
}
respondBool(200, true, gc)
}
// @Summary Set the default profile to use.
// @Produce json
// @Param profileChangeDTO body profileChangeDTO true "Default profile object"
@@ -119,7 +179,7 @@ func (app *appContext) CreateProfile(gc *gin.Context) {
}
profile := Profile{
FromUser: user.Name,
Policy: user.Policy,
ProfileDTO: ProfileDTO{Policy: user.Policy},
Homescreen: req.Homescreen,
}
app.debug.Printf(lm.CreateProfileFromUser, user.Name)
+2
View File
@@ -214,6 +214,8 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
api.POST(p+"/invites/profile", app.SetProfile)
api.GET(p+"/profiles", app.GetProfiles)
api.GET(p+"/profiles/names", app.GetProfileNames)
api.GET(p+"/profiles/raw/:name", app.GetRawProfile)
api.PUT(p+"/profiles/raw/:name", app.ReplaceRawProfile)
api.POST(p+"/profiles/default", app.SetDefaultProfile)
api.POST(p+"/profiles", app.CreateProfile)
api.DELETE(p+"/profiles", app.DeleteProfile)
+11 -5
View File
@@ -748,18 +748,22 @@ type userPageContent struct {
// timePattern: %Y-%m-%dT%H:%M:%S.%f
type Profile struct {
Name string `badgerhold:"key"`
ProfileDTO
Admin bool `json:"admin,omitempty" badgerhold:"index"`
LibraryAccess string `json:"libraries,omitempty"`
FromUser string `json:"fromUser,omitempty"`
Homescreen bool `json:"homescreen"`
Default bool `json:"default,omitempty"`
ReferralTemplateKey string
}
type ProfileDTO struct {
Name string `badgerhold:"key" json:"name"`
Policy mediabrowser.Policy `json:"policy,omitempty"`
Configuration mediabrowser.Configuration `json:"configuration,omitempty"`
Displayprefs map[string]interface{} `json:"displayprefs,omitempty"`
Default bool `json:"default,omitempty"`
Ombi map[string]interface{} `json:"ombi,omitempty"`
Displayprefs map[string]any `json:"displayprefs,omitempty"`
Ombi map[string]any `json:"ombi,omitempty"`
Jellyseerr JellyseerrTemplate `json:"jellyseerr,omitempty"`
ReferralTemplateKey string
}
type JellyseerrTemplate struct {
@@ -1662,9 +1666,11 @@ func (st *Storage) migrateToProfile() error {
st.loadDisplayprefs()
st.loadProfiles()
st.deprecatedProfiles["Default"] = Profile{
ProfileDTO: ProfileDTO{
Policy: st.deprecatedPolicy,
Configuration: st.deprecatedConfiguration,
Displayprefs: st.deprecatedDisplayprefs,
},
}
return st.storeProfiles()
}