From e173ef948d1dae0edea38d435b9c78c9cdb53085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Sat, 17 Feb 2018 23:38:55 +0100 Subject: [PATCH] Removed some unnecesary clones --- src/api/core/accounts.rs | 24 ++++++++++++++---------- src/api/core/ciphers.rs | 36 ++++++++++++++++++++---------------- src/api/core/folders.rs | 5 ++--- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 04a590b..7ac7468 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -28,6 +28,8 @@ struct KeysData { #[post("/accounts/register", data = "")] fn register(data: Json, conn: DbConn) -> EmptyResult { + let data: RegisterData = data.into_inner(); + if !CONFIG.signups_allowed { err!(format!("Signups not allowed")) } @@ -37,22 +39,22 @@ fn register(data: Json, conn: DbConn) -> EmptyResult { err!("Email already exists") } - let mut user = User::new(data.email.clone(), - data.key.clone(), - data.masterPasswordHash.clone()); + let mut user = User::new(data.email, + data.key, + data.masterPasswordHash); // Add extra fields if present - if let Some(name) = data.name.clone() { + if let Some(name) = data.name { user.name = name; } - if let Some(hint) = data.masterPasswordHint.clone() { + if let Some(hint) = data.masterPasswordHint { user.password_hint = Some(hint); } - if let Some(ref keys) = data.keys { - user.private_key = Some(keys.encryptedPrivateKey.clone()); - user.public_key = Some(keys.publicKey.clone()); + if let Some(keys) = data.keys { + user.private_key = Some(keys.encryptedPrivateKey); + user.public_key = Some(keys.publicKey); } user.save(&conn); @@ -67,10 +69,12 @@ fn profile(headers: Headers) -> JsonResult { #[post("/accounts/keys", data = "")] fn post_keys(data: Json, headers: Headers, conn: DbConn) -> JsonResult { + let data: KeysData = data.into_inner(); + let mut user = headers.user; - user.private_key = Some(data.encryptedPrivateKey.clone()); - user.public_key = Some(data.publicKey.clone()); + user.private_key = Some(data.encryptedPrivateKey); + user.public_key = Some(data.publicKey); user.save(&conn); diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index f00abf8..d66b444 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -92,19 +92,21 @@ struct CipherData { #[post("/ciphers", data = "")] fn post_ciphers(data: Json, headers: Headers, conn: DbConn) -> JsonResult { + let data: CipherData = data.into_inner(); + let user_uuid = headers.user.uuid.clone(); let favorite = data.favorite.unwrap_or(false); let mut cipher = Cipher::new(user_uuid, data.type_, favorite); - update_cipher_from_data(&mut cipher, &data, &headers, &conn)?; + update_cipher_from_data(&mut cipher, data, &headers, &conn)?; cipher.save(&conn); Ok(Json(cipher.to_json(&headers.host, &conn))) } -fn update_cipher_from_data(cipher: &mut Cipher, data: &CipherData, headers: &Headers, conn: &DbConn) -> EmptyResult { - if let Some(ref folder_id) = data.folderId { - match Folder::find_by_uuid(folder_id, conn) { +fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, conn: &DbConn) -> EmptyResult { + if let Some(folder_id) = data.folderId { + match Folder::find_by_uuid(&folder_id, conn) { Some(folder) => { if folder.user_uuid != headers.user.uuid { err!("Folder is not owned by user") @@ -113,12 +115,12 @@ fn update_cipher_from_data(cipher: &mut Cipher, data: &CipherData, headers: &Hea None => err!("Folder doesn't exist") } - cipher.folder_uuid = Some(folder_id.clone()); + cipher.folder_uuid = Some(folder_id); } - if let Some(ref org_id) = data.organizationId { + if let Some(org_id) = data.organizationId { // TODO: Check if user in org - cipher.organization_uuid = Some(org_id.clone()); + cipher.organization_uuid = Some(org_id); } let mut values = json!({ @@ -127,10 +129,10 @@ fn update_cipher_from_data(cipher: &mut Cipher, data: &CipherData, headers: &Hea }); let type_data_opt = match data.type_ { - 1 => data.login.clone(), - 2 => data.secureNote.clone(), - 3 => data.card.clone(), - 4 => data.identity.clone(), + 1 => data.login, + 2 => data.secureNote, + 3 => data.card, + 4 => data.identity, _ => err!("Invalid type") }; @@ -184,6 +186,7 @@ fn copy_values(from: &Value, to: &mut Value) -> bool { #[post("/ciphers/import", data = "")] fn post_ciphers_import(data: Json, headers: Headers, conn: DbConn) -> EmptyResult { + let data: Value = data.into_inner(); let folders_value = data["folders"].as_array().unwrap(); let ciphers_value = data["ciphers"].as_array().unwrap(); let relations_value = data["folderRelationships"].as_array().unwrap(); @@ -209,7 +212,7 @@ fn post_ciphers_import(data: Json, headers: Headers, conn: DbConn) -> Emp let favorite = data.favorite.unwrap_or(false); let mut cipher = Cipher::new(user_uuid, data.type_, favorite); - if update_cipher_from_data(&mut cipher, &data, &headers, &conn).is_err() { return; } + if update_cipher_from_data(&mut cipher, data, &headers, &conn).is_err() { return; } cipher.save(&conn); }); @@ -224,6 +227,8 @@ fn post_cipher(uuid: String, data: Json, headers: Headers, conn: DbC #[put("/ciphers/", data = "")] fn put_cipher(uuid: String, data: Json, headers: Headers, conn: DbConn) -> JsonResult { + let data: CipherData = data.into_inner(); + let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) { Some(cipher) => cipher, None => err!("Cipher doesn't exist") @@ -235,7 +240,7 @@ fn put_cipher(uuid: String, data: Json, headers: Headers, conn: DbCo cipher.favorite = data.favorite.unwrap_or(false); - update_cipher_from_data(&mut cipher, &data, &headers, &conn)?; + update_cipher_from_data(&mut cipher, data, &headers, &conn)?; cipher.save(&conn); Ok(Json(cipher.to_json(&headers.host, &conn))) @@ -281,9 +286,8 @@ fn post_attachment(uuid: String, data: Data, content_type: &ContentType, headers Ok(Json(cipher.to_json(&headers.host, &conn))) } -#[post("/ciphers//attachment//delete", data = "<_data>")] -fn delete_attachment_post(uuid: String, attachment_id: String, _data: Json, headers: Headers, conn: DbConn) -> EmptyResult { - // Data contains a json object with the id, but we don't need it +#[post("/ciphers//attachment//delete")] +fn delete_attachment_post(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult { delete_attachment(uuid, attachment_id, headers, conn) } diff --git a/src/api/core/folders.rs b/src/api/core/folders.rs index d079143..fb83733 100644 --- a/src/api/core/folders.rs +++ b/src/api/core/folders.rs @@ -76,9 +76,8 @@ fn put_folder(uuid: String, data: Json, headers: Headers, conn: DbConn) - Ok(Json(folder.to_json())) } -#[post("/folders//delete", data = "<_data>")] -fn delete_folder_post(uuid: String, _data: Json, headers: Headers, conn: DbConn) -> EmptyResult { - // Data contains a json object with the id, but we don't need it +#[post("/folders//delete")] +fn delete_folder_post(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { delete_folder(uuid, headers, conn) }