From f599c6594405e0cf7dfc83f34754bca29f300740 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Mon, 14 Oct 2024 16:15:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E5=88=86=E7=BB=84=E4=B8=8D=E8=83=BD=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E7=94=A8=E6=88=B7=E5=88=86=E7=BB=84=20(close=20#528)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/user_groups.go | 18 ++++++++++++++++++ controller/group.go | 9 +++++++-- middleware/distributor.go | 2 +- router/api-router.go | 1 + web/src/components/LogsTable.js | 20 ++++++++++---------- web/src/pages/Token/EditToken.js | 2 +- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/common/user_groups.go b/common/user_groups.go index e705394..41b53b3 100644 --- a/common/user_groups.go +++ b/common/user_groups.go @@ -22,6 +22,24 @@ func UpdateUserUsableGroupsByJSONString(jsonStr string) error { return json.Unmarshal([]byte(jsonStr), &UserUsableGroups) } +func GetUserUsableGroups(userGroup string) map[string]string { + if userGroup == "" { + // 如果userGroup为空,返回UserUsableGroups + return UserUsableGroups + } + // 如果userGroup不在UserUsableGroups中,返回UserUsableGroups + userGroup + if _, ok := UserUsableGroups[userGroup]; !ok { + appendUserUsableGroups := make(map[string]string) + for k, v := range UserUsableGroups { + appendUserUsableGroups[k] = v + } + appendUserUsableGroups[userGroup] = "用户分组" + return appendUserUsableGroups + } + // 如果userGroup在UserUsableGroups中,返回UserUsableGroups + return UserUsableGroups +} + func GroupInUserUsableGroups(groupName string) bool { _, ok := UserUsableGroups[groupName] return ok diff --git a/controller/group.go b/controller/group.go index 2ee008b..9af07af 100644 --- a/controller/group.go +++ b/controller/group.go @@ -4,6 +4,7 @@ import ( "github.com/gin-gonic/gin" "net/http" "one-api/common" + "one-api/model" ) func GetGroups(c *gin.Context) { @@ -20,10 +21,14 @@ func GetGroups(c *gin.Context) { func GetUserGroups(c *gin.Context) { usableGroups := make(map[string]string) + userGroup := "" + userId := c.GetInt("id") + userGroup, _ = model.CacheGetUserGroup(userId) for groupName, _ := range common.GroupRatio { // UserUsableGroups contains the groups that the user can use - if _, ok := common.UserUsableGroups[groupName]; ok { - usableGroups[groupName] = common.UserUsableGroups[groupName] + userUsableGroups := common.GetUserUsableGroups(userGroup) + if _, ok := userUsableGroups[groupName]; ok { + usableGroups[groupName] = userUsableGroups[groupName] } } c.JSON(http.StatusOK, gin.H{ diff --git a/middleware/distributor.go b/middleware/distributor.go index 7a95964..f1f64ca 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -42,7 +42,7 @@ func Distribute() func(c *gin.Context) { tokenGroup := c.GetString("token_group") if tokenGroup != "" { // check common.UserUsableGroups[userGroup] - if _, ok := common.UserUsableGroups[tokenGroup]; !ok { + if _, ok := common.GetUserUsableGroups(userGroup)[tokenGroup]; !ok { abortWithOpenAiMessage(c, http.StatusForbidden, fmt.Sprintf("令牌分组 %s 已被禁用", tokenGroup)) return } diff --git a/router/api-router.go b/router/api-router.go index c38a314..798713b 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -44,6 +44,7 @@ func SetApiRouter(router *gin.Engine) { selfRoute := userRoute.Group("/") selfRoute.Use(middleware.UserAuth()) { + selfRoute.GET("/self/groups", controller.GetUserGroups) selfRoute.GET("/self", controller.GetSelf) selfRoute.GET("/models", controller.GetUserModels) selfRoute.PUT("/self", controller.UpdateSelf) diff --git a/web/src/components/LogsTable.js b/web/src/components/LogsTable.js index 072f2ad..77d18b9 100644 --- a/web/src/components/LogsTable.js +++ b/web/src/components/LogsTable.js @@ -250,7 +250,7 @@ const LogsTable = () => { title: '类型', dataIndex: 'type', render: (text, record, index) => { - return