Allows Access to write to the underlying database

This commit is contained in:
Gregor Reitzenstein 2020-09-10 12:30:32 +02:00
parent 565a5ed278
commit 8e10381fbd
2 changed files with 21 additions and 1 deletions

View File

@ -87,6 +87,20 @@ impl PermissionsProvider {
Err(e) => { Err(e.into()) }
}
}
fn put_role(&self, txn: &mut RwTransaction, roleID: RoleIdentifier, role: Role) -> Result<()> {
let bytes = flexbuffers::to_vec(role)?;
txn.put(self.roledb, &roleID.to_ne_bytes(), &bytes, lmdb::WriteFlags::empty())?;
Ok(())
}
fn put_user(&self, txn: &mut RwTransaction, userID: UserIdentifier, user: User) -> Result<()> {
let bytes = flexbuffers::to_vec(user)?;
txn.put(self.userdb, &userID.to_ne_bytes(), &bytes, lmdb::WriteFlags::empty())?;
Ok(())
}
}
/// This line documents init
@ -94,8 +108,12 @@ pub fn init(log: Logger, config: &Config, env: &lmdb::Environment) -> std::resul
let mut flags = lmdb::DatabaseFlags::empty();
flags.set(lmdb::DatabaseFlags::INTEGER_KEY, true);
let roledb = env.create_db(Some("role"), flags)?;
debug!(&log, "Opened access database '{}' successfully.", "role");
let permdb = env.create_db(Some("perm"), flags)?;
debug!(&log, "Opened access database '{}' successfully.", "perm");
let userdb = env.create_db(Some("user"), flags)?;
debug!(&log, "Opened access database '{}' successfully.", "user");
info!(&log, "Opened all access databases");
return Ok(PermissionsProvider::new(log, roledb, permdb, userdb));
}

View File

@ -100,9 +100,11 @@ fn main() -> Result<(), Error> {
// Initialize the LMDB environment. Since this would usually block untill the mmap() finishes
// we wrap it in smol::unblock which runs this as future in a different thread.
let e_config = config.clone();
info!(log, "LMDB env");
let env = lmdb::Environment::new()
.set_flags(lmdb::EnvironmentFlags::MAP_ASYNC | lmdb::EnvironmentFlags::NO_SUB_DIR)
.set_max_dbs(LMDB_MAX_DB as libc::c_uint)
.open(&e_config.db)?;
.open(&PathBuf::from_str("/tmp/a.db").unwrap())?;
// Start loading the machine database, authentication system and permission system
// All of those get a custom logger so the source of a log message can be better traced and