2018-02-14 23:53:11 +00:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-10-10 18:40:39 +00:00
|
|
|
use serde_json::Value;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-04-30 09:52:15 +00:00
|
|
|
use super::{User, Cipher};
|
2018-02-14 23:40:34 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[table_name = "folders"]
|
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 Folder {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2018-04-30 09:52:15 +00:00
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "folders_ciphers"]
|
|
|
|
#[belongs_to(Cipher, foreign_key = "cipher_uuid")]
|
|
|
|
#[belongs_to(Folder, foreign_key = "folder_uuid")]
|
|
|
|
#[primary_key(cipher_uuid, folder_uuid)]
|
|
|
|
pub struct FolderCipher {
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
pub folder_uuid: String,
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
/// Local methods
|
|
|
|
impl Folder {
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn new(user_uuid: String, name: String) -> 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-12-07 13:32:40 +00:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-02-10 00:00:55 +00:00
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 18:40:39 +00:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::util::format_date;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
|
|
|
"Name": self.name,
|
|
|
|
"Object": "folder",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 09:52:15 +00:00
|
|
|
impl FolderCipher {
|
2018-05-01 15:54:22 +00:00
|
|
|
pub fn new(folder_uuid: &str, cipher_uuid: &str) -> Self {
|
2018-04-30 09:52:15 +00:00
|
|
|
Self {
|
|
|
|
folder_uuid: folder_uuid.to_string(),
|
2018-05-01 15:54:22 +00:00
|
|
|
cipher_uuid: cipher_uuid.to_string(),
|
2018-04-30 09:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::db::DbConn;
|
|
|
|
use crate::db::schema::{folders, folders_ciphers};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
/// Database methods
|
|
|
|
impl Folder {
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
2018-08-21 16:31:01 +00:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
2018-02-15 00:07:57 +00:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-10-14 12:55:00 +00:00
|
|
|
diesel::replace_into(folders::table)
|
2018-12-19 20:52:53 +00:00
|
|
|
.values(&*self).execute(&**conn)
|
|
|
|
.map_res("Error saving folder")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(&self, conn: &DbConn) -> EmptyResult {
|
2018-08-21 16:31:01 +00:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
2018-05-16 16:19:52 +00:00
|
|
|
FolderCipher::delete_all_by_folder(&self.uuid, &conn)?;
|
|
|
|
|
|
|
|
diesel::delete(
|
|
|
|
folders::table.filter(
|
2018-08-30 15:43:46 +00:00
|
|
|
folders::uuid.eq(&self.uuid)
|
2018-05-16 16:19:52 +00:00
|
|
|
)
|
2018-12-19 20:52:53 +00:00
|
|
|
).execute(&**conn)
|
|
|
|
.map_res("Error deleting folder")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-10-12 14:20:10 +00:00
|
|
|
for folder in Self::find_by_user(user_uuid, &conn) {
|
|
|
|
folder.delete(&conn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
folders::table
|
|
|
|
.filter(folders::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
|
|
|
folders::table
|
|
|
|
.filter(folders::user_uuid.eq(user_uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.load::<Self>(&**conn).expect("Error loading folders")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 09:52:15 +00:00
|
|
|
|
|
|
|
impl FolderCipher {
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn save(&self, conn: &DbConn) -> EmptyResult {
|
2018-04-30 09:52:15 +00:00
|
|
|
diesel::replace_into(folders_ciphers::table)
|
|
|
|
.values(&*self)
|
2018-12-19 20:52:53 +00:00
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error adding cipher to folder")
|
2018-04-30 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
2018-04-30 09:52:15 +00:00
|
|
|
diesel::delete(folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(self.cipher_uuid))
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(self.folder_uuid))
|
2018-12-19 20:52:53 +00:00
|
|
|
).execute(&**conn)
|
|
|
|
.map_res("Error removing cipher from folder")
|
2018-04-30 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-05-15 16:27:53 +00:00
|
|
|
diesel::delete(folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(cipher_uuid))
|
2018-12-19 20:52:53 +00:00
|
|
|
).execute(&**conn)
|
|
|
|
.map_res("Error removing cipher from folders")
|
2018-05-15 16:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_folder(folder_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-05-16 16:19:52 +00:00
|
|
|
diesel::delete(folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
2018-12-19 20:52:53 +00:00
|
|
|
).execute(&**conn)
|
|
|
|
.map_res("Error removing ciphers from folder")
|
2018-05-16 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 09:52:15 +00:00
|
|
|
pub fn find_by_folder_and_cipher(folder_uuid: &str, cipher_uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
}
|
2018-05-04 17:02:19 +00:00
|
|
|
|
|
|
|
pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading folders")
|
|
|
|
}
|
2018-04-30 09:52:15 +00:00
|
|
|
}
|