2022-01-13 07:29:41 +00:00
|
|
|
use crate::ctx::Context;
|
|
|
|
use crate::dbs::Auth;
|
|
|
|
use crate::sql::value::Value;
|
2022-05-03 20:20:36 +00:00
|
|
|
use std::sync::Arc;
|
2022-01-13 07:29:41 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct Session {
|
2022-05-03 20:20:36 +00:00
|
|
|
pub au: Arc<Auth>, // Authentication info
|
2022-01-13 07:29:41 +00:00
|
|
|
pub ip: Option<String>, // Session ip address
|
|
|
|
pub or: Option<String>, // Session origin
|
|
|
|
pub id: Option<String>, // Session id
|
|
|
|
pub ns: Option<String>, // Namespace
|
|
|
|
pub db: Option<String>, // Database
|
|
|
|
pub sc: Option<String>, // Scope
|
|
|
|
pub sd: Option<Value>, // Scope auth data
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Session {
|
2022-05-03 20:20:36 +00:00
|
|
|
// Retrieves the selected namespace
|
|
|
|
pub fn ns(&self) -> Option<Arc<String>> {
|
|
|
|
self.ns.to_owned().map(Arc::new)
|
|
|
|
}
|
|
|
|
// Retrieves the selected database
|
|
|
|
pub fn db(&self) -> Option<Arc<String>> {
|
|
|
|
self.db.to_owned().map(Arc::new)
|
|
|
|
}
|
|
|
|
// Convert a session into a runtime
|
|
|
|
pub fn context(&self, mut ctx: Context) -> Context {
|
2022-01-13 07:29:41 +00:00
|
|
|
// Add session value
|
|
|
|
let key = String::from("session");
|
|
|
|
let val: Value = self.into();
|
|
|
|
ctx.add_value(key, val);
|
|
|
|
// Add scope value
|
|
|
|
let key = String::from("scope");
|
|
|
|
let val: Value = self.sc.to_owned().into();
|
|
|
|
ctx.add_value(key, val);
|
|
|
|
// Add auth data
|
|
|
|
let key = String::from("auth");
|
|
|
|
let val: Value = self.sd.to_owned().into();
|
|
|
|
ctx.add_value(key, val);
|
|
|
|
// Output context
|
2022-05-03 20:20:36 +00:00
|
|
|
ctx
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 16:01:32 +00:00
|
|
|
impl From<&Session> for Value {
|
|
|
|
fn from(val: &Session) -> Value {
|
2022-01-13 07:29:41 +00:00
|
|
|
Value::from(map! {
|
2022-03-04 16:01:32 +00:00
|
|
|
"ip".to_string() => val.ip.to_owned().into(),
|
|
|
|
"or".to_string() => val.or.to_owned().into(),
|
|
|
|
"id".to_string() => val.id.to_owned().into(),
|
|
|
|
"ns".to_string() => val.ns.to_owned().into(),
|
|
|
|
"db".to_string() => val.db.to_owned().into(),
|
|
|
|
"sc".to_string() => val.sc.to_owned().into(),
|
2022-01-13 07:29:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|