mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-06 17:46:23 +00:00
fix(oauth): clear bindings when hard deleting users (#5582)
This commit is contained in:
@@ -45,6 +45,7 @@ func TestMain(m *testing.M) {
|
|||||||
&SubscriptionPlan{},
|
&SubscriptionPlan{},
|
||||||
&SubscriptionOrder{},
|
&SubscriptionOrder{},
|
||||||
&UserSubscription{},
|
&UserSubscription{},
|
||||||
|
&UserOAuthBinding{},
|
||||||
&PerfMetric{},
|
&PerfMetric{},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
panic("failed to migrate: " + err.Error())
|
panic("failed to migrate: " + err.Error())
|
||||||
@@ -66,6 +67,7 @@ func truncateTables(t *testing.T) {
|
|||||||
DB.Exec("DELETE FROM subscription_orders")
|
DB.Exec("DELETE FROM subscription_orders")
|
||||||
DB.Exec("DELETE FROM subscription_plans")
|
DB.Exec("DELETE FROM subscription_plans")
|
||||||
DB.Exec("DELETE FROM user_subscriptions")
|
DB.Exec("DELETE FROM user_subscriptions")
|
||||||
|
DB.Exec("DELETE FROM user_oauth_bindings")
|
||||||
DB.Exec("DELETE FROM perf_metrics")
|
DB.Exec("DELETE FROM perf_metrics")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-4
@@ -328,8 +328,12 @@ func HardDeleteUserById(id int) error {
|
|||||||
if id == 0 {
|
if id == 0 {
|
||||||
return errors.New("id 为空!")
|
return errors.New("id 为空!")
|
||||||
}
|
}
|
||||||
err := DB.Unscoped().Delete(&User{}, "id = ?", id).Error
|
return DB.Transaction(func(tx *gorm.DB) error {
|
||||||
return err
|
if err := deleteUserOAuthBindingsByUserId(tx, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Unscoped().Delete(&User{}, "id = ?", id).Error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func inviteUser(inviterId int) (err error) {
|
func inviteUser(inviterId int) (err error) {
|
||||||
@@ -589,8 +593,12 @@ func (user *User) HardDelete() error {
|
|||||||
if user.Id == 0 {
|
if user.Id == 0 {
|
||||||
return errors.New("id 为空!")
|
return errors.New("id 为空!")
|
||||||
}
|
}
|
||||||
err := DB.Unscoped().Delete(user).Error
|
return DB.Transaction(func(tx *gorm.DB) error {
|
||||||
return err
|
if err := deleteUserOAuthBindingsByUserId(tx, user.Id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Unscoped().Delete(user).Error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateAndFill check password & user status
|
// ValidateAndFill check password & user status
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ import (
|
|||||||
// UserOAuthBinding stores the binding relationship between users and custom OAuth providers
|
// UserOAuthBinding stores the binding relationship between users and custom OAuth providers
|
||||||
type UserOAuthBinding struct {
|
type UserOAuthBinding struct {
|
||||||
Id int `json:"id" gorm:"primaryKey"`
|
Id int `json:"id" gorm:"primaryKey"`
|
||||||
UserId int `json:"user_id" gorm:"not null;uniqueIndex:ux_user_provider"` // User ID - one binding per user per provider
|
UserId int `json:"user_id" gorm:"not null;uniqueIndex:ux_user_provider"` // User ID - one binding per user per provider
|
||||||
ProviderId int `json:"provider_id" gorm:"not null;uniqueIndex:ux_user_provider;uniqueIndex:ux_provider_userid"` // Custom OAuth provider ID
|
ProviderId int `json:"provider_id" gorm:"not null;uniqueIndex:ux_user_provider;uniqueIndex:ux_provider_userid"` // Custom OAuth provider ID
|
||||||
ProviderUserId string `json:"provider_user_id" gorm:"type:varchar(256);not null;uniqueIndex:ux_provider_userid"` // User ID from OAuth provider - one OAuth account per provider
|
ProviderUserId string `json:"provider_user_id" gorm:"type:varchar(256);not null;uniqueIndex:ux_provider_userid"` // User ID from OAuth provider - one OAuth account per provider
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,9 +134,8 @@ func DeleteUserOAuthBinding(userId, providerId int) error {
|
|||||||
return DB.Where("user_id = ? AND provider_id = ?", userId, providerId).Delete(&UserOAuthBinding{}).Error
|
return DB.Where("user_id = ? AND provider_id = ?", userId, providerId).Delete(&UserOAuthBinding{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteUserOAuthBindingsByUserId deletes all OAuth bindings for a user
|
func deleteUserOAuthBindingsByUserId(tx *gorm.DB, userId int) error {
|
||||||
func DeleteUserOAuthBindingsByUserId(userId int) error {
|
return tx.Where("user_id = ?", userId).Delete(&UserOAuthBinding{}).Error
|
||||||
return DB.Where("user_id = ?", userId).Delete(&UserOAuthBinding{}).Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBindingCountByProviderId returns the number of bindings for a provider
|
// GetBindingCountByProviderId returns the number of bindings for a provider
|
||||||
|
|||||||
Reference in New Issue
Block a user