update user api error handling

This commit is contained in:
uubulb 2024-12-21 19:07:19 +08:00
parent 4f4c482103
commit a839056d69
2 changed files with 47 additions and 27 deletions

View File

@ -151,6 +151,6 @@ func batchDeleteUser(c *gin.Context) (any, error) {
return nil, singleton.Localizer.ErrorT("can't delete yourself") return nil, singleton.Localizer.ErrorT("can't delete yourself")
} }
singleton.OnUserDelete(ids) err := singleton.OnUserDelete(ids, newGormError)
return nil, singleton.DB.Where("id IN (?)", ids).Delete(&model.User{}).Error return nil, err
} }

View File

@ -39,50 +39,65 @@ func OnUserUpdate(u *model.User) {
AgentSecretToUserId[u.AgentSecret] = u.ID AgentSecretToUserId[u.AgentSecret] = u.ID
} }
func OnUserDelete(id []uint64) { func OnUserDelete(id []uint64, errorFunc func(string, ...interface{}) error) error {
UserLock.Lock() UserLock.Lock()
defer UserLock.Unlock() defer UserLock.Unlock()
if len(id) < 1 { if len(id) < 1 {
return return Localizer.ErrorT("user id not specified")
} }
var ( var (
cron bool cron, server bool
server bool crons, servers []uint64
) )
for _, uid := range id { for _, uid := range id {
secret := UserIdToAgentSecret[uid] err := DB.Transaction(func(tx *gorm.DB) error {
delete(AgentSecretToUserId, secret)
delete(UserIdToAgentSecret, uid)
CronLock.RLock() CronLock.RLock()
crons := model.FindUserID(CronList, uid) crons = model.FindUserID(CronList, uid)
CronLock.RUnlock() CronLock.RUnlock()
cron = len(crons) > 0 cron = len(crons) > 0
if cron { if cron {
DB.Unscoped().Delete(&model.Cron{}, "id in (?)", crons) if err := tx.Unscoped().Delete(&model.Cron{}, "id in (?)", crons).Error; err != nil {
OnDeleteCron(crons) return err
}
} }
SortedServerLock.RLock() SortedServerLock.RLock()
servers := model.FindUserID(SortedServerList, uid) servers = model.FindUserID(SortedServerList, uid)
SortedServerLock.RUnlock() SortedServerLock.RUnlock()
server = len(servers) > 0 server = len(servers) > 0
if server { if server {
DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil { if err := tx.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil {
return err return err
} }
if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_id in (?)", servers).Error; err != nil { if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_id in (?)", servers).Error; err != nil {
return err return err
} }
}
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 return nil
}) })
if err != nil {
return errorFunc("%v", err)
}
if cron {
OnDeleteCron(crons)
}
if server {
AlertsLock.Lock() AlertsLock.Lock()
for _, sid := range servers { for _, sid := range servers {
for _, alert := range Alerts { for _, alert := range Alerts {
@ -93,10 +108,13 @@ func OnUserDelete(id []uint64) {
} }
} }
} }
DB.Unscoped().Delete(&model.Transfer{}, "server_id in (?)", servers)
AlertsLock.Unlock() AlertsLock.Unlock()
OnServerDelete(servers) OnServerDelete(servers)
} }
secret := UserIdToAgentSecret[uid]
delete(AgentSecretToUserId, secret)
delete(UserIdToAgentSecret, uid)
} }
if cron { if cron {
@ -106,4 +124,6 @@ func OnUserDelete(id []uint64) {
if server { if server {
ReSortServer() ReSortServer()
} }
return nil
} }