mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	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 <anton.khimicha@mail.utoronto.ca> Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
		@@ -65,7 +65,11 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ListGPGKeys returns a list of public keys belongs to given user.
 | 
					// ListGPGKeys returns a list of public keys belongs to given user.
 | 
				
			||||||
func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) {
 | 
					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 {
 | 
						if listOptions.Page != 0 {
 | 
				
			||||||
		sess = listOptions.setSessionPagination(sess)
 | 
							sess = listOptions.setSessionPagination(sess)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1208,6 +1208,16 @@ func deleteUser(e Engine, u *User) error {
 | 
				
			|||||||
	// ***** END: PublicKey *****
 | 
						// ***** END: PublicKey *****
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ***** START: GPGPublicKey *****
 | 
						// ***** 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 {
 | 
						if _, err = e.Delete(&GPGKey{OwnerID: u.ID}); err != nil {
 | 
				
			||||||
		return fmt.Errorf("deleteGPGKeys: %v", err)
 | 
							return fmt.Errorf("deleteGPGKeys: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user