2018-02-15 00:53:11 +01:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
use super::User;
|
2018-02-15 00:40:34 +01:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_object! {
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = folders)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct Folder {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = folders_ciphers)]
|
|
|
|
#[diesel(primary_key(cipher_uuid, folder_uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct FolderCipher {
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
pub folder_uuid: String,
|
|
|
|
}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
/// Local methods
|
|
|
|
impl Folder {
|
2018-02-15 00:40:34 +01:00
|
|
|
pub fn new(user_uuid: String, name: String) -> Self {
|
2018-02-10 01:00:55 +01:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
Self {
|
2018-12-07 14:32:40 +01:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-02-10 01:00:55 +01:00
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::util::format_date;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"RevisionDate": format_date(&self.updated_at),
|
|
|
|
"Name": self.name,
|
|
|
|
"Object": "folder",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:52:15 +02:00
|
|
|
impl FolderCipher {
|
2018-05-01 17:54:22 +02:00
|
|
|
pub fn new(folder_uuid: &str, cipher_uuid: &str) -> Self {
|
2018-04-30 11:52:15 +02:00
|
|
|
Self {
|
|
|
|
folder_uuid: folder_uuid.to_string(),
|
2018-05-01 17:54:22 +02:00
|
|
|
cipher_uuid: cipher_uuid.to_string(),
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use crate::db::DbConn;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
/// Database methods
|
|
|
|
impl Folder {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&mut self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await;
|
2018-02-15 01:07:57 +01:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
|
|
|
sqlite, mysql {
|
|
|
|
match diesel::replace_into(folders::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(FolderDb::to_db(self))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
|
|
|
|
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
|
|
|
|
diesel::update(folders::table)
|
|
|
|
.filter(folders::uuid.eq(&self.uuid))
|
|
|
|
.set(FolderDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving folder")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving folder")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = FolderDb::to_db(self);
|
|
|
|
diesel::insert_into(folders::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(folders::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving folder")
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(&self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await;
|
|
|
|
FolderCipher::delete_all_by_folder(&self.uuid, conn).await?;
|
2018-05-16 18:19:52 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(folders::table.filter(folders::uuid.eq(&self.uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting folder")
|
|
|
|
}}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
for folder in Self::find_by_user(user_uuid, conn).await {
|
|
|
|
folder.delete(conn).await?;
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
folders::table
|
|
|
|
.filter(folders::uuid.eq(uuid))
|
|
|
|
.first::<FolderDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
folders::table
|
|
|
|
.filter(folders::user_uuid.eq(user_uuid))
|
|
|
|
.load::<FolderDb>(conn)
|
|
|
|
.expect("Error loading folders")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 11:52:15 +02:00
|
|
|
|
|
|
|
impl FolderCipher {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&self, conn: &mut DbConn) -> EmptyResult {
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
// Not checking for ForeignKey Constraints here.
|
|
|
|
// Table folders_ciphers does not have ForeignKey Constraints which would cause conflicts.
|
|
|
|
// This table has no constraints pointing to itself, but only to others.
|
2020-08-18 17:15:44 +02:00
|
|
|
diesel::replace_into(folders_ciphers::table)
|
|
|
|
.values(FolderCipherDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding cipher to folder")
|
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
diesel::insert_into(folders_ciphers::table)
|
|
|
|
.values(FolderCipherDb::to_db(self))
|
|
|
|
.on_conflict((folders_ciphers::cipher_uuid, folders_ciphers::folder_uuid))
|
|
|
|
.do_nothing()
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
.map_res("Error adding cipher to folder")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
2020-09-22 12:13:02 +02:00
|
|
|
}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(self, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(
|
|
|
|
folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(self.cipher_uuid))
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(self.folder_uuid)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing cipher from folder")
|
|
|
|
}}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_cipher(cipher_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(folders_ciphers::table.filter(folders_ciphers::cipher_uuid.eq(cipher_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing cipher from folders")
|
|
|
|
}}
|
2018-05-15 18:27:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_folder(folder_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(folders_ciphers::table.filter(folders_ciphers::folder_uuid.eq(folder_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing ciphers from folder")
|
|
|
|
}}
|
2018-05-16 18:19:52 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_folder_and_cipher(folder_uuid: &str, cipher_uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.filter(folders_ciphers::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.first::<FolderCipherDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|
2018-05-04 19:02:19 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_folder(folder_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
folders_ciphers::table
|
|
|
|
.filter(folders_ciphers::folder_uuid.eq(folder_uuid))
|
|
|
|
.load::<FolderCipherDb>(conn)
|
|
|
|
.expect("Error loading folders")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-05-04 19:02:19 +02:00
|
|
|
}
|
2022-05-04 21:13:05 +02:00
|
|
|
|
|
|
|
/// Return a vec with (cipher_uuid, folder_uuid)
|
|
|
|
/// This is used during a full sync so we only need one query for all folder matches.
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<(String, String)> {
|
2022-05-04 21:13:05 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
folders_ciphers::table
|
|
|
|
.inner_join(folders::table)
|
|
|
|
.filter(folders::user_uuid.eq(user_uuid))
|
|
|
|
.select(folders_ciphers::all_columns)
|
|
|
|
.load::<(String, String)>(conn)
|
|
|
|
.unwrap_or_default()
|
|
|
|
}}
|
|
|
|
}
|
2018-04-30 11:52:15 +02:00
|
|
|
}
|