fabaccess-bffh/bffhd/session/mod.rs

81 lines
2.1 KiB
Rust
Raw Normal View History

2022-03-15 20:00:43 +01:00
2022-03-15 17:52:47 +01:00
use once_cell::sync::OnceCell;
2022-03-15 20:00:43 +01:00
use crate::authorization::roles::{Roles};
2022-03-13 17:29:21 +01:00
use crate::resources::Resource;
2022-03-15 17:52:47 +01:00
use crate::Users;
use crate::users::UserRef;
2022-03-12 17:31:53 +01:00
#[derive(Clone)]
pub struct SessionManager {
2022-03-15 17:52:47 +01:00
users: Users,
2022-03-15 19:14:04 +01:00
roles: Roles,
// cache: SessionCache // todo
2022-03-12 17:31:53 +01:00
}
impl SessionManager {
2022-03-15 19:14:04 +01:00
pub fn new(users: Users, roles: Roles) -> Self {
Self { users, roles }
2022-03-12 17:31:53 +01:00
}
2022-03-15 17:52:47 +01:00
// TODO: make infallible
2022-03-12 17:31:53 +01:00
pub fn open(&self, uid: impl AsRef<str>) -> Option<SessionHandle> {
2022-03-15 17:52:47 +01:00
let uid = uid.as_ref();
if let Some(user) = self.users.get_user(uid) {
2022-03-16 19:01:09 +01:00
tracing::trace!(uid, ?user, "opening new session for user");
2022-03-15 19:14:04 +01:00
Some(SessionHandle {
users: self.users.clone(),
roles: self.roles.clone(),
user: UserRef::new(user.id),
})
2022-03-15 17:52:47 +01:00
} else {
None
}
2022-03-12 17:31:53 +01:00
}
}
2022-03-15 19:14:04 +01:00
#[derive(Clone)]
2022-03-12 17:31:53 +01:00
pub struct SessionHandle {
2022-03-15 19:14:04 +01:00
users: Users,
roles: Roles,
2022-03-15 17:52:47 +01:00
user: UserRef,
2022-03-12 17:31:53 +01:00
}
impl SessionHandle {
2022-03-15 17:52:47 +01:00
pub fn get_user(&self) -> UserRef {
2022-03-15 19:14:04 +01:00
self.user.clone()
2022-03-13 17:29:21 +01:00
}
pub fn has_disclose(&self, resource: &Resource) -> bool {
2022-03-15 19:14:04 +01:00
if let Some(user) = self.users.get_user(self.user.get_username()) {
self.roles.is_permitted(&user.userdata, &resource.get_required_privs().disclose)
} else {
false
}
2022-03-13 17:29:21 +01:00
}
pub fn has_read(&self, resource: &Resource) -> bool {
2022-03-15 19:14:04 +01:00
if let Some(user) = self.users.get_user(self.user.get_username()) {
self.roles.is_permitted(&user.userdata, &resource.get_required_privs().read)
} else {
false
}
2022-03-13 17:29:21 +01:00
}
pub fn has_write(&self, resource: &Resource) -> bool {
2022-03-15 19:14:04 +01:00
if let Some(user) = self.users.get_user(self.user.get_username()) {
self.roles.is_permitted(&user.userdata, &resource.get_required_privs().write)
} else {
false
}
2022-03-13 17:29:21 +01:00
}
pub fn has_manage(&self, resource: &Resource) -> bool {
2022-03-15 19:14:04 +01:00
if let Some(user) = self.users.get_user(self.user.get_username()) {
self.roles.is_permitted(&user.userdata, &resource.get_required_privs().manage)
} else {
false
}
2022-03-13 17:29:21 +01:00
}
2022-03-12 17:31:53 +01:00
}