Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions controller/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,26 @@ func GitHubBind(c *gin.Context) {
})
return
}
session := sessions.Default(c)
id := session.Get("id")
// id := c.GetInt("id") // critical bug!
user.Id = id.(int)
func GitHubBind(c *gin.Context) {
session := sessions.Default(c)
idInterface := session.Get("id")
if idInterface == nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "用户未登录",
})
return
}

id, ok := idInterface.(int)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "用户ID类型错误",
})
return
}
user.Id = id
err = user.FillUserById()
if err != nil {
c.JSON(http.StatusOK, gin.H{
Expand Down
67 changes: 46 additions & 21 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,27 +538,52 @@ func UpdateSelf(c *gin.Context) {
})
return
}
if user.Password == "" {
user.Password = "$I_LOVE_U" // make Validator happy :)
}
if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
})
return
}

cleanUser := model.User{
Id: c.GetInt("id"),
Username: user.Username,
Password: user.Password,
DisplayName: user.DisplayName,
}
if user.Password == "$I_LOVE_U" {
user.Password = "" // rollback to what it should be
cleanUser.Password = ""
}
func UpdateSelf(c *gin.Context) {
var user model.User
err := json.NewDecoder(c.Request.Body).Decode(&user)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的请求数据",
})
return
}

// 移除魔法字符串,使用更安全的验证方式
passwordEmpty := user.Password == ""

if err := common.Validate.Struct(&user); err != nil {
// 如果密码为空且验证失败,检查是否只是密码字段的问题
if passwordEmpty {
// 创建临时用户对象进行验证,排除密码字段
tempUser := user
tempUser.Password = "temp_password_for_validation"
if tempErr := common.Validate.Struct(&tempUser); tempErr != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
})
return
}
}

cleanUser := model.User{
Id: c.GetInt("id"),
Username: user.Username,
DisplayName: user.DisplayName,
}

// 只有当密码不为空时才设置密码
if !passwordEmpty {
cleanUser.Password = user.Password
}
updatePassword := user.Password != ""
if err := cleanUser.Update(updatePassword); err != nil {
c.JSON(http.StatusOK, gin.H{
Expand Down
41 changes: 9 additions & 32 deletions service/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,15 @@ func OpenAIErrorWrapperLocal(err error, code string, statusCode int) *dto.OpenAI
return openaiErr
}

func RelayErrorHandler(resp *http.Response) (errWithStatusCode *dto.OpenAIErrorWithStatusCode) {
errWithStatusCode = &dto.OpenAIErrorWithStatusCode{
StatusCode: resp.StatusCode,
Error: dto.OpenAIError{
Type: "upstream_error",
Code: "bad_response_status_code",
Param: strconv.Itoa(resp.StatusCode),
},
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var errResponse dto.GeneralErrorResponse
err = json.Unmarshal(responseBody, &errResponse)
if err != nil {
return
}
if errResponse.Error.Message != "" {
// OpenAI format error, so we override the default one
errWithStatusCode.Error = errResponse.Error
} else {
errWithStatusCode.Error.Message = errResponse.ToMessage()
}
if errWithStatusCode.Error.Message == "" {
errWithStatusCode.Error.Message = fmt.Sprintf("bad response status code %d", resp.StatusCode)
}
return
func cacheSetUserName(userId int, username string) {
if !common.RedisEnabled {
return
}
key := fmt.Sprintf("user_name:%d", userId)
err := common.RedisSet(key, username, time.Duration(UserId2UsernameCacheSeconds)*time.Second)
if err != nil {
common.SysError("Redis set user name error: " + err.Error()) // 修正错误信息
}
}

func ResetStatusCode(openaiErr *dto.OpenAIErrorWithStatusCode, statusCodeMappingStr string) {
Expand Down