From b6502e9e9df14d9a3719d4fae15395f83cef3e17 Mon Sep 17 00:00:00 2001 From: "Shane A. Faulkner" Date: Fri, 31 Aug 2018 23:30:53 -0500 Subject: [PATCH 1/2] Add support for CipherUpdate notifications --- src/api/core/ciphers.rs | 58 +++++++++++++++++++++------------------- src/api/notifications.rs | 6 ++--- src/db/models/cipher.rs | 17 ++++++++++++ 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index f9e090cc..86332efe 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -1,6 +1,7 @@ use std::path::Path; use std::collections::HashSet; +use rocket::State; use rocket::Data; use rocket::http::ContentType; @@ -16,7 +17,7 @@ use db::models::*; use crypto; -use api::{self, PasswordData, JsonResult, EmptyResult, JsonUpcase}; +use api::{self, PasswordData, JsonResult, EmptyResult, JsonUpcase, WebSocketUsers, UpdateType}; use auth::Headers; use CONFIG; @@ -117,22 +118,22 @@ struct CipherData { } #[post("/ciphers/admin", data = "")] -fn post_ciphers_admin(data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { +fn post_ciphers_admin(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { // TODO: Implement this correctly - post_ciphers(data, headers, conn) + post_ciphers(data, headers, conn, ws) } #[post("/ciphers", data = "")] -fn post_ciphers(data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { +fn post_ciphers(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { let data: CipherData = data.into_inner().data; let mut cipher = Cipher::new(data.Type, data.Name.clone()); - update_cipher_from_data(&mut cipher, data, &headers, false, &conn)?; + update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } -fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, shared_to_collection: bool, conn: &DbConn) -> EmptyResult { +fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, shared_to_collection: bool, conn: &DbConn, ws: &State) -> EmptyResult { if let Some(org_id) = data.OrganizationId { match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) { None => err!("You don't have permission to add item to organization"), @@ -190,6 +191,7 @@ fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Head cipher.password_history = data.PasswordHistory.map(|f| f.to_string()); cipher.save(&conn); + ws.send_cipher_update(UpdateType::SyncCipherUpdate, &cipher, &cipher.get_users(&conn)); if cipher.move_to_folder(data.FolderId, &headers.user.uuid, &conn).is_err() { err!("Error saving the folder information") @@ -219,7 +221,7 @@ struct RelationsData { #[post("/ciphers/import", data = "")] -fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { +fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { let data: ImportData = data.into_inner().data; // Read and create the folders @@ -243,7 +245,7 @@ fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbC .map(|i| folders[*i].uuid.clone()); let mut cipher = Cipher::new(cipher_data.Type, cipher_data.Name.clone()); - update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn)?; + update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn, &ws)?; cipher.move_to_folder(folder_uuid, &headers.user.uuid.clone(), &conn).ok(); } @@ -257,22 +259,22 @@ fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbC #[put("/ciphers//admin", data = "")] -fn put_cipher_admin(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { - put_cipher(uuid, data, headers, conn) +fn put_cipher_admin(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { + put_cipher(uuid, data, headers, conn, ws) } #[post("/ciphers//admin", data = "")] -fn post_cipher_admin(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { - post_cipher(uuid, data, headers, conn) +fn post_cipher_admin(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { + post_cipher(uuid, data, headers, conn, ws) } #[post("/ciphers/", data = "")] -fn post_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { - put_cipher(uuid, data, headers, conn) +fn post_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { + put_cipher(uuid, data, headers, conn, ws) } #[put("/ciphers/", data = "")] -fn put_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { +fn put_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { let data: CipherData = data.into_inner().data; let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) { @@ -284,7 +286,7 @@ fn put_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn err!("Cipher is not write accessible") } - update_cipher_from_data(&mut cipher, data, &headers, false, &conn)?; + update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } @@ -349,17 +351,17 @@ struct ShareCipherData { } #[post("/ciphers//share", data = "")] -fn post_cipher_share(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { +fn post_cipher_share(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { let data: ShareCipherData = data.into_inner().data; - share_cipher_by_uuid(&uuid, data, &headers, &conn) + share_cipher_by_uuid(&uuid, data, &headers, &conn, &ws) } #[put("/ciphers//share", data = "")] -fn put_cipher_share(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn) -> JsonResult { +fn put_cipher_share(uuid: String, data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> JsonResult { let data: ShareCipherData = data.into_inner().data; - share_cipher_by_uuid(&uuid, data, &headers, &conn) + share_cipher_by_uuid(&uuid, data, &headers, &conn, &ws) } #[derive(Deserialize)] @@ -370,7 +372,7 @@ struct ShareSelectedCipherData { } #[put("/ciphers/share", data = "")] -fn put_cipher_share_seleted(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { +fn put_cipher_share_seleted(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { let mut data: ShareSelectedCipherData = data.into_inner().data; let mut cipher_ids: Vec = Vec::new(); @@ -402,15 +404,16 @@ fn put_cipher_share_seleted(data: JsonUpcase, headers: }; match shared_cipher_data.Cipher.Id.take() { - Some(id) => share_cipher_by_uuid(&id, shared_cipher_data , &headers, &conn)?, + Some(id) => share_cipher_by_uuid(&id, shared_cipher_data , &headers, &conn, &ws)?, None => err!("Request missing ids field") + }; } Ok(()) } -fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, conn: &DbConn) -> JsonResult { +fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, conn: &DbConn, ws: &State) -> JsonResult { let mut cipher = match Cipher::find_by_uuid(&uuid, &conn) { Some(cipher) => { if cipher.is_write_accessible_to_user(&headers.user.uuid, &conn) { @@ -443,7 +446,7 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co } } } - update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_to_collection, &conn)?; + update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_to_collection, &conn, &ws)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } @@ -581,7 +584,7 @@ fn delete_cipher_selected_post(data: JsonUpcase, headers: Headers, conn: } #[post("/ciphers/move", data = "")] -fn move_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { +fn move_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { let data = data.into_inner().data; let folder_id = match data.get("FolderId") { @@ -627,14 +630,15 @@ fn move_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn) err!("Error saving the folder information") } cipher.save(&conn); + ws.send_cipher_update(UpdateType::SyncCipherUpdate, &cipher, &cipher.get_users(&conn)); } Ok(()) } #[put("/ciphers/move", data = "")] -fn move_cipher_selected_put(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { - move_cipher_selected(data, headers, conn) +fn move_cipher_selected_put(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + move_cipher_selected(data, headers, conn, ws) } #[post("/ciphers/purge", data = "")] diff --git a/src/api/notifications.rs b/src/api/notifications.rs index ae9721b8..299d64c1 100644 --- a/src/api/notifications.rs +++ b/src/api/notifications.rs @@ -268,7 +268,7 @@ impl WebSocketUsers { self.send_update(&folder.user_uuid, data).ok(); } - pub fn send_cipher_update(&self, ut: UpdateType, cipher: &Cipher, user_uuids: &[&String]) { + pub fn send_cipher_update(&self, ut: UpdateType, cipher: &Cipher, user_uuids: &Vec) { let user_uuid = convert_option(cipher.user_uuid.clone()); let org_uuid = convert_option(cipher.organization_uuid.clone()); @@ -283,8 +283,8 @@ impl WebSocketUsers { ut, ); - for user_uuid in user_uuids { - self.send_update(user_uuid, data.clone()).ok(); + for uuid in user_uuids { + self.send_update(&uuid, data.clone()).ok(); } } } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 917a76ea..43c89498 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -358,4 +358,21 @@ impl Cipher { .select(ciphers_collections::collection_uuid) .load::(&**conn).unwrap_or(vec![]) } + + pub fn get_users(&self, conn: &DbConn) -> Vec { + let mut user_uuids = Vec::new(); + match self.user_uuid { + Some(ref user_uuid) => user_uuids.push(user_uuid.clone()), + None => { // Belongs to Organization, need to update affected users + if let Some(ref org_uuid) = self.organization_uuid { + UserOrganization::find_by_cipher_and_org(&self.uuid, &org_uuid, conn) + .iter() + .for_each(|user_org| { + user_uuids.push(user_org.user_uuid.clone()) + }); + } + } + }; + user_uuids + } } From d8e5e532737b5245254b28e31bb61cc9f7e8562e Mon Sep 17 00:00:00 2001 From: "Shane A. Faulkner" Date: Sat, 1 Sep 2018 10:59:13 -0500 Subject: [PATCH 2/2] Add notifications for cipher delete and create --- src/api/core/ciphers.rs | 84 +++++++++++++++++++++++------------------ src/db/models/cipher.rs | 33 ++++++---------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 86332efe..e18405f2 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -128,12 +128,12 @@ fn post_ciphers(data: JsonUpcase, headers: Headers, conn: DbConn, ws let data: CipherData = data.into_inner().data; let mut cipher = Cipher::new(data.Type, data.Name.clone()); - update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws)?; + update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws, UpdateType::SyncCipherCreate)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } -fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, shared_to_collection: bool, conn: &DbConn, ws: &State) -> EmptyResult { +fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, shared_to_collection: bool, conn: &DbConn, ws: &State, ut: UpdateType) -> EmptyResult { if let Some(org_id) = data.OrganizationId { match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) { None => err!("You don't have permission to add item to organization"), @@ -191,7 +191,7 @@ fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Head cipher.password_history = data.PasswordHistory.map(|f| f.to_string()); cipher.save(&conn); - ws.send_cipher_update(UpdateType::SyncCipherUpdate, &cipher, &cipher.get_users(&conn)); + ws.send_cipher_update(ut, &cipher, &cipher.update_users_revision(&conn)); if cipher.move_to_folder(data.FolderId, &headers.user.uuid, &conn).is_err() { err!("Error saving the folder information") @@ -245,7 +245,7 @@ fn post_ciphers_import(data: JsonUpcase, headers: Headers, conn: DbC .map(|i| folders[*i].uuid.clone()); let mut cipher = Cipher::new(cipher_data.Type, cipher_data.Name.clone()); - update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn, &ws)?; + update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn, &ws, UpdateType::SyncCipherCreate)?; cipher.move_to_folder(folder_uuid, &headers.user.uuid.clone(), &conn).ok(); } @@ -286,7 +286,7 @@ fn put_cipher(uuid: String, data: JsonUpcase, headers: Headers, conn err!("Cipher is not write accessible") } - update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws)?; + update_cipher_from_data(&mut cipher, data, &headers, false, &conn, &ws, UpdateType::SyncCipherUpdate)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } @@ -446,7 +446,7 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co } } } - update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_to_collection, &conn, &ws)?; + update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_to_collection, &conn, &ws, UpdateType::SyncCipherUpdate)?; Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn))) } @@ -512,53 +512,53 @@ fn post_attachment_admin(uuid: String, data: Data, content_type: &ContentType, h } #[post("/ciphers//attachment//share", format = "multipart/form-data", data = "")] -fn post_attachment_share(uuid: String, attachment_id: String, data: Data, content_type: &ContentType, headers: Headers, conn: DbConn) -> JsonResult { - _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn)?; +fn post_attachment_share(uuid: String, attachment_id: String, data: Data, content_type: &ContentType, headers: Headers, conn: DbConn, ws: State) -> JsonResult { + _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &ws)?; post_attachment(uuid, data, content_type, headers, conn) } #[post("/ciphers//attachment//delete-admin")] -fn delete_attachment_post_admin(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult { - delete_attachment(uuid, attachment_id, headers, conn) +fn delete_attachment_post_admin(uuid: String, attachment_id: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + delete_attachment(uuid, attachment_id, headers, conn, ws) } #[post("/ciphers//attachment//delete")] -fn delete_attachment_post(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult { - delete_attachment(uuid, attachment_id, headers, conn) +fn delete_attachment_post(uuid: String, attachment_id: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + delete_attachment(uuid, attachment_id, headers, conn, ws) } #[delete("/ciphers//attachment/")] -fn delete_attachment(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn) +fn delete_attachment(uuid: String, attachment_id: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &ws) } #[delete("/ciphers//attachment//admin")] -fn delete_attachment_admin(uuid: String, attachment_id: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn) +fn delete_attachment_admin(uuid: String, attachment_id: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_attachment_by_id(&uuid, &attachment_id, &headers, &conn, &ws) } #[post("/ciphers//delete")] -fn delete_cipher_post(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_by_uuid(&uuid, &headers, &conn) +fn delete_cipher_post(uuid: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_by_uuid(&uuid, &headers, &conn, &ws) } #[post("/ciphers//delete-admin")] -fn delete_cipher_post_admin(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_by_uuid(&uuid, &headers, &conn) +fn delete_cipher_post_admin(uuid: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_by_uuid(&uuid, &headers, &conn, &ws) } #[delete("/ciphers/")] -fn delete_cipher(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_by_uuid(&uuid, &headers, &conn) +fn delete_cipher(uuid: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_by_uuid(&uuid, &headers, &conn, &ws) } #[delete("/ciphers//admin")] -fn delete_cipher_admin(uuid: String, headers: Headers, conn: DbConn) -> EmptyResult { - _delete_cipher_by_uuid(&uuid, &headers, &conn) +fn delete_cipher_admin(uuid: String, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + _delete_cipher_by_uuid(&uuid, &headers, &conn, &ws) } #[delete("/ciphers", data = "")] -fn delete_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { +fn delete_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { let data: Value = data.into_inner().data; let uuids = match data.get("Ids") { @@ -570,7 +570,7 @@ fn delete_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbCon }; for uuid in uuids { - if let error @ Err(_) = _delete_cipher_by_uuid(uuid, &headers, &conn) { + if let error @ Err(_) = _delete_cipher_by_uuid(uuid, &headers, &conn, &ws) { return error; }; } @@ -579,8 +579,8 @@ fn delete_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbCon } #[post("/ciphers/delete", data = "")] -fn delete_cipher_selected_post(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { - delete_cipher_selected(data, headers, conn) +fn delete_cipher_selected_post(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { + delete_cipher_selected(data, headers, conn, ws) } #[post("/ciphers/move", data = "")] @@ -630,7 +630,7 @@ fn move_cipher_selected(data: JsonUpcase, headers: Headers, conn: DbConn, err!("Error saving the folder information") } cipher.save(&conn); - ws.send_cipher_update(UpdateType::SyncCipherUpdate, &cipher, &cipher.get_users(&conn)); + ws.send_cipher_update(UpdateType::SyncCipherUpdate, &cipher, &cipher.update_users_revision(&conn)); } Ok(()) @@ -642,7 +642,7 @@ fn move_cipher_selected_put(data: JsonUpcase, headers: Headers, conn: DbC } #[post("/ciphers/purge", data = "")] -fn delete_all(data: JsonUpcase, headers: Headers, conn: DbConn) -> EmptyResult { +fn delete_all(data: JsonUpcase, headers: Headers, conn: DbConn, ws: State) -> EmptyResult { let data: PasswordData = data.into_inner().data; let password_hash = data.MasterPasswordHash; @@ -657,6 +657,9 @@ fn delete_all(data: JsonUpcase, headers: Headers, conn: DbConn) -> if cipher.delete(&conn).is_err() { err!("Failed deleting cipher") } + else { + ws.send_cipher_update(UpdateType::SyncCipherDelete, &cipher, &cipher.update_users_revision(&conn)); + } } // Delete folders @@ -664,13 +667,16 @@ fn delete_all(data: JsonUpcase, headers: Headers, conn: DbConn) -> if f.delete(&conn).is_err() { err!("Failed deleting folder") } + else { + ws.send_folder_update(UpdateType::SyncFolderCreate, &f); + } } Ok(()) } -fn _delete_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn) -> EmptyResult { - let cipher = match Cipher::find_by_uuid(uuid, conn) { +fn _delete_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn, ws: &State) -> EmptyResult { + let cipher = match Cipher::find_by_uuid(&uuid, &conn) { Some(cipher) => cipher, None => err!("Cipher doesn't exist"), }; @@ -679,13 +685,16 @@ fn _delete_cipher_by_uuid(uuid: &str, headers: &Headers, conn: &DbConn) -> Empty err!("Cipher can't be deleted by user") } - match cipher.delete(conn) { - Ok(()) => Ok(()), + match cipher.delete(&conn) { + Ok(()) => { + ws.send_cipher_update(UpdateType::SyncCipherDelete, &cipher, &cipher.update_users_revision(&conn)); + Ok(()) + } Err(_) => err!("Failed deleting cipher") } } -fn _delete_cipher_attachment_by_id(uuid: &str, attachment_id: &str, headers: &Headers, conn: &DbConn) -> EmptyResult { +fn _delete_cipher_attachment_by_id(uuid: &str, attachment_id: &str, headers: &Headers, conn: &DbConn, ws: &State) -> EmptyResult { let attachment = match Attachment::find_by_id(&attachment_id, &conn) { Some(attachment) => attachment, None => err!("Attachment doesn't exist") @@ -706,7 +715,10 @@ fn _delete_cipher_attachment_by_id(uuid: &str, attachment_id: &str, headers: &He // Delete attachment match attachment.delete(&conn) { - Ok(()) => Ok(()), + Ok(()) => { + ws.send_cipher_update(UpdateType::SyncCipherDelete, &cipher, &cipher.update_users_revision(&conn)); + Ok(()) + } Err(_) => err!("Deleting attachement failed") } } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 43c89498..2941db00 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -130,19 +130,25 @@ impl Cipher { json_object } - pub fn update_users_revision(&self, conn: &DbConn) { + pub fn update_users_revision(&self, conn: &DbConn) -> Vec { + let mut user_uuids = Vec::new(); match self.user_uuid { - Some(ref user_uuid) => User::update_uuid_revision(&user_uuid, conn), + Some(ref user_uuid) => { + User::update_uuid_revision(&user_uuid, conn); + user_uuids.push(user_uuid.clone()) + }, None => { // Belongs to Organization, need to update affected users if let Some(ref org_uuid) = self.organization_uuid { UserOrganization::find_by_cipher_and_org(&self.uuid, &org_uuid, conn) .iter() .for_each(|user_org| { - User::update_uuid_revision(&user_org.user_uuid, conn) + User::update_uuid_revision(&user_org.user_uuid, conn); + user_uuids.push(user_org.user_uuid.clone()) }); } } }; + user_uuids } pub fn save(&mut self, conn: &DbConn) -> bool { @@ -157,7 +163,7 @@ impl Cipher { } } - pub fn delete(self, conn: &DbConn) -> QueryResult<()> { + pub fn delete(&self, conn: &DbConn) -> QueryResult<()> { self.update_users_revision(conn); FolderCipher::delete_all_by_cipher(&self.uuid, &conn)?; @@ -166,7 +172,7 @@ impl Cipher { diesel::delete( ciphers::table.filter( - ciphers::uuid.eq(self.uuid) + ciphers::uuid.eq(&self.uuid) ) ).execute(&**conn).and(Ok(())) } @@ -358,21 +364,4 @@ impl Cipher { .select(ciphers_collections::collection_uuid) .load::(&**conn).unwrap_or(vec![]) } - - pub fn get_users(&self, conn: &DbConn) -> Vec { - let mut user_uuids = Vec::new(); - match self.user_uuid { - Some(ref user_uuid) => user_uuids.push(user_uuid.clone()), - None => { // Belongs to Organization, need to update affected users - if let Some(ref org_uuid) = self.organization_uuid { - UserOrganization::find_by_cipher_and_org(&self.uuid, &org_uuid, conn) - .iter() - .for_each(|user_org| { - user_uuids.push(user_org.user_uuid.clone()) - }); - } - } - }; - user_uuids - } }