fix: 设备管理接口不再拦截代理与企业账号
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled

设备类型选项接口用 router.Group("/devices", 拦截函数) 限制仅平台访问,Fiber 会把该函数注册为整个 /devices 前缀的中间件,导致代理、企业账号的设备列表、设备下的卡、分配回收等接口全部返回 403。改为在 ListDeviceTypes 内部判断用户类型,其他设备接口恢复原有权限。
This commit is contained in:
邹进
2026-10-25 17:11:07 +09:00
parent a855742752
commit 4de67f6dc3
2 changed files with 6 additions and 11 deletions

View File

@@ -38,6 +38,10 @@ func (h *DeviceHandler) List(c *fiber.Ctx) error {
// ListDeviceTypes 查询设备类型选项。
// GET /api/admin/devices/types
func (h *DeviceHandler) ListDeviceTypes(c *fiber.Ctx) error {
userType := middleware.GetUserTypeFromContext(c.UserContext())
if userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
return errors.New(errors.CodeForbidden, constants.PlatformManagementForbiddenMessage)
}
result, err := h.service.ListDeviceTypes(c.UserContext())
if err != nil {
return err

View File

@@ -6,24 +6,15 @@ import (
"github.com/break/junhong_cmp_fiber/internal/gateway"
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
"github.com/break/junhong_cmp_fiber/pkg/openapi"
)
func registerDeviceRoutes(router fiber.Router, handler *admin.DeviceHandler, importHandler *admin.DeviceImportHandler, doc *openapi.Generator, basePath string) {
devices := router.Group("/devices")
groupPath := basePath + "/devices"
platformDevices := router.Group("/devices", func(c *fiber.Ctx) error {
userType := middleware.GetUserTypeFromContext(c.UserContext())
if userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
return errors.New(errors.CodeForbidden, constants.PlatformManagementForbiddenMessage)
}
return c.Next()
})
Register(platformDevices, doc, groupPath, "GET", "/types", handler.ListDeviceTypes, RouteSpec{
// 仅平台可用的判断放在 ListDeviceTypes 内部:Fiber 的 Group 带处理函数会注册为整个 /devices 前缀的中间件,会拦住代理与企业账号的所有设备接口。
Register(devices, doc, groupPath, "GET", "/types", handler.ListDeviceTypes, RouteSpec{
Summary: "查询设备类型选项",
Description: "仅超级管理员与平台账号可访问。返回未删除设备中去重、去除首尾空白且按升序排列的非空设备类型字符串。",
Tags: []string{"设备管理"},