Skip to content

Commit 80b1d02

Browse files
anton-khimichAnton Khimich6543
authored
Fix gpg key deletion (#14561)
* Fix GPG key deletion when user is deleted 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. * Format added code in models/user.go * Create a new function for listing GPG keys and apply it Create a new function `listGPGKeys` and replace a previous use of `ListGPGKeys`. Thanks to @6543 for the patch. Co-authored-by: Anton Khimich <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 3c965c3 commit 80b1d02

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
@@ -1208,6 +1208,16 @@ func deleteUser(e Engine, u *User) error {
12081208
// ***** END: PublicKey *****
12091209

12101210
// ***** START: GPGPublicKey *****
1211+
keys, err := listGPGKeys(e, u.ID, ListOptions{})
1212+
if err != nil {
1213+
return fmt.Errorf("ListGPGKeys: %v", err)
1214+
}
1215+
// Delete GPGKeyImport(s).
1216+
for _, key := range keys {
1217+
if _, err = e.Delete(&GPGKeyImport{KeyID: key.KeyID}); err != nil {
1218+
return fmt.Errorf("deleteGPGKeyImports: %v", err)
1219+
}
1220+
}
12111221
if _, err = e.Delete(&GPGKey{OwnerID: u.ID}); err != nil {
12121222
return fmt.Errorf("deleteGPGKeys: %v", err)
12131223
}

0 commit comments

Comments
 (0)