From e54b52f10970b2f258224ea8dfa92267654a1e47 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Wed, 16 May 2018 17:19:52 +0100 Subject: [PATCH] Improve Folder::delete() to handle FolderCipher --- src/api/core/accounts.rs | 7 ++++++- src/api/core/ciphers.rs | 7 ++++++- src/api/core/folders.rs | 10 ++++------ src/db/models/folder.rs | 21 ++++++++++++++------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 2ffd563..eda3b68 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -176,7 +176,12 @@ fn delete_account(data: Json, headers: Headers, conn: DbConn) -> E } // Delete folders - for f in Folder::find_by_user(&user.uuid, &conn) { f.delete(&conn); } + for f in Folder::find_by_user(&user.uuid, &conn) { + match f.delete(&conn) { + Ok(()) => (), + Err(_) => err!("Failed deleting folder") + } + } // Delete devices for d in Device::find_by_user(&user.uuid, &conn) { d.delete(&conn); } diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index f3058cb..55f2383 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -557,7 +557,12 @@ fn delete_all(data: Json, headers: Headers, conn: DbConn) -> Empty } // Delete folders - for f in Folder::find_by_user(&user.uuid, &conn) { f.delete(&conn); } + for f in Folder::find_by_user(&user.uuid, &conn) { + match f.delete(&conn) { + Ok(()) => (), + Err(_) => err!("Failed deleting folder") + } + } Ok(()) } diff --git a/src/api/core/folders.rs b/src/api/core/folders.rs index 13c09ab..491119d 100644 --- a/src/api/core/folders.rs +++ b/src/api/core/folders.rs @@ -89,11 +89,9 @@ fn delete_folder(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { err!("Folder belongs to another user") } - // Delete FolderCipher mappings - for fc in FolderCipher::find_by_folder(&uuid, &conn) { fc.delete(&conn).expect("Error deleting mapping"); } - // Delete the actual folder entry - folder.delete(&conn); - - Ok(()) + match folder.delete(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed deleting folder") + } } diff --git a/src/db/models/folder.rs b/src/db/models/folder.rs index 8fcf055..d9b90bf 100644 --- a/src/db/models/folder.rs +++ b/src/db/models/folder.rs @@ -81,13 +81,14 @@ impl Folder { } } - pub fn delete(self, conn: &DbConn) -> bool { - match diesel::delete(folders::table.filter( - folders::uuid.eq(self.uuid))) - .execute(&**conn) { - Ok(1) => true, // One row deleted - _ => false, - } + pub fn delete(self, conn: &DbConn) -> QueryResult<()> { + FolderCipher::delete_all_by_folder(&self.uuid, &conn)?; + + diesel::delete( + folders::table.filter( + folders::uuid.eq(self.uuid) + ) + ).execute(&**conn).and(Ok(())) } pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option { @@ -123,6 +124,12 @@ impl FolderCipher { ).execute(&**conn).and(Ok(())) } + pub fn delete_all_by_folder(folder_uuid: &str, conn: &DbConn) -> QueryResult<()> { + diesel::delete(folders_ciphers::table + .filter(folders_ciphers::folder_uuid.eq(folder_uuid)) + ).execute(&**conn).and(Ok(())) + } + pub fn find_by_folder_and_cipher(folder_uuid: &str, cipher_uuid: &str, conn: &DbConn) -> Option { folders_ciphers::table .filter(folders_ciphers::folder_uuid.eq(folder_uuid))