added support for looking up user by (first)token

This commit is contained in:
Kai Jan Kriegel 2023-02-08 18:57:36 +01:00
parent dce3fd803b
commit 00f90c7314
2 changed files with 24 additions and 0 deletions

View File

@ -146,6 +146,23 @@ impl UserDB {
self.db.get(&txn, &uid.as_bytes())
}
pub fn get_by_token(&self, token: &str) -> Result<Option<ArchivedValue<User>>, db::Error> {
let txn = self.env.begin_ro_txn()?;
let mut iter = self.db.get_all(&txn)?.into_iter();
return Ok(iter.find_map(|(uid, archived_user)| {
let uid = unsafe { std::str::from_utf8_unchecked(uid).to_string() };
let user: User =
Deserialize::<User, _>::deserialize(archived_user.as_ref(), &mut Infallible).unwrap();
if user.userdata.kv.get("cardtoken") == Some(&token.to_string()) {
Some(archived_user)
} else {
None
}
}));
Ok(None)
}
pub fn put(&self, uid: &str, user: &User) -> Result<(), db::Error> {
let mut serializer = AllocSerializer::<1024>::default();
serializer.serialize_value(user).expect("rkyv error");

View File

@ -99,6 +99,13 @@ impl Users {
})
}
pub fn get_user_by_token(&self, token: &str) -> Option<db::User> {
tracing::trace!(token, "Looking up user by token");
self.userdb.get_by_token(token).unwrap().map(|user| {
Deserialize::<db::User, _>::deserialize(user.as_ref(), &mut Infallible).unwrap()
})
}
pub fn put_user(&self, uid: &str, user: &db::User) -> Result<(), crate::db::Error> {
tracing::trace!(uid, ?user, "Updating user");
self.userdb.put(uid, user)