This commit is contained in:
2026-02-18 16:47:35 -05:00
parent f9d8fccf40
commit 97fdbbf968
16 changed files with 199 additions and 156 deletions

View File

@@ -8,6 +8,7 @@ pub struct ServerPermsV0(u32);
impl ServerPermsV0 {
pub const NONE: Self = Self(0);
pub const ACCOUNT_TOKENS: Self = Self(1 << 0);
pub const ALL: Self = Self(u32::MAX);
}
#[derive(bitcode::Encode, bitcode::Decode)]

View File

@@ -7,7 +7,8 @@ use net::{ClientSender, ConAccepter, listen};
use openworm::{
net::{
ClientMsg, ClientRequestMsg, CreateAccount, CreateAccountResp, DisconnectReason, LoadMsg,
Login, LoginResp, RecvHandler, ServerError, ServerMsg, install_crypto_provider,
Login, LoginResp, RecvHandler, RequestUsersResp, ServerError, ServerMsg, ServerUser,
install_crypto_provider,
},
rsc::DataDir,
};
@@ -49,7 +50,7 @@ pub async fn run_server(port: u16) {
db: db.clone(),
};
if db.users.is_empty() {
let token = account_token(&db, ServerPerms::ACCOUNT_TOKENS);
let token = account_token(&db, ServerPerms::ALL);
println!("no users found, token for admin: {token}");
}
let (endpoint, handle) = listen(port, &dir.path, handler);
@@ -231,6 +232,27 @@ impl RecvHandler<ClientRequestMsg> for ClientHandler {
*self.state.write().await = ClientState::Authed(id);
let _ = replier.send(LoginResp::Ok { id }).await;
}
ClientMsg::RequestUsers(_) => {
if self
.db
.users
.get(&self.id)
.is_some_and(|u| !u.server_perms.contains(ServerPerms::ALL))
{
let _ = replier.send(ServerError::NoPermission).await;
return;
}
let users: Vec<_> = self
.db
.users
.iter()
.map(|(id, u)| ServerUser {
id,
username: u.username,
})
.collect();
let _ = replier.send(RequestUsersResp { users }).await;
}
}
}