2018-02-14 23:53:11 +00:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-02-10 00:00:55 +00:00
|
|
|
use serde_json::Value as JsonValue;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
use super::User;
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[table_name = "ciphers"]
|
2018-02-14 23:40:34 +00:00
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Cipher {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub folder_uuid: Option<String>,
|
|
|
|
pub organization_uuid: Option<String>,
|
|
|
|
|
|
|
|
// Login = 1,
|
|
|
|
// SecureNote = 2,
|
|
|
|
// Card = 3,
|
|
|
|
// Identity = 4
|
|
|
|
pub type_: i32,
|
|
|
|
|
|
|
|
pub data: String,
|
|
|
|
pub favorite: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Cipher {
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn new(user_uuid: String, type_: i32, favorite: bool) -> Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
uuid: Uuid::new_v4().to_string(),
|
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
folder_uuid: None,
|
|
|
|
organization_uuid: None,
|
|
|
|
|
|
|
|
type_,
|
|
|
|
favorite,
|
|
|
|
|
|
|
|
data: String::new(),
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:40:34 +00:00
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use db::DbConn;
|
|
|
|
use db::schema::ciphers;
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Cipher {
|
|
|
|
pub fn to_json(&self, conn: &DbConn) -> JsonValue {
|
2018-02-10 00:00:55 +00:00
|
|
|
use serde_json;
|
|
|
|
use util::format_date;
|
2018-02-14 23:40:34 +00:00
|
|
|
use super::Attachment;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
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()).collect();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"Type": self.type_,
|
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
|
|
|
"FolderId": self.folder_uuid,
|
|
|
|
"Favorite": self.favorite,
|
|
|
|
"OrganizationId": "",
|
2018-02-14 23:40:34 +00:00
|
|
|
"Attachments": attachments_json,
|
2018-02-10 00:00:55 +00:00
|
|
|
"OrganizationUseTotp": false,
|
2018-02-14 23:40:34 +00:00
|
|
|
"Data": data_json,
|
2018-02-10 00:00:55 +00:00
|
|
|
"Object": "cipher",
|
|
|
|
"Edit": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self, conn: &DbConn) -> bool {
|
|
|
|
// TODO: Update modified date
|
|
|
|
|
|
|
|
match diesel::replace_into(ciphers::table)
|
|
|
|
.values(self)
|
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row inserted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(self, conn: &DbConn) -> bool {
|
|
|
|
match diesel::delete(ciphers::table.filter(
|
|
|
|
ciphers::uuid.eq(self.uuid)))
|
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row deleted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::uuid.eq(uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.first::<Self>(&**conn).ok()
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
ciphers::table
|
|
|
|
.filter(ciphers::user_uuid.eq(user_uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.load::<Self>(&**conn).expect("Error loading ciphers")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
}
|