mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-07 01:56:53 +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")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-2
@@ -328,9 +328,13 @@ 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 {
|
||||||
|
if err := deleteUserOAuthBindingsByUserId(tx, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return tx.Unscoped().Delete(&User{}, "id = ?", id).Error
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func inviteUser(inviterId int) (err error) {
|
func inviteUser(inviterId int) (err error) {
|
||||||
user, err := GetUserById(inviterId, true)
|
user, err := GetUserById(inviterId, true)
|
||||||
@@ -589,9 +593,13 @@ 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 {
|
||||||
|
if err := deleteUserOAuthBindingsByUserId(tx, user.Id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return tx.Unscoped().Delete(user).Error
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateAndFill check password & user status
|
// ValidateAndFill check password & user status
|
||||||
func (user *User) ValidateAndFill() (err error) {
|
func (user *User) ValidateAndFill() (err error) {
|
||||||
|
|||||||
@@ -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