Removed some unnecesary clones

This commit is contained in:
Daniel García 2018-02-17 23:38:55 +01:00
parent 4c9d82d790
commit e173ef948d
3 changed files with 36 additions and 29 deletions

View File

@ -28,6 +28,8 @@ struct KeysData {
#[post("/accounts/register", data = "<data>")] #[post("/accounts/register", data = "<data>")]
fn register(data: Json<RegisterData>, conn: DbConn) -> EmptyResult { fn register(data: Json<RegisterData>, conn: DbConn) -> EmptyResult {
let data: RegisterData = data.into_inner();
if !CONFIG.signups_allowed { if !CONFIG.signups_allowed {
err!(format!("Signups not allowed")) err!(format!("Signups not allowed"))
} }
@ -37,22 +39,22 @@ fn register(data: Json<RegisterData>, conn: DbConn) -> EmptyResult {
err!("Email already exists") err!("Email already exists")
} }
let mut user = User::new(data.email.clone(), let mut user = User::new(data.email,
data.key.clone(), data.key,
data.masterPasswordHash.clone()); data.masterPasswordHash);
// Add extra fields if present // Add extra fields if present
if let Some(name) = data.name.clone() { if let Some(name) = data.name {
user.name = name; user.name = name;
} }
if let Some(hint) = data.masterPasswordHint.clone() { if let Some(hint) = data.masterPasswordHint {
user.password_hint = Some(hint); user.password_hint = Some(hint);
} }
if let Some(ref keys) = data.keys { if let Some(keys) = data.keys {
user.private_key = Some(keys.encryptedPrivateKey.clone()); user.private_key = Some(keys.encryptedPrivateKey);
user.public_key = Some(keys.publicKey.clone()); user.public_key = Some(keys.publicKey);
} }
user.save(&conn); user.save(&conn);
@ -67,10 +69,12 @@ fn profile(headers: Headers) -> JsonResult {
#[post("/accounts/keys", data = "<data>")] #[post("/accounts/keys", data = "<data>")]
fn post_keys(data: Json<KeysData>, headers: Headers, conn: DbConn) -> JsonResult { fn post_keys(data: Json<KeysData>, headers: Headers, conn: DbConn) -> JsonResult {
let data: KeysData = data.into_inner();
let mut user = headers.user; let mut user = headers.user;
user.private_key = Some(data.encryptedPrivateKey.clone()); user.private_key = Some(data.encryptedPrivateKey);
user.public_key = Some(data.publicKey.clone()); user.public_key = Some(data.publicKey);
user.save(&conn); user.save(&conn);

View File

@ -92,19 +92,21 @@ struct CipherData {
#[post("/ciphers", data = "<data>")] #[post("/ciphers", data = "<data>")]
fn post_ciphers(data: Json<CipherData>, headers: Headers, conn: DbConn) -> JsonResult { fn post_ciphers(data: Json<CipherData>, headers: Headers, conn: DbConn) -> JsonResult {
let data: CipherData = data.into_inner();
let user_uuid = headers.user.uuid.clone(); let user_uuid = headers.user.uuid.clone();
let favorite = data.favorite.unwrap_or(false); let favorite = data.favorite.unwrap_or(false);
let mut cipher = Cipher::new(user_uuid, data.type_, favorite); 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); cipher.save(&conn);
Ok(Json(cipher.to_json(&headers.host, &conn))) Ok(Json(cipher.to_json(&headers.host, &conn)))
} }
fn update_cipher_from_data(cipher: &mut Cipher, data: &CipherData, headers: &Headers, conn: &DbConn) -> EmptyResult { fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, conn: &DbConn) -> EmptyResult {
if let Some(ref folder_id) = data.folderId { if let Some(folder_id) = data.folderId {
match Folder::find_by_uuid(folder_id, conn) { match Folder::find_by_uuid(&folder_id, conn) {
Some(folder) => { Some(folder) => {
if folder.user_uuid != headers.user.uuid { if folder.user_uuid != headers.user.uuid {
err!("Folder is not owned by user") 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") 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 // TODO: Check if user in org
cipher.organization_uuid = Some(org_id.clone()); cipher.organization_uuid = Some(org_id);
} }
let mut values = json!({ 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_ { let type_data_opt = match data.type_ {
1 => data.login.clone(), 1 => data.login,
2 => data.secureNote.clone(), 2 => data.secureNote,
3 => data.card.clone(), 3 => data.card,
4 => data.identity.clone(), 4 => data.identity,
_ => err!("Invalid type") _ => err!("Invalid type")
}; };
@ -184,6 +186,7 @@ fn copy_values(from: &Value, to: &mut Value) -> bool {
#[post("/ciphers/import", data = "<data>")] #[post("/ciphers/import", data = "<data>")]
fn post_ciphers_import(data: Json<Value>, headers: Headers, conn: DbConn) -> EmptyResult { fn post_ciphers_import(data: Json<Value>, headers: Headers, conn: DbConn) -> EmptyResult {
let data: Value = data.into_inner();
let folders_value = data["folders"].as_array().unwrap(); let folders_value = data["folders"].as_array().unwrap();
let ciphers_value = data["ciphers"].as_array().unwrap(); let ciphers_value = data["ciphers"].as_array().unwrap();
let relations_value = data["folderRelationships"].as_array().unwrap(); let relations_value = data["folderRelationships"].as_array().unwrap();
@ -209,7 +212,7 @@ fn post_ciphers_import(data: Json<Value>, headers: Headers, conn: DbConn) -> Emp
let favorite = data.favorite.unwrap_or(false); let favorite = data.favorite.unwrap_or(false);
let mut cipher = Cipher::new(user_uuid, data.type_, favorite); 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); cipher.save(&conn);
}); });
@ -224,6 +227,8 @@ fn post_cipher(uuid: String, data: Json<CipherData>, headers: Headers, conn: DbC
#[put("/ciphers/<uuid>", data = "<data>")] #[put("/ciphers/<uuid>", data = "<data>")]
fn put_cipher(uuid: String, data: Json<CipherData>, headers: Headers, conn: DbConn) -> JsonResult { fn put_cipher(uuid: String, data: Json<CipherData>, headers: Headers, conn: DbConn) -> JsonResult {
let data: CipherData = data.into_inner();
let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) { let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) {
Some(cipher) => cipher, Some(cipher) => cipher,
None => err!("Cipher doesn't exist") None => err!("Cipher doesn't exist")
@ -235,7 +240,7 @@ fn put_cipher(uuid: String, data: Json<CipherData>, headers: Headers, conn: DbCo
cipher.favorite = data.favorite.unwrap_or(false); 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); cipher.save(&conn);
Ok(Json(cipher.to_json(&headers.host, &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))) Ok(Json(cipher.to_json(&headers.host, &conn)))
} }
#[post("/ciphers/<uuid>/attachment/<attachment_id>/delete", data = "<_data>")] #[post("/ciphers/<uuid>/attachment/<attachment_id>/delete")]
fn delete_attachment_post(uuid: String, attachment_id: String, _data: Json<Value>, headers: Headers, conn: DbConn) -> EmptyResult { fn delete_attachment_post(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult {
// Data contains a json object with the id, but we don't need it
delete_attachment(uuid, attachment_id, headers, conn) delete_attachment(uuid, attachment_id, headers, conn)
} }

View File

@ -76,9 +76,8 @@ fn put_folder(uuid: String, data: Json<Value>, headers: Headers, conn: DbConn) -
Ok(Json(folder.to_json())) Ok(Json(folder.to_json()))
} }
#[post("/folders/<uuid>/delete", data = "<_data>")] #[post("/folders/<uuid>/delete")]
fn delete_folder_post(uuid: String, _data: Json<Value>, headers: Headers, conn: DbConn) -> EmptyResult { fn delete_folder_post(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult {
// Data contains a json object with the id, but we don't need it
delete_folder(uuid, headers, conn) delete_folder(uuid, headers, conn)
} }