后端: - 新增全局设备管理 API(DeviceHandler.GetAllDevices) - 新增登录日志导出功能(LogHandler.ExportLoginLogs, CSV/XLSX) - 新增设置服务(SettingsService)和设置页面 API - 设备管理支持多条件筛选(状态/信任状态/关键词) - 登录日志支持流式导出防 OOM - 操作日志支持按方法/时间范围搜索 - 主题配置服务(ThemeService) - 增强监控健康检查(Prometheus metrics + SLO) - 移除旧 ratelimit.go(已迁移至 robustness) - 修复 SocialAccount NULL 扫描问题 - 新增 API 契约测试、Handler 测试、Settings 测试 前端: - 新增管理员设备管理页面(DevicesPage) - 新增管理员登录日志导出功能 - 新增系统设置页面(SettingsPage) - 设备管理支持筛选和分页 - 增强 HTTP 响应类型 测试: - 业务逻辑测试 68 个(含并发 CONC_001~003) - 规模测试 16 个(P99 百分位统计) - E2E 测试、集成测试、契约测试 - 性能基准测试、鲁棒性测试 全面测试通过(38 个测试包)
282 lines
6.7 KiB
Go
282 lines
6.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/user-management-system/internal/auth"
|
|
"github.com/user-management-system/internal/domain"
|
|
"github.com/user-management-system/internal/service"
|
|
)
|
|
|
|
// UserHandler handles user management requests
|
|
type UserHandler struct {
|
|
userService *service.UserService
|
|
}
|
|
|
|
// NewUserHandler creates a new UserHandler
|
|
func NewUserHandler(userService *service.UserService) *UserHandler {
|
|
return &UserHandler{userService: userService}
|
|
}
|
|
|
|
func (h *UserHandler) CreateUser(c *gin.Context) {
|
|
var req struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
Nickname string `json:"nickname"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
user := &domain.User{
|
|
Username: req.Username,
|
|
Email: domain.StrPtr(req.Email),
|
|
Nickname: req.Nickname,
|
|
Status: domain.UserStatusActive,
|
|
}
|
|
|
|
if req.Password != "" {
|
|
hashed, err := auth.HashPassword(req.Password)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to hash password"})
|
|
return
|
|
}
|
|
user.Password = hashed
|
|
}
|
|
|
|
if err := h.userService.Create(c.Request.Context(), user); err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, toUserResponse(user))
|
|
}
|
|
|
|
func (h *UserHandler) ListUsers(c *gin.Context) {
|
|
cursor := c.Query("cursor")
|
|
sizeStr := c.DefaultQuery("size", "")
|
|
|
|
// Use cursor-based pagination when cursor is provided
|
|
if cursor != "" || sizeStr != "" {
|
|
var req service.ListCursorRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
result, err := h.userService.ListCursor(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, result)
|
|
return
|
|
}
|
|
|
|
// Fallback to legacy offset-based pagination
|
|
offset, _ := strconv.ParseInt(c.DefaultQuery("offset", "0"), 10, 64)
|
|
limit, _ := strconv.ParseInt(c.DefaultQuery("limit", "20"), 10, 64)
|
|
|
|
users, total, err := h.userService.List(c.Request.Context(), int(offset), int(limit))
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
userResponses := make([]*UserResponse, len(users))
|
|
for i, u := range users {
|
|
userResponses[i] = toUserResponse(u)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"users": userResponses,
|
|
"total": total,
|
|
"offset": offset,
|
|
"limit": limit,
|
|
})
|
|
}
|
|
|
|
func (h *UserHandler) GetUser(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.GetByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, toUserResponse(user))
|
|
}
|
|
|
|
func (h *UserHandler) UpdateUser(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Email *string `json:"email"`
|
|
Nickname *string `json:"nickname"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.GetByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
if req.Email != nil {
|
|
user.Email = req.Email
|
|
}
|
|
if req.Nickname != nil {
|
|
user.Nickname = *req.Nickname
|
|
}
|
|
|
|
if err := h.userService.Update(c.Request.Context(), user); err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, toUserResponse(user))
|
|
}
|
|
|
|
func (h *UserHandler) DeleteUser(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
|
|
return
|
|
}
|
|
|
|
if err := h.userService.Delete(c.Request.Context(), id); err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "user deleted"})
|
|
}
|
|
|
|
func (h *UserHandler) UpdatePassword(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.userService.ChangePassword(c.Request.Context(), id, req.OldPassword, req.NewPassword); err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "密码修改成功"})
|
|
}
|
|
|
|
func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Status string `json:"status" binding:"required"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var status domain.UserStatus
|
|
switch req.Status {
|
|
case "active", "1":
|
|
status = domain.UserStatusActive
|
|
case "inactive", "0":
|
|
status = domain.UserStatusInactive
|
|
case "locked", "2":
|
|
status = domain.UserStatusLocked
|
|
case "disabled", "3":
|
|
status = domain.UserStatusDisabled
|
|
default:
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"})
|
|
return
|
|
}
|
|
|
|
if err := h.userService.UpdateStatus(c.Request.Context(), id, status); err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "status updated"})
|
|
}
|
|
|
|
func (h *UserHandler) GetUserRoles(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"roles": []interface{}{}})
|
|
}
|
|
|
|
func (h *UserHandler) AssignRoles(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "role assignment not implemented"})
|
|
}
|
|
|
|
func (h *UserHandler) UploadAvatar(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "avatar upload not implemented"})
|
|
}
|
|
|
|
func (h *UserHandler) ListAdmins(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"admins": []interface{}{}})
|
|
}
|
|
|
|
func (h *UserHandler) CreateAdmin(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "admin creation not implemented"})
|
|
}
|
|
|
|
func (h *UserHandler) DeleteAdmin(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "admin deletion not implemented"})
|
|
}
|
|
|
|
type UserResponse struct {
|
|
ID int64 `json:"id"`
|
|
Username string `json:"username"`
|
|
Email string `json:"email,omitempty"`
|
|
Nickname string `json:"nickname,omitempty"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func toUserResponse(u *domain.User) *UserResponse {
|
|
email := ""
|
|
if u.Email != nil {
|
|
email = *u.Email
|
|
}
|
|
return &UserResponse{
|
|
ID: u.ID,
|
|
Username: u.Username,
|
|
Email: email,
|
|
Nickname: u.Nickname,
|
|
Status: strconv.FormatInt(int64(u.Status), 10),
|
|
}
|
|
}
|