Merge commit from fork

This commit is contained in:
RedwindA
2026-08-06 17:53:55 +08:00
committed by GitHub
parent ea4f021012
commit 0cd9dc85e3
4 changed files with 117 additions and 30 deletions
+39 -10
View File
@@ -139,6 +139,22 @@ func (user *User) SetAccessToken(token string) {
user.AccessToken = &token
}
// UpdateUserAccessToken rotates a dashboard personal access token without
// writing a stale user snapshot back over concurrently updated fields.
func UpdateUserAccessToken(id int, token string) error {
if id == 0 {
return errors.New("id 为空!")
}
result := DB.Model(&User{}).Where("id = ?", id).Update("access_token", token)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
func (user *User) GetSetting() dto.UserSetting {
setting := dto.UserSetting{}
if user.Setting != "" {
@@ -489,15 +505,19 @@ func HardDeleteUserById(id int) error {
return user.HardDelete()
}
func inviteUser(inviterId int) (err error) {
user, err := GetUserById(inviterId, true)
if err != nil {
return err
func inviteUser(inviterId int) error {
result := DB.Model(&User{}).Where("id = ?", inviterId).Updates(map[string]interface{}{
"aff_count": gorm.Expr("aff_count + ?", 1),
"aff_quota": gorm.Expr("aff_quota + ?", common.QuotaForInviter),
"aff_history": gorm.Expr("aff_history + ?", common.QuotaForInviter),
})
if result.Error != nil {
return result.Error
}
user.AffCount++
user.AffQuota += common.QuotaForInviter
user.AffHistoryQuota += common.QuotaForInviter
return DB.Save(user).Error
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
func (user *User) TransferAffQuotaToQuota(quota int) error {
@@ -514,7 +534,7 @@ func (user *User) TransferAffQuotaToQuota(quota int) error {
defer tx.Rollback() // 确保在函数退出时事务能回滚
// 加锁查询用户以确保数据一致性
err := lockForUpdate(tx).First(&user, user.Id).Error
err := lockForUpdate(tx).First(user, user.Id).Error
if err != nil {
return err
}
@@ -748,7 +768,16 @@ func (user *User) UpdateWithTx(tx *gorm.DB, updatePassword bool) error {
return err
}
}
if err = tx.Model(&current).Omit("quota", "used_quota", "request_count", "auth_version").Updates(newUser).Error; err != nil {
if err = tx.Model(&current).Omit(
"access_token",
"quota",
"used_quota",
"request_count",
"aff_count",
"aff_quota",
"aff_history",
"auth_version",
).Updates(newUser).Error; err != nil {
return err
}
return tx.First(user, user.Id).Error