feat(user): delete all user inbounds when deleting user

This commit is contained in:
Sora39831 2026-04-05 03:40:32 +08:00
parent 062f2bd714
commit dfbe02c2b8
2 changed files with 21 additions and 23 deletions

View file

@ -105,18 +105,6 @@ func (a *UserController) deleteUser(c *gin.Context) {
} }
currentUser := session.GetLoginUser(c) 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) err = a.userService.DeleteUser(id, currentUser.Id)
if err != nil { if err != nil {
jsonMsg(c, I18nWeb(c, "pages.users.toasts.delete"), a.localizeUserError(c, err)) jsonMsg(c, I18nWeb(c, "pages.users.toasts.delete"), a.localizeUserError(c, err))

View file

@ -382,18 +382,28 @@ func (s *UserService) DeleteUser(id int, currentUserId int) error {
return err return err
} }
return db.Transaction(func(tx *gorm.DB) error { if user.Role == "admin" {
if user.Role == "admin" { adminCount, err := s.countAdmins(db)
adminCount, err := s.countAdmins(tx) if err != nil {
if err != nil { return err
return err
}
if adminCount <= 1 {
return ErrLastAdminRequired
}
} }
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 { func (s *UserService) RegisterUser(username string, password string, inboundService *InboundService) error {