From dfbe02c2b8410164b33beebaf298e0826433d8df Mon Sep 17 00:00:00 2001 From: Sora39831 <540587985@qq.com> Date: Sun, 5 Apr 2026 03:40:32 +0800 Subject: [PATCH] feat(user): delete all user inbounds when deleting user --- web/controller/user.go | 12 ------------ web/service/user.go | 32 +++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/web/controller/user.go b/web/controller/user.go index dcbd3454..6e3ccb07 100644 --- a/web/controller/user.go +++ b/web/controller/user.go @@ -105,18 +105,6 @@ func (a *UserController) deleteUser(c *gin.Context) { } currentUser := session.GetLoginUser(c) - inbounds, err := a.inboundService.GetInbounds(id) - if err != nil { - jsonMsg(c, I18nWeb(c, "pages.users.toasts.delete"), err) - return - } - for _, inbound := range inbounds { - if _, err := a.inboundService.DelInbound(inbound.Id); err != nil { - jsonMsg(c, I18nWeb(c, "pages.users.toasts.delete"), err) - return - } - } - err = a.userService.DeleteUser(id, currentUser.Id) if err != nil { jsonMsg(c, I18nWeb(c, "pages.users.toasts.delete"), a.localizeUserError(c, err)) diff --git a/web/service/user.go b/web/service/user.go index 4704c767..55dfa5d1 100644 --- a/web/service/user.go +++ b/web/service/user.go @@ -382,18 +382,28 @@ func (s *UserService) DeleteUser(id int, currentUserId int) error { return err } - return db.Transaction(func(tx *gorm.DB) error { - if user.Role == "admin" { - adminCount, err := s.countAdmins(tx) - if err != nil { - return err - } - if adminCount <= 1 { - return ErrLastAdminRequired - } + if user.Role == "admin" { + adminCount, err := s.countAdmins(db) + if err != nil { + return err } - return tx.Delete(&model.User{}, id).Error - }) + if adminCount <= 1 { + return ErrLastAdminRequired + } + } + + inboundService := InboundService{} + inbounds, err := inboundService.GetInbounds(id) + if err != nil { + return err + } + for _, inbound := range inbounds { + if _, err := inboundService.DelInbound(inbound.Id); err != nil { + return err + } + } + + return db.Delete(&model.User{}, id).Error } func (s *UserService) RegisterUser(username string, password string, inboundService *InboundService) error {