package api import ( "log/slog" "net/http" "github.com/vblogio/upandown/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 } if h.OnUserRemoved != nil { h.OnUserRemoved(id) } if err := h.Spaces.RetireUser(id); err != nil { slog.Warn("espace personnel non mis de côté", "user", id, "err", err) } slog.Info("compte supprimé", "user", id, "by", userID(r)) w.WriteHeader(http.StatusNoContent) }