mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2025-06-11 19:03:21 +02:00
implement dumping and loading the full database
This commit is contained in:
@ -178,6 +178,29 @@ impl Users {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_map(&mut self, dump: &HashMap<String,UserData>) -> miette::Result<()> {
|
||||
let mut txn = unsafe { self.userdb.get_rw_txn() }?;
|
||||
|
||||
self.userdb.clear_txn(&mut txn)?;
|
||||
|
||||
for (uid, data) in dump {
|
||||
let user = db::User {
|
||||
id: uid.clone(),
|
||||
userdata: data.clone(),
|
||||
};
|
||||
|
||||
tracing::trace!(%uid, ?user, "Storing user object");
|
||||
if let Err(e) = self.userdb.put_txn(&mut txn, uid.as_str(), &user) {
|
||||
tracing::warn!(error=?e, "failed to add user")
|
||||
}
|
||||
}
|
||||
txn.commit().map_err(crate::db::Error::from)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn dump_map(&self) -> miette::Result<HashMap<String, UserData>> {
|
||||
return Ok(self.userdb.get_all()?)
|
||||
}
|
||||
pub fn dump_file(&self, path_str: &str, force: bool) -> miette::Result<usize> {
|
||||
let path = Path::new(path_str);
|
||||
let exists = path.exists();
|
||||
@ -208,7 +231,7 @@ impl Users {
|
||||
}
|
||||
let mut file = fs::File::create(path).into_diagnostic()?;
|
||||
|
||||
let users = self.userdb.get_all()?;
|
||||
let users = self.dump_map()?;
|
||||
let encoded = toml::ser::to_vec(&users).into_diagnostic()?;
|
||||
file.write_all(&encoded[..]).into_diagnostic()?;
|
||||
|
||||
|
Reference in New Issue
Block a user