From e9d8c95b445b45c8dd49f15547ad3f9fa430e4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E8=BF=9B?= Date: Sun, 25 Oct 2026 17:11:07 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=BE=E5=A4=87=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=8D=E5=86=8D=E6=8B=A6=E6=88=AA=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E4=B8=8E=E4=BC=81=E4=B8=9A=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 设备类型选项接口用 router.Group("/devices", 拦截函数) 限制仅平台访问,Fiber 会把该函数注册为整个 /devices 前缀的中间件,导致代理、企业账号的设备列表、设备下的卡、分配回收等接口全部返回 403。改为在 ListDeviceTypes 内部判断用户类型,其他设备接口恢复原有权限。 --- internal/handler/admin/device.go | 4 ++++ internal/routes/device.go | 13 ++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/internal/handler/admin/device.go b/internal/handler/admin/device.go index 3575cb77..2a3abb0e 100644 --- a/internal/handler/admin/device.go +++ b/internal/handler/admin/device.go @@ -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 diff --git a/internal/routes/device.go b/internal/routes/device.go index c9f3c5fa..2cf01e96 100644 --- a/internal/routes/device.go +++ b/internal/routes/device.go @@ -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{"设备管理"},