136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"0451meishiditu/backend/internal/models"
|
|
"0451meishiditu/backend/internal/resp"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func (h *Handlers) AdminListAdmins(c *gin.Context) {
|
|
page, pageSize := parsePage(c.Query("page"), c.Query("page_size"))
|
|
keyword := strings.TrimSpace(c.Query("keyword"))
|
|
|
|
q := h.db.Model(&models.AdminUser{})
|
|
if keyword != "" {
|
|
q = q.Where("username LIKE ?", "%"+keyword+"%")
|
|
}
|
|
|
|
var total int64
|
|
if err := q.Count(&total).Error; err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "db error")
|
|
return
|
|
}
|
|
|
|
var items []models.AdminUser
|
|
if err := q.Order("id desc").Limit(pageSize).Offset((page-1)*pageSize).Find(&items).Error; err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "db error")
|
|
return
|
|
}
|
|
|
|
resp.OKMeta(c, items, gin.H{
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
"total": total,
|
|
"total_page": calcTotalPage(total, pageSize),
|
|
})
|
|
}
|
|
|
|
type adminCreateReq struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
Role string `json:"role"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
|
|
func (h *Handlers) AdminCreateAdmin(c *gin.Context) {
|
|
var req adminCreateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
resp.Fail(c, http.StatusBadRequest, "invalid payload")
|
|
return
|
|
}
|
|
req.Username = strings.TrimSpace(req.Username)
|
|
if req.Username == "" || len(req.Password) < 6 {
|
|
resp.Fail(c, http.StatusBadRequest, "invalid username or password")
|
|
return
|
|
}
|
|
if req.Role == "" {
|
|
req.Role = "admin"
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "hash error")
|
|
return
|
|
}
|
|
|
|
enabled := true
|
|
if req.Enabled != nil {
|
|
enabled = *req.Enabled
|
|
}
|
|
|
|
item := models.AdminUser{
|
|
Username: req.Username,
|
|
PasswordHash: string(hash),
|
|
Role: req.Role,
|
|
Enabled: enabled,
|
|
}
|
|
|
|
if err := h.db.Create(&item).Error; err != nil {
|
|
resp.Fail(c, http.StatusBadRequest, "create failed")
|
|
return
|
|
}
|
|
resp.OK(c, item)
|
|
}
|
|
|
|
type adminPasswordReq struct {
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
func (h *Handlers) AdminUpdateAdminPassword(c *gin.Context) {
|
|
id64, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req adminPasswordReq
|
|
if err := c.ShouldBindJSON(&req); err != nil || len(req.Password) < 6 {
|
|
resp.Fail(c, http.StatusBadRequest, "invalid payload")
|
|
return
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "hash error")
|
|
return
|
|
}
|
|
|
|
if err := h.db.Model(&models.AdminUser{}).Where("id = ?", uint(id64)).
|
|
Update("password_hash", string(hash)).Error; err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "update failed")
|
|
return
|
|
}
|
|
resp.OK(c, gin.H{"updated": true})
|
|
}
|
|
|
|
type adminEnabledReq struct {
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
func (h *Handlers) AdminUpdateAdminEnabled(c *gin.Context) {
|
|
id64, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req adminEnabledReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
resp.Fail(c, http.StatusBadRequest, "invalid payload")
|
|
return
|
|
}
|
|
if err := h.db.Model(&models.AdminUser{}).Where("id = ?", uint(id64)).
|
|
Update("enabled", req.Enabled).Error; err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "update failed")
|
|
return
|
|
}
|
|
resp.OK(c, gin.H{"updated": true})
|
|
}
|
|
|