fix(oauth): avoid overwriting user state when binding

This commit is contained in:
CaIon
2026-08-11 22:03:45 +08:00
parent 3d5dc36f1d
commit d7992672a6
11 changed files with 125 additions and 22 deletions
+1
View File
@@ -46,6 +46,7 @@ func (*authFlowTestOAuthProvider) IsUserIDTaken(string) bool
func (*authFlowTestOAuthProvider) FillUserByProviderID(*model.User, string) error { return nil }
func (*authFlowTestOAuthProvider) SetProviderUserID(*model.User, string) {}
func (*authFlowTestOAuthProvider) GetProviderPrefix() string { return "flow_" }
func (*authFlowTestOAuthProvider) ProviderUserIDColumn() string { return "" }
func setupAuthFlowControllerTest(t *testing.T) *authFlowTestOAuthProvider {
t.Helper()
+5 -10
View File
@@ -263,25 +263,20 @@ func handleOAuthBind(c *gin.Context, provider oauth.Provider, pendingFlow *model
return
}
user := model.User{Id: pendingFlow.UserId}
err = user.FillUserById()
if err != nil {
common.ApiError(c, err)
return
}
userId := pendingFlow.UserId
// Handle binding based on provider type
if genericProvider, ok := provider.(*oauth.GenericOAuthProvider); ok {
// Custom provider: use user_oauth_bindings table
err = model.UpdateUserOAuthBinding(user.Id, genericProvider.GetProviderId(), oauthUser.ProviderUserID)
err = model.UpdateUserOAuthBinding(userId, genericProvider.GetProviderId(), oauthUser.ProviderUserID)
if err != nil {
common.ApiError(c, err)
return
}
} else {
// Built-in provider: update user record directly
provider.SetProviderUserID(&user, oauthUser.ProviderUserID)
err = user.Update(false)
// Built-in provider: 只更新绑定列。完整快照的 user.Update 会把读取时刻的
// role/status/group 一并写回,覆盖并发发生的封禁、降权或分组变更。
err = model.UpdateUserBindColumn(userId, provider.ProviderUserIDColumn(), oauthUser.ProviderUserID)
if err != nil {
common.ApiError(c, err)
return
+4 -12
View File
@@ -156,21 +156,13 @@ func WeChatBind(c *gin.Context) {
})
return
}
user := model.User{
Id: c.GetInt("id"),
}
if user.Id == 0 {
userId := c.GetInt("id")
if userId == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"success": false, "message": "未登录"})
return
}
err = user.FillUserById()
if err != nil {
common.ApiError(c, err)
return
}
user.WeChatId = wechatId
err = user.Update(false)
if err != nil {
// 只更新绑定列,避免完整用户快照覆盖并发的封禁、降权或分组变更。
if err := model.UpdateUserBindColumn(userId, "wechat_id", wechatId); err != nil {
common.ApiError(c, err)
return
}