94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
|
|
package auth
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"onixbyte.com/message-converter-manifest/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"})
|
||
|
|
}
|