mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-08-03 16:09:34 +00:00
Updates #7985. Squashed commit of the following: commit 1d5a3e66cc046ac8be6c0ebe7f5e06bc3e2a5e72 Merge:bdc76b1b152398e27eAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Aug 28 19:05:51 2025 +0300 Merge branch 'master' into 7985-fix-contol-profile commitbdc76b1b10Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 26 15:47:07 2025 +0300 home: imp code commitaef012ed02Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 26 13:14:39 2025 +0300 home: add tests commit799e8b49a1Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Aug 25 16:59:06 2025 +0300 all: fix control profile
115 lines
2.3 KiB
Go
115 lines
2.3 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
)
|
|
|
|
// Theme is an enum of all allowed UI themes.
|
|
type Theme string
|
|
|
|
// Allowed [Theme] values.
|
|
//
|
|
// Keep in sync with client/src/helpers/constants.ts.
|
|
const (
|
|
ThemeAuto Theme = "auto"
|
|
ThemeLight Theme = "light"
|
|
ThemeDark Theme = "dark"
|
|
)
|
|
|
|
// UnmarshalText implements [encoding.TextUnmarshaler] interface for *Theme.
|
|
func (t *Theme) UnmarshalText(b []byte) (err error) {
|
|
switch string(b) {
|
|
case "auto":
|
|
*t = ThemeAuto
|
|
case "dark":
|
|
*t = ThemeDark
|
|
case "light":
|
|
*t = ThemeLight
|
|
default:
|
|
return fmt.Errorf("invalid theme %q, supported: %q, %q, %q", b, ThemeAuto, ThemeDark, ThemeLight)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// profileJSON is an object for /control/profile and /control/profile/update
|
|
// endpoints.
|
|
type profileJSON struct {
|
|
Name string `json:"name"`
|
|
Language string `json:"language"`
|
|
Theme Theme `json:"theme"`
|
|
}
|
|
|
|
// handleGetProfile is the handler for GET /control/profile endpoint.
|
|
func (web *webAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) {
|
|
var name string
|
|
|
|
if !web.auth.isGLiNet && !web.auth.isUserless {
|
|
u, ok := webUserFromContext(r.Context())
|
|
if !ok {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
}
|
|
|
|
name = string(u.Login)
|
|
}
|
|
|
|
var resp profileJSON
|
|
func() {
|
|
config.RLock()
|
|
defer config.RUnlock()
|
|
|
|
resp = profileJSON{
|
|
Name: name,
|
|
Language: config.Language,
|
|
Theme: config.Theme,
|
|
}
|
|
}()
|
|
|
|
aghhttp.WriteJSONResponseOK(w, r, resp)
|
|
}
|
|
|
|
// handlePutProfile is the handler for PUT /control/profile/update endpoint.
|
|
func (web *webAPI) handlePutProfile(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
if aghhttp.WriteTextPlainDeprecated(w, r) {
|
|
return
|
|
}
|
|
|
|
profileReq := &profileJSON{}
|
|
err := json.NewDecoder(r.Body).Decode(profileReq)
|
|
if err != nil {
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "reading req: %s", err)
|
|
|
|
return
|
|
}
|
|
|
|
lang := profileReq.Language
|
|
if !allowedLanguages.Has(lang) {
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang)
|
|
|
|
return
|
|
}
|
|
|
|
theme := profileReq.Theme
|
|
|
|
func() {
|
|
config.Lock()
|
|
defer config.Unlock()
|
|
|
|
config.Language = lang
|
|
config.Theme = theme
|
|
web.logger.InfoContext(ctx, "profile updated", "lang", lang, "theme", theme)
|
|
}()
|
|
|
|
web.confModifier.Apply(ctx)
|
|
|
|
aghhttp.OK(w)
|
|
}
|