]> source.dussan.org Git - gitea.git/commitdiff
Fix gpg key deletion (#14561)
authorAnton Khimich <anton.khimich@gmail.com>
Thu, 4 Feb 2021 09:16:21 +0000 (04:16 -0500)
committerGitHub <noreply@github.com>
Thu, 4 Feb 2021 09:16:21 +0000 (17:16 +0800)
* 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 <anton.khimicha@mail.utoronto.ca>
Co-authored-by: 6543 <6543@obermui.de>
models/gpg_key.go
models/user.go

index b944fdcbffe456ce6670134ac8f9a569b8ef3ccd..3e8ddd9621c1e3ee0da127b906e4d7c320db388e 100644 (file)
@@ -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)
        }
index 8147c9f62610bb36be43317976b8c6575afe8f87..495fed1ff4d0367312110f30e9c2c5580b97c987 100644 (file)
@@ -1208,6 +1208,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)
        }