refactor: improve user group handling and add GetUserUsableGroups function

- Introduced a new function `GetUserUsableGroupsCopy` to return a copy of user usable groups.
- Updated `GetUserUsableGroups` to utilize the new function for better encapsulation.
- Changed variable names from `UserUsableGroups` to `userUsableGroups` for consistency.
- Enhanced `GetUserUsableGroups` logic to ensure it returns a copy of the groups, preventing unintended modifications.
This commit is contained in:
CalciumIon
2024-12-27 21:19:22 +08:00
parent 5f082d72bb
commit 77861e6440
2 changed files with 24 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"one-api/common" "one-api/common"
"one-api/model" "one-api/model"
"one-api/setting"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@@ -454,7 +455,11 @@ func GetUserModels(c *gin.Context) {
}) })
return return
} }
models := model.GetGroupModels(user.Group) groups := setting.GetUserUsableGroups(user.Group)
var models []string
for group := range groups {
models = append(models, model.GetGroupModels(group)...)
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "", "message": "",

View File

@@ -5,13 +5,21 @@ import (
"one-api/common" "one-api/common"
) )
var UserUsableGroups = map[string]string{ var userUsableGroups = map[string]string{
"default": "默认分组", "default": "默认分组",
"vip": "vip分组", "vip": "vip分组",
} }
func GetUserUsableGroupsCopy() map[string]string {
copyUserUsableGroups := make(map[string]string)
for k, v := range userUsableGroups {
copyUserUsableGroups[k] = v
}
return copyUserUsableGroups
}
func UserUsableGroups2JSONString() string { func UserUsableGroups2JSONString() string {
jsonBytes, err := json.Marshal(UserUsableGroups) jsonBytes, err := json.Marshal(userUsableGroups)
if err != nil { if err != nil {
common.SysError("error marshalling user groups: " + err.Error()) common.SysError("error marshalling user groups: " + err.Error())
} }
@@ -19,29 +27,25 @@ func UserUsableGroups2JSONString() string {
} }
func UpdateUserUsableGroupsByJSONString(jsonStr string) error { func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
UserUsableGroups = make(map[string]string) userUsableGroups = make(map[string]string)
return json.Unmarshal([]byte(jsonStr), &UserUsableGroups) return json.Unmarshal([]byte(jsonStr), &userUsableGroups)
} }
func GetUserUsableGroups(userGroup string) map[string]string { func GetUserUsableGroups(userGroup string) map[string]string {
groupsCopy := GetUserUsableGroupsCopy()
if userGroup == "" { if userGroup == "" {
// 如果userGroup为空返回UserUsableGroups // 如果userGroup为空返回UserUsableGroups
return UserUsableGroups return groupsCopy
} }
// 如果userGroup不在UserUsableGroups中返回UserUsableGroups + userGroup // 如果userGroup不在UserUsableGroups中返回UserUsableGroups + userGroup
if _, ok := UserUsableGroups[userGroup]; !ok { if _, ok := groupsCopy[userGroup]; !ok {
appendUserUsableGroups := make(map[string]string) groupsCopy[userGroup] = "用户分组"
for k, v := range UserUsableGroups {
appendUserUsableGroups[k] = v
}
appendUserUsableGroups[userGroup] = "用户分组"
return appendUserUsableGroups
} }
// 如果userGroup在UserUsableGroups中返回UserUsableGroups // 如果userGroup在UserUsableGroups中返回UserUsableGroups
return UserUsableGroups return groupsCopy
} }
func GroupInUserUsableGroups(groupName string) bool { func GroupInUserUsableGroups(groupName string) bool {
_, ok := UserUsableGroups[groupName] _, ok := userUsableGroups[groupName]
return ok return ok
} }