mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2024-11-25 16:17:56 +01:00
83 lines
2.3 KiB
Rust
83 lines
2.3 KiB
Rust
use crate::db::{AllocAdapter, Environment, RawDB, Result, DB};
|
|
use crate::db::{DatabaseFlags, LMDBorrow, RoTransaction, WriteFlags};
|
|
use lmdb::{RwTransaction, Transaction};
|
|
use std::collections::HashSet;
|
|
use std::sync::Arc;
|
|
|
|
use rkyv::{Archived, Deserialize};
|
|
|
|
#[derive(
|
|
Clone,
|
|
PartialEq,
|
|
Eq,
|
|
Debug,
|
|
rkyv::Archive,
|
|
rkyv::Serialize,
|
|
rkyv::Deserialize,
|
|
serde::Serialize,
|
|
serde::Deserialize,
|
|
)]
|
|
pub struct User {
|
|
id: u128,
|
|
username: String,
|
|
roles: Vec<String>,
|
|
}
|
|
|
|
type Adapter = AllocAdapter<User>;
|
|
#[derive(Clone, Debug)]
|
|
pub struct UserDB {
|
|
env: Arc<Environment>,
|
|
db: DB<Adapter>,
|
|
}
|
|
|
|
impl UserDB {
|
|
pub unsafe fn new(env: Arc<Environment>, db: RawDB) -> Self {
|
|
let db = DB::new_unchecked(db);
|
|
Self { env, db }
|
|
}
|
|
|
|
pub unsafe fn open(env: Arc<Environment>) -> Result<Self> {
|
|
let db = RawDB::open(&env, Some("user"))?;
|
|
Ok(Self::new(env, db))
|
|
}
|
|
|
|
pub unsafe fn create(env: Arc<Environment>) -> Result<Self> {
|
|
let flags = DatabaseFlags::empty();
|
|
let db = RawDB::create(&env, Some("user"), flags)?;
|
|
Ok(Self::new(env, db))
|
|
}
|
|
|
|
pub fn get(&self, uid: &str) -> Result<Option<LMDBorrow<RoTransaction, Archived<User>>>> {
|
|
let txn = self.env.begin_ro_txn()?;
|
|
if let Some(state) = self.db.get(&txn, &uid.as_bytes())? {
|
|
let ptr = state.into();
|
|
Ok(Some(unsafe { LMDBorrow::new(ptr, txn) }))
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
pub fn put(&self, uid: &str, user: &User) -> Result<()> {
|
|
let mut txn = self.env.begin_rw_txn()?;
|
|
let flags = WriteFlags::empty();
|
|
self.db.put(&mut txn, &uid.as_bytes(), user, flags)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_all(&self) -> Result<Vec<(String, User)>> {
|
|
let txn = self.env.begin_ro_txn()?;
|
|
let mut cursor = self.db.open_ro_cursor(&txn)?;
|
|
let iter = cursor.iter_start();
|
|
let mut out = Vec::new();
|
|
let mut deserializer = rkyv::Infallible;
|
|
for user in iter {
|
|
let (uid, user) = user?;
|
|
let uid = unsafe { std::str::from_utf8_unchecked(uid).to_string() };
|
|
let user: User = user.deserialize(&mut deserializer).unwrap();
|
|
out.push((uid, user));
|
|
}
|
|
|
|
Ok(out)
|
|
}
|
|
}
|