Implement addUserFallible instead of addUser

This commit is contained in:
Nadja Reitzenstein 2022-04-28 20:38:44 +02:00
parent ed10b15a10
commit 505afccbf8

View File

@ -48,29 +48,37 @@ impl manage::Server for Users {
} }
Promise::ok(()) Promise::ok(())
} }
fn add_user( fn add_user_fallible(
&mut self, &mut self,
params: manage::AddUserParams, params: manage::AddUserFallibleParams,
mut result: manage::AddUserResults, mut result: manage::AddUserFallibleResults,
) -> Promise<(), ::capnp::Error> { ) -> Promise<(), ::capnp::Error> {
let params = pry!(params.get()); let params = pry!(params.get());
let username = pry!(params.get_username()); let username = pry!(params.get_username());
let password = pry!(params.get_password()); let password = pry!(params.get_password());
// FIXME: saslprep passwords & usernames before storing them // FIXME: saslprep passwords & usernames before storing them
let mut builder = result.get();
if !username.is_empty() && !password.is_empty() { if !username.is_empty() && !password.is_empty() {
if self.session.users.get_user(username).is_none() { if self.session.users.get_user(username).is_none() {
let user = db::User::new_with_plain_pw(username, password); let user = db::User::new_with_plain_pw(username, password);
self.session.users.put_user(username, &user); self.session.users.put_user(username, &user);
let mut builder = result.get(); let mut builder = builder.init_successful();
User::fill(&self.session, user, builder); User::fill(&self.session, user, builder);
} else { } else {
let mut builder = builder.init_failed();
builder.set_error(manage::add_user_error::AddUserError::AlreadyExists);
tracing::warn!("Failed to add user: Username taken"); tracing::warn!("Failed to add user: Username taken");
} }
} else { } else {
if username.is_empty() { if username.is_empty() {
let mut builder = builder.init_failed();
builder.set_error(manage::add_user_error::AddUserError::UsernameInvalid);
tracing::warn!("Failed to add user: Username empty"); tracing::warn!("Failed to add user: Username empty");
} else if password.is_empty() { } else if password.is_empty() {
let mut builder = builder.init_failed();
builder.set_error(manage::add_user_error::AddUserError::PasswordInvalid);
tracing::warn!("Failed to add user: Password empty"); tracing::warn!("Failed to add user: Password empty");
} }
} }