From 8e10381fbd2a14f4298edb22fe2ecf4df0890303 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Thu, 10 Sep 2020 12:30:32 +0200 Subject: [PATCH] Allows Access to write to the underlying database --- src/access.rs | 18 ++++++++++++++++++ src/main.rs | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/access.rs b/src/access.rs index 9dfec84..ffc9887 100644 --- a/src/access.rs +++ b/src/access.rs @@ -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)); } diff --git a/src/main.rs b/src/main.rs index a60888c..ea69ccf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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