mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2025-06-11 10:53:19 +02:00
Session initialization
This commit is contained in:
37
bffhd/session/db.rs
Normal file
37
bffhd/session/db.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::sync::Arc;
|
||||
use lmdb::{DatabaseFlags, Environment};
|
||||
|
||||
use rkyv::{Archive, Serialize, Deserialize};
|
||||
|
||||
use crate::db::{AllocAdapter, DB, RawDB};
|
||||
use crate::users::UserRef;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Archive, Serialize, Deserialize)]
|
||||
pub struct Session {
|
||||
userid: UserRef,
|
||||
}
|
||||
|
||||
type Adapter = AllocAdapter<Session>;
|
||||
pub struct SessionCache {
|
||||
env: Arc<Environment>,
|
||||
db: DB<Adapter>,
|
||||
}
|
||||
|
||||
impl SessionCache {
|
||||
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>) -> lmdb::Result<Self> {
|
||||
let db = RawDB::open(&env, Some("sessions"))?;
|
||||
Ok(Self::new(env, db))
|
||||
}
|
||||
|
||||
pub unsafe fn create(env: Arc<Environment>) -> lmdb::Result<Self> {
|
||||
let flags = DatabaseFlags::empty();
|
||||
let db = RawDB::create(&env, Some("sessions"), flags)?;
|
||||
Ok(Self::new(env, db))
|
||||
}
|
||||
}
|
@ -1,38 +1,46 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use anyhow::Context;
|
||||
use lmdb::Environment;
|
||||
use once_cell::sync::OnceCell;
|
||||
use crate::authorization::roles::Role;
|
||||
use crate::resources::Resource;
|
||||
use crate::users::User;
|
||||
use crate::session::db::SessionCache;
|
||||
use crate::Users;
|
||||
use crate::users::UserRef;
|
||||
|
||||
struct Inner {
|
||||
mod db;
|
||||
|
||||
}
|
||||
impl Inner {
|
||||
pub fn new() -> Self {
|
||||
Self { }
|
||||
}
|
||||
}
|
||||
static SESSION_CACHE: OnceCell<SessionCache> = OnceCell::new();
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SessionManager {
|
||||
inner: Arc<Inner>,
|
||||
users: Users,
|
||||
}
|
||||
impl SessionManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(Inner::new()),
|
||||
}
|
||||
pub fn new(users: Users) -> Self {
|
||||
Self { users }
|
||||
}
|
||||
|
||||
// TODO: make infallible
|
||||
pub fn open(&self, uid: impl AsRef<str>) -> Option<SessionHandle> {
|
||||
unimplemented!()
|
||||
let uid = uid.as_ref();
|
||||
if let Some(user) = self.users.get_user(uid) {
|
||||
tracing::trace!(uid, "opening new session for user");
|
||||
Some(SessionHandle { user: UserRef::new(user.id) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SessionHandle {
|
||||
user: UserRef,
|
||||
}
|
||||
|
||||
impl SessionHandle {
|
||||
pub fn get_user(&self) -> User {
|
||||
pub fn get_user(&self) -> UserRef {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user