User::save() should return QueryResult instead of bool

pull/220/head
janost 6 years ago
parent faec050a6d
commit e985221b50

@ -84,9 +84,10 @@ fn register(data: JsonUpcase<RegisterData>, conn: DbConn) -> EmptyResult {
user.public_key = Some(keys.PublicKey);
}
user.save(&conn);
Ok(())
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to save user")
}
}
#[get("/accounts/profile")]
@ -119,9 +120,10 @@ fn post_profile(data: JsonUpcase<ProfileData>, headers: Headers, conn: DbConn) -
Some(ref h) if h.is_empty() => None,
_ => data.MasterPasswordHint,
};
user.save(&conn);
Ok(Json(user.to_json(&conn)))
match user.save(&conn) {
Ok(()) => Ok(Json(user.to_json(&conn))),
Err(_) => err!("Failed to save user profile")
}
}
#[get("/users/<uuid>/public-key")]
@ -147,11 +149,14 @@ fn post_keys(data: JsonUpcase<KeysData>, headers: Headers, conn: DbConn) -> Json
user.private_key = Some(data.EncryptedPrivateKey);
user.public_key = Some(data.PublicKey);
user.save(&conn);
Ok(Json(user.to_json(&conn)))
match user.save(&conn) {
Ok(()) => Ok(Json(user.to_json(&conn))),
Err(_) => err!("Failed to save the user's keys")
}
}
#[derive(Deserialize)]
#[allow(non_snake_case)]
struct ChangePassData {
@ -171,9 +176,10 @@ fn post_password(data: JsonUpcase<ChangePassData>, headers: Headers, conn: DbCon
user.set_password(&data.NewMasterPasswordHash);
user.key = data.Key;
user.save(&conn);
Ok(())
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to save password")
}
}
#[derive(Deserialize)]
@ -200,9 +206,10 @@ fn post_kdf(data: JsonUpcase<ChangeKdfData>, headers: Headers, conn: DbConn) ->
user.client_kdf_type = data.Kdf;
user.set_password(&data.NewMasterPasswordHash);
user.key = data.Key;
user.save(&conn);
Ok(())
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to save password settings")
}
}
#[post("/accounts/security-stamp", data = "<data>")]
@ -215,9 +222,10 @@ fn post_sstamp(data: JsonUpcase<PasswordData>, headers: Headers, conn: DbConn) -
}
user.reset_security_stamp();
user.save(&conn);
Ok(())
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to reset security stamp")
}
}
#[derive(Deserialize)]
@ -272,9 +280,10 @@ fn post_email(data: JsonUpcase<ChangeEmailData>, headers: Headers, conn: DbConn)
user.set_password(&data.NewMasterPasswordHash);
user.key = data.Key;
user.save(&conn);
Ok(())
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to save email address")
}
}
#[post("/accounts/delete", data = "<data>")]

@ -227,7 +227,9 @@ fn post_eq_domains(data: JsonUpcase<EquivDomainData>, headers: Headers, conn: Db
user.excluded_globals = to_string(&excluded_globals).unwrap_or("[]".to_string());
user.equivalent_domains = to_string(&equivalent_domains).unwrap_or("[]".to_string());
user.save(&conn);
match user.save(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed to save user")
}
Ok(())
}

@ -391,11 +391,11 @@ fn send_invite(org_id: String, data: JsonUpcase<InviteData>, headers: AdminHeade
match invitation.save(&conn) {
Ok(()) => {
let mut user = User::new(email.clone());
if user.save(&conn) {
if user.save(&conn).is_err() {
err!("Failed to create placeholder for invited user")
} else {
user_org_status = UserOrgStatus::Invited as i32;
user
} else {
err!("Failed to create placeholder for invited user")
}
}
Err(_) => err!(format!("Failed to invite: {}", email))

@ -75,9 +75,10 @@ fn recover(data: JsonUpcase<RecoverTwoFactor>, conn: DbConn) -> JsonResult {
// Remove the recovery code, not needed without twofactors
user.totp_recover = None;
user.save(&conn);
Ok(Json(json!({})))
match user.save(&conn) {
Ok(()) => Ok(Json(json!({}))),
Err(_) => err!("Failed to remove the user's two factor recovery code")
}
}
#[derive(Deserialize)]
@ -217,7 +218,9 @@ fn _generate_recover_code(user: &mut User, conn: &DbConn) {
if user.totp_recover.is_none() {
let totp_recover = BASE32.encode(&crypto::get_random(vec![0u8; 20]));
user.totp_recover = Some(totp_recover);
user.save(conn);
if user.save(conn).is_err() {
println!("Error: Failed to save the user's two factor recovery code")
}
}
}

@ -148,15 +148,11 @@ impl User {
}
pub fn save(&mut self, conn: &DbConn) -> bool {
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
self.updated_at = Utc::now().naive_utc();
match diesel::replace_into(users::table) // Insert or update
.values(&*self)
.execute(&**conn) {
Ok(1) => true, // One row inserted
_ => false,
}
diesel::replace_into(users::table) // Insert or update
.values(&*self).execute(&**conn).and(Ok(()))
}
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {

Loading…
Cancel
Save