Files
pipely/internal/auth/admin_handler.go
T
siujamo 033082954a refactor: rename module to pipely and migrate web UI to MUI
- Rename Go module from onixbyte.com/message-converter-manifest to onixbyte.com/pipely
- Replace shadcn UI with Material UI components across all pages
- Add axios-based API client in shared/web-client with credentials management
- Organise API calls into feature modules: auth, devices, deployments, artefacts, users
- Add .env.example with VITE_API_BASE_URL pointing to pipely.onixbyte.com
- Fix deprecated FormEvent → MouseEvent in dialog submit handlers
2026-07-07 14:40:09 +08:00

94 lines
2.3 KiB
Go

package auth
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"onixbyte.com/pipely/internal/model"
)
// -- Admin user management handlers --
type CreateUserRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
Role string `json:"role"`
}
type ChangePasswordRequest struct {
NewPassword string `json:"newPassword" binding:"required"`
}
// ListUsers handles GET /admin/users.
func (h *Handler) ListUsers(c *gin.Context) {
users, err := h.svc.ListUsers()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if users == nil {
users = []model.AdminUser{}
}
c.JSON(http.StatusOK, gin.H{"data": users})
}
// CreateUser handles POST /admin/users.
func (h *Handler) CreateUser(c *gin.Context) {
var req CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
user, err := h.svc.CreateUser(req.Username, req.Password, req.Role)
if err != nil {
if err == ErrUserExists {
c.JSON(http.StatusConflict, gin.H{"error": "Username already exists"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, user)
}
// DeleteUser handles DELETE /admin/users/:id.
func (h *Handler) DeleteUser(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
if err := h.svc.DeleteUser(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
}
// ChangePassword handles PUT /admin/users/:id/password.
func (h *Handler) ChangePassword(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
var req ChangePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.svc.ChangePassword(id, req.NewPassword); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "password changed"})
}