Refactor reset_prepare() to reduce indents

This commit is contained in:
finga 2020-08-28 22:27:54 +02:00
parent 5b3e56954e
commit b155c17337

View file

@ -58,7 +58,10 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
ldap.unbind()?;
let (rs, _res) = result.success()?;
if rs.len() == 1 {
if rs.len() != 1 {
bail!("Invalid password reset request for '{}'", email_address);
}
// generate key
let mut rng = rand::thread_rng();
let key: String = (0..64)
@ -67,7 +70,9 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
// store key with id
let keys = Arc::clone(&keys.keys);
if let Ok(mut keys) = keys.lock() {
let mut keys = keys
.lock()
.map_err(|e| anyhow!("Could not aquire lock for keys: {}", e))?;
keys.insert(key.to_string(), email_address.to_string());
debug!("Generated new key for '{}'", email_address);
@ -84,22 +89,8 @@ fn reset_prepare(config: &Ldap0rConfig, keys: &Keys, email_address: &str) -> Res
// send email
let mut mailer = SmtpClient::new_unencrypted_localhost()?.transport();
let result = mailer.send(email.into());
if result.is_ok() {
mailer.send(email.into())?;
info!("Password reset email was sent to '{}'", email_address);
} else {
bail!(
"Sending password reset email with reset URL to '{}' failed",
email_address
);
}
} else {
bail!("Could not aquire lock for keys");
};
} else {
bail!("Invalid password reset request for '{}'", email_address);
}
Ok(())
}