ds2api / internal /httpapi /admin /accounts /handler_account_role.go
luckfun233
feat: Implement account role management and banning functionality
766bd1c
Raw
History Blame Contribute Delete
2.03 kB
package accounts
import (
"encoding/json"
"net/http"
"net/url"
"github.com/go-chi/chi/v5"
)
func (h *Handler) setAccountRole(w http.ResponseWriter, r *http.Request) {
identifier := chi.URLParam(r, "identifier")
if decoded, err := url.PathUnescape(identifier); err == nil {
identifier = decoded
}
var req struct {
Role string `json:"role"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"})
return
}
acc, err := h.Store.SetAccountRole(identifier, req.Role)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()})
return
}
h.Pool.Reset()
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"identifier": acc.Identifier(),
"role": acc.Role,
})
}
func (h *Handler) setAccountBanned(w http.ResponseWriter, r *http.Request) {
identifier := chi.URLParam(r, "identifier")
if decoded, err := url.PathUnescape(identifier); err == nil {
identifier = decoded
}
var req struct {
Banned bool `json:"banned"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"})
return
}
acc, err := h.Store.SetAccountBanned(identifier, req.Banned)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()})
return
}
h.Pool.Reset()
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"identifier": acc.Identifier(),
"banned": acc.Banned,
})
}
func (h *Handler) promoteStandby(w http.ResponseWriter, r *http.Request) {
var req struct {
Count int `json:"count"`
}
_ = json.NewDecoder(r.Body).Decode(&req)
if req.Count <= 0 {
req.Count = h.Store.RuntimeBackupReleaseCount()
}
promoted := h.Store.PromoteStandbyAccounts(req.Count)
if len(promoted) > 0 {
h.Pool.Reset()
}
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"promoted": promoted,
"count": len(promoted),
})
}