From 9cf9a54dca008800001c18f2018db98326d44fa5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 11 Dec 2018 10:33:20 +0800 Subject: [PATCH] fix undeleted content when deleting user (#5509) --- models/ssh_key.go | 7 ++++++- models/user.go | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/models/ssh_key.go b/models/ssh_key.go index c2836e688..e782e1ed0 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -535,6 +535,7 @@ func DeletePublicKey(doer *User, id int64) (err error) { if err = sess.Commit(); err != nil { return err } + sess.Close() return RewriteAllPublicKeys() } @@ -543,6 +544,10 @@ func DeletePublicKey(doer *User, id int64) (err error) { // Note: x.Iterate does not get latest data after insert/delete, so we have to call this function // outside any session scope independently. func RewriteAllPublicKeys() error { + return rewriteAllPublicKeys(x) +} + +func rewriteAllPublicKeys(e Engine) error { //Don't rewrite key if internal server if setting.SSH.StartBuiltinServer { return nil @@ -569,7 +574,7 @@ func RewriteAllPublicKeys() error { } } - err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) { + err = e.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) { _, err = t.WriteString((bean.(*PublicKey)).AuthorizedString()) return err }) diff --git a/models/user.go b/models/user.go index 01c7f5048..49621248f 100644 --- a/models/user.go +++ b/models/user.go @@ -1038,25 +1038,26 @@ func deleteUser(e *xorm.Session, u *User) error { &EmailAddress{UID: u.ID}, &UserOpenID{UID: u.ID}, &Reaction{UserID: u.ID}, + &TeamUser{UID: u.ID}, + &Collaboration{UserID: u.ID}, + &Stopwatch{UserID: u.ID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } // ***** START: PublicKey ***** - keys := make([]*PublicKey, 0, 10) - if err = e.Find(&keys, &PublicKey{OwnerID: u.ID}); err != nil { - return fmt.Errorf("get all public keys: %v", err) - } - - keyIDs := make([]int64, len(keys)) - for i := range keys { - keyIDs[i] = keys[i].ID - } - if err = deletePublicKeys(e, keyIDs...); err != nil { + if _, err = e.Delete(&PublicKey{OwnerID: u.ID}); err != nil { return fmt.Errorf("deletePublicKeys: %v", err) } + rewriteAllPublicKeys(e) // ***** END: PublicKey ***** + // ***** START: GPGPublicKey ***** + if _, err = e.Delete(&GPGKey{OwnerID: u.ID}); err != nil { + return fmt.Errorf("deleteGPGKeys: %v", err) + } + // ***** END: GPGPublicKey ***** + // Clear assignee. if err = clearAssigneeByUserID(e, u.ID); err != nil { return fmt.Errorf("clear assignee: %v", err) @@ -1110,6 +1111,7 @@ func DeleteUser(u *User) (err error) { if err = sess.Commit(); err != nil { return err } + sess.Close() return RewriteAllPublicKeys() }