🐛 fix: admin custom channel id failed (#56)
This commit is contained in:
parent
99929f15d5
commit
8646ee77ca
@ -13,7 +13,6 @@ import (
|
|||||||
providersBase "one-api/providers/base"
|
providersBase "one-api/providers/base"
|
||||||
"one-api/types"
|
"one-api/types"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
@ -55,9 +54,9 @@ func GetValidFieldName(err error, obj interface{}) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fetchChannel(c *gin.Context, modelName string) (channel *model.Channel, fail bool) {
|
func fetchChannel(c *gin.Context, modelName string) (channel *model.Channel, fail bool) {
|
||||||
channelId, ok := c.Get("channelId")
|
channelId := c.GetInt("channelId")
|
||||||
if ok {
|
if channelId > 0 {
|
||||||
channel, fail = fetchChannelById(c, channelId.(int))
|
channel, fail = fetchChannelById(c, channelId)
|
||||||
if fail {
|
if fail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -73,13 +72,8 @@ func fetchChannel(c *gin.Context, modelName string) (channel *model.Channel, fai
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchChannelById(c *gin.Context, channelId any) (*model.Channel, bool) {
|
func fetchChannelById(c *gin.Context, channelId int) (*model.Channel, bool) {
|
||||||
id, err := strconv.Atoi(channelId.(string))
|
channel, err := model.GetChannelById(channelId, true)
|
||||||
if err != nil {
|
|
||||||
common.AbortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
|
||||||
return nil, true
|
|
||||||
}
|
|
||||||
channel, err := model.GetChannelById(id, true)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.AbortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
common.AbortWithMessage(c, http.StatusBadRequest, "无效的渠道 Id")
|
||||||
return nil, true
|
return nil, true
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-contrib/sessions"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"one-api/model"
|
"one-api/model"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func authHelper(c *gin.Context, minRole int) {
|
func authHelper(c *gin.Context, minRole int) {
|
||||||
@ -108,7 +109,12 @@ func TokenAuth() func(c *gin.Context) {
|
|||||||
c.Set("token_name", token.Name)
|
c.Set("token_name", token.Name)
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
if model.IsAdmin(token.UserId) {
|
if model.IsAdmin(token.UserId) {
|
||||||
c.Set("channelId", parts[1])
|
channelId := common.String2Int(parts[1])
|
||||||
|
if channelId == 0 {
|
||||||
|
abortWithMessage(c, http.StatusForbidden, "无效的渠道 Id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Set("channelId", channelId)
|
||||||
} else {
|
} else {
|
||||||
abortWithMessage(c, http.StatusForbidden, "普通用户不支持指定渠道")
|
abortWithMessage(c, http.StatusForbidden, "普通用户不支持指定渠道")
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user