Updated Cipher API with breaking changes, and included backwards compatibility

This commit is contained in:
Daniel García
2018-03-06 00:02:36 +01:00
parent e2f7f56a81
commit 1277cb099d
7 changed files with 153 additions and 80 deletions

View File

@ -18,15 +18,25 @@ pub struct Cipher {
pub folder_uuid: Option<String>,
pub organization_uuid: Option<String>,
/*
Login = 1,
SecureNote = 2,
Card = 3,
Identity = 4
*/
pub type_: i32,
pub name: String,
pub notes: Option<String>,
pub fields: Option<String>,
pub data: String,
pub favorite: bool,
}
/// Local methods
impl Cipher {
pub fn new(user_uuid: String, type_: i32, favorite: bool) -> Self {
pub fn new(user_uuid: String, type_: i32, name: String, favorite: bool) -> Self {
let now = Utc::now().naive_utc();
Self {
@ -40,6 +50,10 @@ impl Cipher {
type_,
favorite,
name,
notes: None,
fields: None,
data: String::new(),
}
@ -58,12 +72,25 @@ impl Cipher {
use util::format_date;
use super::Attachment;
let data_json: JsonValue = serde_json::from_str(&self.data).unwrap();
let attachments = Attachment::find_by_cipher(&self.uuid, conn);
let attachments_json: Vec<JsonValue> = attachments.iter().map(|c| c.to_json(host)).collect();
json!({
let fields_json: JsonValue = if let Some(ref fields) = self.fields {
serde_json::from_str(fields).unwrap()
} else { JsonValue::Null };
let mut data_json: JsonValue = serde_json::from_str(&self.data).unwrap();
// TODO: ******* Backwards compat start **********
// To remove backwards compatibility, just remove this entire section
// and remove the compat code from ciphers::update_cipher_from_data
if self.type_ == 1 && data_json["Uris"].is_array() {
let uri = data_json["Uris"][0]["uri"].clone();
data_json["Uri"] = uri;
}
// TODO: ******* Backwards compat end **********
let mut json_object = json!({
"Id": self.uuid,
"Type": self.type_,
"RevisionDate": format_date(&self.updated_at),
@ -72,10 +99,27 @@ impl Cipher {
"OrganizationId": "",
"Attachments": attachments_json,
"OrganizationUseTotp": false,
"Name": self.name,
"Notes": self.notes,
"Fields": fields_json,
"Data": data_json,
"Object": "cipher",
"Edit": true,
})
});
let key = match self.type_ {
1 => "Login",
2 => "SecureNote",
3 => "Card",
4 => "Identity",
_ => panic!("Wrong type"),
};
json_object[key] = data_json;
json_object
}
pub fn save(&mut self, conn: &DbConn) -> bool {