Skip to content

Commit da7a525

Browse files
anton-khimichAnton Khimich
and
Anton Khimich
authored
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 <[email protected]>
1 parent 0143131 commit da7a525

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

models/gpg_key.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
6565

6666
// ListGPGKeys returns a list of public keys belongs to given user.
6767
func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) {
68-
sess := x.Where("owner_id=? AND primary_key_id=''", uid)
68+
return listGPGKeys(x, uid, listOptions)
69+
}
70+
71+
func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) {
72+
sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)
6973
if listOptions.Page != 0 {
7074
sess = listOptions.setSessionPagination(sess)
7175
}

models/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,16 @@ func deleteUser(e Engine, u *User) error {
11221122
// ***** END: PublicKey *****
11231123

11241124
// ***** START: GPGPublicKey *****
1125+
keys, err := listGPGKeys(e, u.ID, ListOptions{})
1126+
if err != nil {
1127+
return fmt.Errorf("ListGPGKeys: %v", err)
1128+
}
1129+
// Delete GPGKeyImport(s).
1130+
for _, key := range keys {
1131+
if _, err = e.Delete(&GPGKeyImport{KeyID: key.KeyID}); err != nil {
1132+
return fmt.Errorf("deleteGPGKeyImports: %v", err)
1133+
}
1134+
}
11251135
if _, err = e.Delete(&GPGKey{OwnerID: u.ID}); err != nil {
11261136
return fmt.Errorf("deleteGPGKeys: %v", err)
11271137
}

0 commit comments

Comments
 (0)