Files
stackwatch/internal/api/admin.go
T
claude BotandClaude Sonnet 5.5 56e1bbe842 Stackwatch : suivi des mises à jour de stacks docker compose via SSH
Base Up And Down (auth, thème, dialogues) + moteur fleet (script distant POSIX,
analyse pull + comparaison d'images, mise à jour d'une stack), interface de suivi.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 08:37:40 +01:00

83 lines
2.0 KiB
Go

package api
import (
"log/slog"
"net/http"
"github.com/vblogio/stackwatch/internal/auth"
)
func (h *Handler) adminUsers(w http.ResponseWriter, r *http.Request) {
if !h.admin(w, r) {
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, http.StatusOK, h.Accounts.List())
}
func (h *Handler) adminCreateUser(w http.ResponseWriter, r *http.Request) {
if !h.admin(w, r) {
return
}
var req struct {
Username string `json:"username"`
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
Role string `json:"role"`
}
if !h.decode(w, r, &req) {
return
}
acc, err := h.Accounts.CreateLocal(auth.NewAccount{Username: req.Username, Name: req.Name, Email: req.Email,
Password: req.Password, Role: req.Role, Status: auth.StatusActive})
if err != nil {
accountErr(w, err)
return
}
slog.Info("compte créé", "user", acc.ID, "by", UserID(r))
writeJSON(w, http.StatusCreated, acc.Info())
}
func (h *Handler) adminPatchUser(w http.ResponseWriter, r *http.Request) {
if !h.admin(w, r) {
return
}
var req struct {
Name *string `json:"name"`
Email *string `json:"email"`
Role *string `json:"role"`
Status *string `json:"status"`
Password *string `json:"password"`
}
if !h.decode(w, r, &req) {
return
}
id := r.PathValue("id")
acc, err := h.Accounts.Update(id, auth.Patch{Name: req.Name, Email: req.Email, Role: req.Role,
Status: req.Status, Password: req.Password})
if err != nil {
accountErr(w, err)
return
}
slog.Info("compte modifié", "user", id, "by", UserID(r))
writeJSON(w, http.StatusOK, acc.Info())
}
func (h *Handler) adminDeleteUser(w http.ResponseWriter, r *http.Request) {
if !h.admin(w, r) {
return
}
id := r.PathValue("id")
if id == UserID(r) {
writeErr(w, http.StatusBadRequest, "vous ne pouvez pas supprimer votre propre compte")
return
}
if err := h.Accounts.Delete(id); err != nil {
accountErr(w, err)
return
}
slog.Info("compte supprimé", "user", id, "by", UserID(r))
w.WriteHeader(http.StatusNoContent)
}