Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
93 lines
3.2 KiB
Go
93 lines
3.2 KiB
Go
// cmd/gateway/admin_user_handlers.go 提供管理员维护管理账号的接口,包括创建、更新、禁用和列表查询。
|
|
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/tursom/mc-gateway/internal/adminhttp"
|
|
)
|
|
|
|
func handleAdminUsersList(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := requireRole(w, r, adminRoleAdmin); !ok {
|
|
return
|
|
}
|
|
users, err := listUsers(r.Context())
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"users": users})
|
|
}
|
|
|
|
func handleAdminUsersCreate(w http.ResponseWriter, r *http.Request) {
|
|
session, ok := requireRole(w, r, adminRoleAdmin)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var req adminhttp.CreateUserRequest
|
|
if !adminhttp.DecodeJSONRequest(w, r, &req) {
|
|
return
|
|
}
|
|
err := createUser(r.Context(), session.Username, req.Username, req.Role, req.Password, req.Disabled)
|
|
if err != nil {
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_create", "user", req.Username, false, err.Error())
|
|
adminhttp.WriteAPIError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_create", "user", req.Username, true, "user created")
|
|
adminhttp.WriteJSON(w, http.StatusCreated, map[string]any{"ok": true})
|
|
}
|
|
|
|
func handleAdminUserItem(w http.ResponseWriter, r *http.Request, rawUsername string) {
|
|
session, ok := requireRole(w, r, adminRoleAdmin)
|
|
if !ok {
|
|
return
|
|
}
|
|
username, err := adminhttp.PathSegment(rawUsername)
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodPatch:
|
|
var req adminhttp.PatchUserRequest
|
|
if !adminhttp.DecodeJSONRequest(w, r, &req) {
|
|
return
|
|
}
|
|
err := patchUser(r.Context(), session.Username, username, req.Role, req.Disabled, req.Password)
|
|
if err != nil {
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_patch", "user", username, false, err.Error())
|
|
adminhttp.WriteAPIError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_patch", "user", username, true, "user updated")
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
case http.MethodDelete:
|
|
err := deleteUser(r.Context(), username)
|
|
if err != nil {
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_delete", "user", username, false, err.Error())
|
|
adminhttp.WriteAPIError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "user_delete", "user", username, true, "user deleted")
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
default:
|
|
adminhttp.WriteAPIError(w, http.StatusMethodNotAllowed, "method not allowed")
|
|
}
|
|
}
|
|
|
|
func handleAdminAuditLogs(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := requireRole(w, r, adminRoleAdmin); !ok {
|
|
return
|
|
}
|
|
logs, err := listAuditLogs(r.Context())
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"audit_logs": logs})
|
|
}
|