From da7a525c5c96e97126d5932f3a08fabfaef11b46 Mon Sep 17 00:00:00 2001 From: Anton Khimich Date: Thu, 4 Feb 2021 15:28:48 -0500 Subject: [PATCH] Fix GPG key deletion during account deletion (#14561) (#14569) Per #14531, deleting a user account will delete the user's GPG keys from the `gpg_key` table but not from `gpg_key_import`, which causes an error when creating an account with the same email and attempting to re-add the same key. This commit deletes all entries from `gpg_key_import` that match any GPG key IDs belonging to the user. Co-authored-by: Anton Khimich --- models/gpg_key.go | 6 +++++- models/user.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/models/gpg_key.go b/models/gpg_key.go index b944fdcbf..3e8ddd962 100644 --- a/models/gpg_key.go +++ b/models/gpg_key.go @@ -65,7 +65,11 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) { // ListGPGKeys returns a list of public keys belongs to given user. func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) { - sess := x.Where("owner_id=? AND primary_key_id=''", uid) + return listGPGKeys(x, uid, listOptions) +} + +func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) { + sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid) if listOptions.Page != 0 { sess = listOptions.setSessionPagination(sess) } diff --git a/models/user.go b/models/user.go index f8e0ae8cb..9cd074a42 100644 --- a/models/user.go +++ b/models/user.go @@ -1122,6 +1122,16 @@ func deleteUser(e Engine, u *User) error { // ***** END: PublicKey ***** // ***** START: GPGPublicKey ***** + keys, err := listGPGKeys(e, u.ID, ListOptions{}) + if err != nil { + return fmt.Errorf("ListGPGKeys: %v", err) + } + // Delete GPGKeyImport(s). + for _, key := range keys { + if _, err = e.Delete(&GPGKeyImport{KeyID: key.KeyID}); err != nil { + return fmt.Errorf("deleteGPGKeyImports: %v", err) + } + } if _, err = e.Delete(&GPGKey{OwnerID: u.ID}); err != nil { return fmt.Errorf("deleteGPGKeys: %v", err) }