From a839056d698910fb774302ebecb02c9c34cdebe7 Mon Sep 17 00:00:00 2001 From: uubulb Date: Sat, 21 Dec 2024 19:07:19 +0800 Subject: [PATCH] update user api error handling --- cmd/dashboard/controller/user.go | 4 +- service/singleton/user.go | 70 ++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/cmd/dashboard/controller/user.go b/cmd/dashboard/controller/user.go index 12b236f..5b5c019 100644 --- a/cmd/dashboard/controller/user.go +++ b/cmd/dashboard/controller/user.go @@ -151,6 +151,6 @@ func batchDeleteUser(c *gin.Context) (any, error) { return nil, singleton.Localizer.ErrorT("can't delete yourself") } - singleton.OnUserDelete(ids) - return nil, singleton.DB.Where("id IN (?)", ids).Delete(&model.User{}).Error + err := singleton.OnUserDelete(ids, newGormError) + return nil, err } diff --git a/service/singleton/user.go b/service/singleton/user.go index 88c333d..1a6e98e 100644 --- a/service/singleton/user.go +++ b/service/singleton/user.go @@ -39,50 +39,65 @@ func OnUserUpdate(u *model.User) { AgentSecretToUserId[u.AgentSecret] = u.ID } -func OnUserDelete(id []uint64) { +func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) error { UserLock.Lock() defer UserLock.Unlock() if len(id) < 1 { - return + return Localizer.ErrorT("user id not specified") } var ( - cron bool - server bool + cron, server bool + crons, servers []uint64 ) for _, uid := range id { - secret := UserIdToAgentSecret[uid] - delete(AgentSecretToUserId, secret) - delete(UserIdToAgentSecret, uid) + err := DB.Transaction(func(tx *gorm.DB) error { + CronLock.RLock() + crons = model.FindUserID(CronList, uid) + CronLock.RUnlock() - CronLock.RLock() - crons := model.FindUserID(CronList, uid) - CronLock.RUnlock() + cron = len(crons) > 0 + if cron { + if err := tx.Unscoped().Delete(&model.Cron{}, "id in (?)", crons).Error; err != nil { + return err + } + } - cron = len(crons) > 0 - if cron { - DB.Unscoped().Delete(&model.Cron{}, "id in (?)", crons) - OnDeleteCron(crons) - } + SortedServerLock.RLock() + servers = model.FindUserID(SortedServerList, uid) + SortedServerLock.RUnlock() - SortedServerLock.RLock() - servers := model.FindUserID(SortedServerList, uid) - SortedServerLock.RUnlock() - - server = len(servers) > 0 - if server { - DB.Transaction(func(tx *gorm.DB) error { + server = len(servers) > 0 + if server { if err := tx.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil { return err } if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_id in (?)", servers).Error; err != nil { return err } - return nil - }) + } + if err := tx.Unscoped().Delete(&model.Transfer{}, "server_id in (?)", servers).Error; err != nil { + return err + } + + if err := tx.Where("id IN (?)", id).Delete(&model.User{}).Error; err != nil { + return err + } + return nil + }) + + if err != nil { + return errorFunc("%v", err) + } + + if cron { + OnDeleteCron(crons) + } + + if server { AlertsLock.Lock() for _, sid := range servers { for _, alert := range Alerts { @@ -93,10 +108,13 @@ func OnUserDelete(id []uint64) { } } } - DB.Unscoped().Delete(&model.Transfer{}, "server_id in (?)", servers) AlertsLock.Unlock() OnServerDelete(servers) } + + secret := UserIdToAgentSecret[uid] + delete(AgentSecretToUserId, secret) + delete(UserIdToAgentSecret, uid) } if cron { @@ -106,4 +124,6 @@ func OnUserDelete(id []uint64) { if server { ReSortServer() } + + return nil }