fabaccess-bffh/bffhd/capnp/user.rs

71 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2022-03-21 00:01:50 +01:00
use crate::authorization::permissions::Permission;
2022-05-05 15:50:44 +02:00
use crate::session::SessionHandle;
2022-03-21 00:01:50 +01:00
use crate::users::{db, UserRef};
2022-11-04 17:25:17 +01:00
use api::user_capnp::user::{self, self_service, manage, admin};
use capnp::capability::{Promise, FromClientHook};
2022-05-05 15:50:44 +02:00
use capnp_rpc::pry;
use api::role_capnp;
use crate::capnp::role::Role;
2022-03-21 00:01:50 +01:00
#[derive(Clone)]
pub struct User {
2022-03-12 17:31:53 +01:00
session: SessionHandle,
2022-03-21 00:01:50 +01:00
user: UserRef,
2022-03-12 17:31:53 +01:00
}
impl User {
2022-03-21 00:01:50 +01:00
pub fn new(session: SessionHandle, user: UserRef) -> Self {
Self { session, user }
}
2022-03-21 00:01:50 +01:00
pub fn new_self(session: SessionHandle) -> Self {
let user = session.get_user_ref();
Self::new(session, user)
}
pub fn fill(session: SessionHandle, user: db::User) -> Self {
Self {
user: UserRef::new(user.id),
session,
2022-03-21 00:01:50 +01:00
}
}
}
2022-11-04 17:25:17 +01:00
impl user::Server for User {
fn roles(
&mut self,
2022-11-04 17:25:17 +01:00
_: user::RolesParams,
mut result: user::RolesResults,
2022-03-21 00:01:50 +01:00
) -> Promise<(), ::capnp::Error> {
if let Some(user) = self.session.users.get_user(self.user.get_username()) {
let mut builder = result.get().init_roles(user.userdata.roles.len() as u32);
for (i, role) in user.userdata.roles.into_iter().enumerate() {
let role = Role::new(self.session.clone(), role);
let cap: role_capnp::role::Client = capnp_rpc::new_client(role);
builder.reborrow().set(i as u32, cap.into_client_hook());
}
2022-03-21 00:01:50 +01:00
}
Promise::ok(())
}
}
impl manage::Server for User {
2022-03-21 00:01:50 +01:00
2022-11-04 17:25:17 +01:00
}
impl admin::Server for User {
fn setpw(
&mut self,
2022-11-04 17:25:17 +01:00
param: admin::SetpwParams,
_: admin::SetpwResults,
) -> Promise<(), ::capnp::Error> {
let new_pw = pry!(pry!(param.get()).get_new());
let uid = self.user.get_username();
if let Some(mut user) = self.session.users.get_user(uid) {
user.set_pw(new_pw.as_bytes());
self.session.users.put_user(uid, &user);
}
Promise::ok(())
}
}