feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
"github.com/user-management-system/internal/api/middleware"
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
"github.com/user-management-system/internal/domain"
|
|
|
|
|
|
"github.com/user-management-system/internal/service"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// DeviceHandler handles device management requests
|
|
|
|
|
|
type DeviceHandler struct {
|
|
|
|
|
|
deviceService *service.DeviceService
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewDeviceHandler creates a new DeviceHandler
|
|
|
|
|
|
func NewDeviceHandler(deviceService *service.DeviceService) *DeviceHandler {
|
|
|
|
|
|
return &DeviceHandler{deviceService: deviceService}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
func (h *DeviceHandler) currentActor(c *gin.Context) (int64, bool, bool) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
|
|
|
|
|
return 0, false, false
|
|
|
|
|
|
}
|
|
|
|
|
|
return userID, middleware.IsAdmin(c), true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// CreateDevice 创建设备
|
|
|
|
|
|
// @Summary 创建设备记录
|
|
|
|
|
|
// @Description 当前用户创建设备记录
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param request body service.CreateDeviceRequest true "设备信息"
|
|
|
|
|
|
// @Success 201 {object} Response{data=domain.Device} "设备创建成功"
|
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
|
// @Router /api/v1/devices [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) CreateDevice(c *gin.Context) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req service.CreateDeviceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
device, err := h.deviceService.CreateDevice(c.Request.Context(), userID, &req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": device,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetMyDevices 获取我的设备列表
|
|
|
|
|
|
// @Summary 获取当前用户的设备列表
|
|
|
|
|
|
// @Description 获取当前用户的所有设备记录
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param page query int false "页码"
|
|
|
|
|
|
// @Param page_size query int false "每页数量"
|
|
|
|
|
|
// @Success 200 {object} Response{data=DeviceListResponse} "设备列表"
|
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
|
// @Router /api/v1/devices [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) GetMyDevices(c *gin.Context) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
|
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
2026-04-18 15:33:12 +08:00
|
|
|
|
if pageSize < 1 || pageSize > 100 {
|
|
|
|
|
|
pageSize = 20
|
|
|
|
|
|
}
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
|
|
|
|
|
|
devices, total, err := h.deviceService.GetUserDevices(c.Request.Context(), userID, page, pageSize)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2026-04-11 13:34:56 +08:00
|
|
|
|
"code": 0,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": gin.H{
|
2026-04-11 13:34:56 +08:00
|
|
|
|
"items": devices,
|
test: add comprehensive test coverage and improve code quality
- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
2026-04-17 20:43:50 +08:00
|
|
|
|
"total": total,
|
|
|
|
|
|
"page": page,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"page_size": pageSize,
|
|
|
|
|
|
},
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetDevice 获取设备详情
|
|
|
|
|
|
// @Summary 获取设备详情
|
|
|
|
|
|
// @Description 根据ID获取设备详细信息
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Success 200 {object} Response{data=domain.Device} "设备信息"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id} [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) GetDevice(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
device, err := h.deviceService.GetDeviceForActor(c.Request.Context(), actorUserID, id, isAdmin)
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": device,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// UpdateDevice 更新设备
|
|
|
|
|
|
// @Summary 更新设备信息
|
|
|
|
|
|
// @Description 更新设备的基本信息
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Param request body service.UpdateDeviceRequest true "更新信息"
|
|
|
|
|
|
// @Success 200 {object} Response{data=domain.Device} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id} [put]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) UpdateDevice(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req service.UpdateDeviceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
device, err := h.deviceService.UpdateDeviceForActor(c.Request.Context(), actorUserID, id, isAdmin, &req)
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": device,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// DeleteDevice 删除设备
|
|
|
|
|
|
// @Summary 删除设备
|
|
|
|
|
|
// @Description 删除设备记录
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Success 200 {object} Response "删除成功"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id} [delete]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) DeleteDevice(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.DeleteDeviceForActor(c.Request.Context(), actorUserID, id, isAdmin); err != nil {
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "device deleted",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// UpdateDeviceStatus 更新设备状态
|
|
|
|
|
|
// @Summary 更新设备状态
|
|
|
|
|
|
// @Description 更新设备状态(active/inactive)
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Param request body UpdateDeviceStatusRequest true "状态信息"
|
|
|
|
|
|
// @Success 200 {object} Response "状态更新成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "无效的状态值"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id}/status [put]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
Status string `json:"status" binding:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var status domain.DeviceStatus
|
|
|
|
|
|
switch req.Status {
|
|
|
|
|
|
case "active", "1":
|
|
|
|
|
|
status = domain.DeviceStatusActive
|
|
|
|
|
|
case "inactive", "0":
|
|
|
|
|
|
status = domain.DeviceStatusInactive
|
|
|
|
|
|
default:
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.UpdateDeviceStatusForActor(c.Request.Context(), actorUserID, id, isAdmin, status); err != nil {
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "status updated",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetUserDevices 获取指定用户的设备列表
|
|
|
|
|
|
// @Summary 获取用户设备列表
|
|
|
|
|
|
// @Description 获取指定用户的设备列表(仅本人或管理员)
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "用户ID"
|
|
|
|
|
|
// @Param page query int false "页码"
|
|
|
|
|
|
// @Param page_size query int false "每页数量"
|
|
|
|
|
|
// @Success 200 {object} Response{data=DeviceListResponse} "设备列表"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Router /api/v1/users/{id}/devices [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) GetUserDevices(c *gin.Context) {
|
2026-04-07 12:08:16 +08:00
|
|
|
|
// IDOR 修复:检查当前用户是否有权限查看指定用户的设备
|
|
|
|
|
|
currentUserID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
2026-04-07 12:08:16 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否为管理员
|
2026-05-08 08:05:26 +08:00
|
|
|
|
isAdmin := middleware.IsAdmin(c)
|
2026-04-07 12:08:16 +08:00
|
|
|
|
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
userIDParam := c.Param("id")
|
|
|
|
|
|
userID, err := strconv.ParseInt(userIDParam, 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-07 12:08:16 +08:00
|
|
|
|
// 非管理员只能查看自己的设备
|
|
|
|
|
|
if !isAdmin && userID != currentUserID {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusForbidden, gin.H{"code": 403, "message": "无权访问该用户的设备列表"})
|
2026-04-07 12:08:16 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
|
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
2026-04-18 15:33:12 +08:00
|
|
|
|
if pageSize < 1 || pageSize > 100 {
|
|
|
|
|
|
pageSize = 20
|
|
|
|
|
|
}
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
|
|
|
|
|
|
devices, total, err := h.deviceService.GetUserDevices(c.Request.Context(), userID, page, pageSize)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2026-04-11 13:34:56 +08:00
|
|
|
|
"code": 0,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": gin.H{
|
|
|
|
|
|
"items": devices,
|
test: add comprehensive test coverage and improve code quality
- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
2026-04-17 20:43:50 +08:00
|
|
|
|
"total": total,
|
|
|
|
|
|
"page": page,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"page_size": pageSize,
|
|
|
|
|
|
},
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetAllDevices 获取所有设备列表
|
|
|
|
|
|
// @Summary 获取所有设备列表
|
|
|
|
|
|
// @Description 获取所有设备列表(仅管理员),支持游标分页和偏移分页
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param cursor query string false "游标分页游标"
|
|
|
|
|
|
// @Param size query int false "每页数量(游标模式)"
|
|
|
|
|
|
// @Param page query int false "页码"
|
|
|
|
|
|
// @Param page_size query int false "每页数量"
|
|
|
|
|
|
// @Success 200 {object} Response{data=DeviceListResponse} "设备列表"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Router /api/v1/admin/devices [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) GetAllDevices(c *gin.Context) {
|
|
|
|
|
|
var req service.GetAllDevicesRequest
|
|
|
|
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-07 12:08:16 +08:00
|
|
|
|
// Use cursor-based pagination when cursor is provided
|
|
|
|
|
|
if req.Cursor != "" || req.Size > 0 {
|
|
|
|
|
|
result, err := h.deviceService.GetAllDevicesCursor(c.Request.Context(), &req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": result,
|
|
|
|
|
|
})
|
2026-04-07 12:08:16 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback to legacy offset-based pagination
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
devices, total, err := h.deviceService.GetAllDevices(c.Request.Context(), &req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2026-04-11 13:34:56 +08:00
|
|
|
|
"code": 0,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": gin.H{
|
2026-04-11 13:34:56 +08:00
|
|
|
|
"items": devices,
|
test: add comprehensive test coverage and improve code quality
- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
2026-04-17 20:43:50 +08:00
|
|
|
|
"total": total,
|
|
|
|
|
|
"page": req.Page,
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"page_size": req.PageSize,
|
|
|
|
|
|
},
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TrustDeviceRequest 信任设备请求
|
|
|
|
|
|
type TrustDeviceRequest struct {
|
|
|
|
|
|
TrustDuration string `json:"trust_duration"` // 信任持续时间,如 "30d" 表示30天
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TrustDevice 设置设备为信任设备
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// @Summary 设置设备为信任设备
|
|
|
|
|
|
// @Description 将指定设备设置为信任设备,在信任期内免二次验证
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Param request body TrustDeviceRequest true "信任配置"
|
|
|
|
|
|
// @Success 200 {object} Response "设置成功"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id}/trust [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) TrustDevice(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req TrustDeviceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析信任持续时间
|
|
|
|
|
|
trustDuration := parseDuration(req.TrustDuration)
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.TrustDeviceForActor(c.Request.Context(), actorUserID, id, isAdmin, trustDuration); err != nil {
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "device trusted",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// TrustDeviceByDeviceID 根据设备标识设置设备为信任状态
|
|
|
|
|
|
// @Summary 根据设备标识设置信任
|
|
|
|
|
|
// @Description 根据设备唯一标识字符串设置设备为信任状态
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param deviceId path string true "设备唯一标识"
|
|
|
|
|
|
// @Param request body TrustDeviceRequest true "信任配置"
|
|
|
|
|
|
// @Success 200 {object} Response "设置成功"
|
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
|
// @Router /api/v1/devices/trust/{deviceId} [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) TrustDeviceByDeviceID(c *gin.Context) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deviceID := c.Param("deviceId")
|
|
|
|
|
|
if deviceID == "" {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req TrustDeviceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析信任持续时间
|
|
|
|
|
|
trustDuration := parseDuration(req.TrustDuration)
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.TrustDeviceByDeviceID(c.Request.Context(), userID, deviceID, trustDuration); err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "device trusted",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UntrustDevice 取消设备信任状态
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// @Summary 取消设备信任
|
|
|
|
|
|
// @Description 取消设备的信任状态
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "设备ID"
|
|
|
|
|
|
// @Success 200 {object} Response "取消成功"
|
|
|
|
|
|
// @Failure 404 {object} Response "设备不存在"
|
|
|
|
|
|
// @Router /api/v1/devices/{id}/trust [delete]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) UntrustDevice(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 08:05:26 +08:00
|
|
|
|
actorUserID, isAdmin, ok := h.currentActor(c)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.UntrustDeviceForActor(c.Request.Context(), actorUserID, id, isAdmin); err != nil {
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "device untrusted",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetMyTrustedDevices 获取我的信任设备列表
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// @Summary 获取信任设备列表
|
|
|
|
|
|
// @Description 获取当前用户的信任设备列表
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Success 200 {object} Response{data=[]domain.Device} "信任设备列表"
|
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
|
// @Router /api/v1/devices/trusted [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) GetMyTrustedDevices(c *gin.Context) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
devices, err := h.deviceService.GetTrustedDevices(c.Request.Context(), userID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": devices,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LogoutAllOtherDevices 登出所有其他设备
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// @Summary 登出其他设备
|
|
|
|
|
|
// @Description 登出当前用户除指定设备外的所有其他设备
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param X-Device-ID header string true "当前设备ID"
|
|
|
|
|
|
// @Success 200 {object} Response "登出成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "无效的设备ID"
|
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
|
// @Router /api/v1/devices/logout-others [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *DeviceHandler) LogoutAllOtherDevices(c *gin.Context) {
|
|
|
|
|
|
userID, ok := getUserIDFromContext(c)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从请求中获取当前设备ID
|
|
|
|
|
|
currentDeviceIDStr := c.GetHeader("X-Device-ID")
|
|
|
|
|
|
currentDeviceID, err := strconv.ParseInt(currentDeviceIDStr, 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:34:56 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid current device id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.deviceService.LogoutAllOtherDevices(c.Request.Context(), userID, currentDeviceID); err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "all other devices logged out",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parseDuration 解析duration字符串,如 "30d" -> 30天的time.Duration
|
|
|
|
|
|
func parseDuration(s string) time.Duration {
|
|
|
|
|
|
if s == "" {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
// 简单实现,支持 d(天)和h(小时)
|
|
|
|
|
|
var d int
|
|
|
|
|
|
var h int
|
|
|
|
|
|
_, _ = d, h
|
|
|
|
|
|
switch s[len(s)-1] {
|
|
|
|
|
|
case 'd':
|
|
|
|
|
|
d = 1
|
|
|
|
|
|
_, _ = fmt.Sscanf(s[:len(s)-1], "%d", &d)
|
|
|
|
|
|
return time.Duration(d) * 24 * time.Hour
|
|
|
|
|
|
case 'h':
|
|
|
|
|
|
_, _ = fmt.Sscanf(s[:len(s)-1], "%d", &h)
|
|
|
|
|
|
return time.Duration(h) * time.Hour
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|