From 1d4f900e488ad9f38ef5e7639c882d57a0be2dc0 Mon Sep 17 00:00:00 2001 From: Jeremy Lin Date: Sun, 24 Jan 2021 21:57:32 -0800 Subject: [PATCH] Add cipher response to restore operations This matches changes in the upstream Bitwarden server and clients. Upstream PR: https://github.com/bitwarden/server/pull/1072 --- src/api/core/ciphers.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 1587bf79..077689b6 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -967,18 +967,18 @@ fn delete_cipher_selected_put_admin(data: JsonUpcase, headers: Headers, c } #[put("/ciphers//restore")] -fn restore_cipher_put(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult { +fn restore_cipher_put(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult { _restore_cipher_by_uuid(&uuid, &headers, &conn, &nt) } #[put("/ciphers//restore-admin")] -fn restore_cipher_put_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult { +fn restore_cipher_put_admin(uuid: String, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult { _restore_cipher_by_uuid(&uuid, &headers, &conn, &nt) } #[put("/ciphers/restore", data = "")] -fn restore_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult { - _restore_multiple_ciphers(data, headers, conn, nt) +fn restore_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn, nt: Notify) -> JsonResult { + _restore_multiple_ciphers(data, &headers, &conn, &nt) } #[derive(Deserialize)] @@ -1134,7 +1134,7 @@ fn _delete_multiple_ciphers(data: JsonUpcase, headers: Headers, conn: DbC Ok(()) } -fn _restore_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, nt: &Notify) -> EmptyResult { +fn _restore_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, nt: &Notify) -> JsonResult { let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) { Some(cipher) => cipher, None => err!("Cipher doesn't exist"), @@ -1148,10 +1148,10 @@ fn _restore_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, nt: &No cipher.save(&conn)?; nt.send_cipher_update(UpdateType::CipherUpdate, &cipher, &cipher.update_users_revision(&conn)); - Ok(()) + Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } -fn _restore_multiple_ciphers(data: JsonUpcase, headers: Headers, conn: DbConn, nt: Notify) -> EmptyResult { +fn _restore_multiple_ciphers(data: JsonUpcase, headers: &Headers, conn: &DbConn, nt: &Notify) -> JsonResult { let data: Value = data.into_inner().data; let uuids = match data.get("Ids") { @@ -1162,13 +1162,19 @@ fn _restore_multiple_ciphers(data: JsonUpcase, headers: Headers, conn: Db None => err!("Request missing ids field"), }; + let mut ciphers: Vec = Vec::new(); for uuid in uuids { - if let error @ Err(_) = _restore_cipher_by_uuid(uuid, &headers, &conn, &nt) { - return error; - }; + match _restore_cipher_by_uuid(uuid, headers, conn, nt) { + Ok(json) => ciphers.push(json.into_inner()), + err => return err + } } - Ok(()) + Ok(Json(json!({ + "Data": ciphers, + "Object": "list", + "ContinuationToken": null + }))) } fn _delete_cipher_attachment_by_id(