1
0
Fork 0
mirror of https://gitlab.com/hagrid-keyserver/hagrid.git synced 2023-02-13 20:55:02 -05:00

database: Introduce function to delete specific userids.

- Fixes #88.
This commit is contained in:
Justus Winter 2019-04-12 14:41:19 +02:00
parent 3e81d2dc17
commit 667980848a
No known key found for this signature in database
GPG key ID: 686F55B4AB2B3386
2 changed files with 20 additions and 6 deletions

View file

@ -529,6 +529,24 @@ pub trait Database: Sync + Send {
self.filter_userids(fpr, |_| false)
}
/// Deletes all UserID packets matching `addr` (see [RFC2822
/// name-addr] and unlinks the email addresses.
///
/// [RFC2822 name-addr]: https://tools.ietf.org/html/rfc2822#section-3.4
fn delete_userids_matching(&self, fpr: &Fingerprint, addr: &Email)
-> Result <()> {
self.filter_userids(fpr, |uid| {
match Email::try_from(uid) {
// Keep those not matching `addr`.
Ok(a) => a != *addr,
// This should not happen, because all TPKs in the
// database should only have UserIDs with well-formed
// values. Be conservative and keep the component.
Err(_) => true,
}
})
}
/// Deletes all user ids NOT matching fulfilling `filter`.
///
/// I.e. we retain fulfilling `filter`.

View file

@ -323,9 +323,7 @@ pub fn test_uid_deletion<D: Database>(db: &mut D) {
let fpr = Fingerprint::try_from(tpk.fingerprint()).unwrap();
// Delete second UID.
db.filter_userids(
&fpr, |u| Email::try_from(u).map(|e| e != email2).unwrap_or(true))
.unwrap();
db.delete_userids_matching(&fpr, &email2).unwrap();
// Check that the second is still there, and that the TPK is
// otherwise intact.
@ -334,9 +332,7 @@ pub fn test_uid_deletion<D: Database>(db: &mut D) {
assert_eq!(tpk.subkeys().count(), n_subkeys);
// Delete first UID.
db.filter_userids(
&fpr, |u| Email::try_from(u).map(|e| e != email1).unwrap_or(true))
.unwrap();
db.delete_userids_matching(&fpr, &email1).unwrap();
// Check that the second is still there, and that the TPK is
// otherwise intact.