diff options
Diffstat (limited to 'src/db/models')
-rw-r--r-- | src/db/models/attachment.rs | 6 | ||||
-rw-r--r-- | src/db/models/cipher.rs | 23 | ||||
-rw-r--r-- | src/db/models/collection.rs | 12 | ||||
-rw-r--r-- | src/db/models/folder.rs | 2 | ||||
-rw-r--r-- | src/db/models/organization.rs | 37 | ||||
-rw-r--r-- | src/db/models/user.rs | 19 |
6 files changed, 92 insertions, 7 deletions
diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index 1f5e29a7..66d5b723 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -111,4 +111,10 @@ impl Attachment { .filter(attachments::cipher_uuid.eq(cipher_uuid)) .load::<Self>(&**conn).expect("Error loading attachments") } + + pub fn find_by_ciphers(cipher_uuids: Vec<String>, conn: &DbConn) -> Vec<Self> { + attachments::table + .filter(attachments::cipher_uuid.eq_any(cipher_uuids)) + .load::<Self>(&**conn).expect("Error loading attachments") + } } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 899e5dda..3faf1aea 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -3,7 +3,7 @@ use serde_json::Value as JsonValue; use uuid::Uuid; -use super::{User, Organization, Attachment, FolderCipher, CollectionCipher, UserOrgType, UserOrgStatus}; +use super::{User, Organization, Attachment, FolderCipher, CollectionCipher, UserOrganization, UserOrgType, UserOrgStatus}; #[derive(Debug, Identifiable, Queryable, Insertable, Associations)] #[table_name = "ciphers"] @@ -122,7 +122,23 @@ impl Cipher { json_object } + pub fn update_users_revision(&self, conn: &DbConn) { + match self.user_uuid { + Some(ref user_uuid) => User::update_uuid_revision(&user_uuid, conn), + 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) + }); + } + } + }; + } + pub fn save(&mut self, conn: &DbConn) -> bool { + self.update_users_revision(conn); self.updated_at = Utc::now().naive_utc(); match diesel::replace_into(ciphers::table) @@ -134,6 +150,8 @@ impl Cipher { } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { + self.update_users_revision(conn); + FolderCipher::delete_all_by_cipher(&self.uuid, &conn)?; CollectionCipher::delete_all_by_cipher(&self.uuid, &conn)?; Attachment::delete_all_by_cipher(&self.uuid, &conn)?; @@ -157,6 +175,7 @@ impl Cipher { None => { match folder_uuid { Some(new_folder) => { + self.update_users_revision(conn); let folder_cipher = FolderCipher::new(&new_folder, &self.uuid); folder_cipher.save(&conn).or(Err("Couldn't save folder setting")) }, @@ -169,6 +188,7 @@ impl Cipher { if current_folder == new_folder { Ok(()) //nothing to do } else { + self.update_users_revision(conn); match FolderCipher::find_by_folder_and_cipher(¤t_folder, &self.uuid, &conn) { Some(current_folder) => { current_folder.delete(&conn).or(Err("Failed removing old folder mapping")) @@ -181,6 +201,7 @@ impl Cipher { } }, None => { + self.update_users_revision(conn); match FolderCipher::find_by_folder_and_cipher(¤t_folder, &self.uuid, &conn) { Some(current_folder) => { current_folder.delete(&conn).or(Err("Failed removing old folder mapping")) diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index fa16ec28..2bf116c5 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -185,6 +185,8 @@ impl CollectionUser { } pub fn save(user_uuid: &str, collection_uuid: &str, read_only:bool, conn: &DbConn) -> QueryResult<()> { + User::update_uuid_revision(&user_uuid, conn); + diesel::replace_into(users_collections::table) .values(( users_collections::user_uuid.eq(user_uuid), @@ -194,6 +196,8 @@ impl CollectionUser { } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { + User::update_uuid_revision(&self.user_uuid, conn); + diesel::delete(users_collections::table .filter(users_collections::user_uuid.eq(&self.user_uuid)) .filter(users_collections::collection_uuid.eq(&self.collection_uuid))) @@ -216,12 +220,20 @@ impl CollectionUser { } pub fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> QueryResult<()> { + CollectionUser::find_by_collection(&collection_uuid, conn) + .iter() + .for_each(|collection| { + User::update_uuid_revision(&collection.user_uuid, conn) + }); + diesel::delete(users_collections::table .filter(users_collections::collection_uuid.eq(collection_uuid)) ).execute(&**conn).and(Ok(())) } pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> QueryResult<()> { + User::update_uuid_revision(&user_uuid, conn); + diesel::delete(users_collections::table .filter(users_collections::user_uuid.eq(user_uuid)) ).execute(&**conn).and(Ok(())) diff --git a/src/db/models/folder.rs b/src/db/models/folder.rs index d9b90bf0..701a7da9 100644 --- a/src/db/models/folder.rs +++ b/src/db/models/folder.rs @@ -71,6 +71,7 @@ use db::schema::{folders, folders_ciphers}; /// Database methods impl Folder { pub fn save(&mut self, conn: &DbConn) -> bool { + User::update_uuid_revision(&self.user_uuid, conn); self.updated_at = Utc::now().naive_utc(); match diesel::replace_into(folders::table) @@ -82,6 +83,7 @@ impl Folder { } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { + User::update_uuid_revision(&self.user_uuid, conn); FolderCipher::delete_all_by_folder(&self.uuid, &conn)?; diesel::delete( diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 4bb55912..d46d15da 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -1,6 +1,7 @@ use serde_json::Value as JsonValue; use uuid::Uuid; +use super::{User, CollectionUser}; #[derive(Debug, Identifiable, Queryable, Insertable)] #[table_name = "organizations"] @@ -108,12 +109,17 @@ impl UserOrganization { use diesel; use diesel::prelude::*; use db::DbConn; -use db::schema::organizations; -use db::schema::users_organizations; +use db::schema::{organizations, users_organizations, users_collections, ciphers_collections}; /// Database methods impl Organization { pub fn save(&mut self, conn: &DbConn) -> bool { + UserOrganization::find_by_org(&self.uuid, conn) + .iter() + .for_each(|user_org| { + User::update_uuid_revision(&user_org.user_uuid, conn); + }); + match diesel::replace_into(organizations::table) .values(&*self) .execute(&**conn) { @@ -172,7 +178,6 @@ impl UserOrganization { } pub fn to_json_user_details(&self, conn: &DbConn) -> JsonValue { - use super::User; let user = User::find_by_uuid(&self.user_uuid, conn).unwrap(); json!({ @@ -190,7 +195,6 @@ impl UserOrganization { } pub fn to_json_collection_user_details(&self, read_only: &bool, conn: &DbConn) -> JsonValue { - use super::User; let user = User::find_by_uuid(&self.user_uuid, conn).unwrap(); json!({ @@ -209,7 +213,6 @@ impl UserOrganization { let coll_uuids = if self.access_all { vec![] // If we have complete access, no need to fill the array } else { - use super::CollectionUser; let collections = CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn); collections.iter().map(|c| json!({"Id": c.collection_uuid, "ReadOnly": c.read_only})).collect() }; @@ -228,6 +231,8 @@ impl UserOrganization { } pub fn save(&mut self, conn: &DbConn) -> bool { + User::update_uuid_revision(&self.user_uuid, conn); + match diesel::replace_into(users_organizations::table) .values(&*self) .execute(&**conn) { @@ -237,7 +242,7 @@ impl UserOrganization { } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { - use super::CollectionUser; + User::update_uuid_revision(&self.user_uuid, conn); CollectionUser::delete_all_by_user(&self.user_uuid, &conn)?; @@ -291,6 +296,26 @@ impl UserOrganization { .filter(users_organizations::org_uuid.eq(org_uuid)) .first::<Self>(&**conn).ok() } + + pub fn find_by_cipher_and_org(cipher_uuid: &str, org_uuid: &str, conn: &DbConn) -> Vec<Self> { + users_organizations::table + .filter(users_organizations::org_uuid.eq(org_uuid)) + .left_join(users_collections::table.on( + users_collections::user_uuid.eq(users_organizations::user_uuid) + )) + .left_join(ciphers_collections::table.on( + ciphers_collections::collection_uuid.eq(users_collections::collection_uuid).and( + ciphers_collections::cipher_uuid.eq(&cipher_uuid) + ) + )) + .filter( + users_organizations::access_all.eq(true).or( // AccessAll.. + ciphers_collections::cipher_uuid.eq(&cipher_uuid) // ..or access to collection with cipher + ) + ) + .select(users_organizations::all_columns) + .load::<Self>(&**conn).expect("Error loading user organizations") + } } diff --git a/src/db/models/user.rs b/src/db/models/user.rs index 2c30c52d..e100d893 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -154,6 +154,25 @@ impl User { } } + pub fn update_uuid_revision(uuid: &str, conn: &DbConn) { + if let Some(mut user) = User::find_by_uuid(&uuid, conn) { + if user.update_revision(conn).is_err(){ + println!("Warning: Failed to update revision for {}", user.email); + }; + }; + } + + pub fn update_revision(&mut self, conn: &DbConn) -> QueryResult<()> { + self.updated_at = Utc::now().naive_utc(); + diesel::update( + users::table.filter( + users::uuid.eq(&self.uuid) + ) + ) + .set(users::updated_at.eq(&self.updated_at)) + .execute(&**conn).and(Ok(())) + } + pub fn find_by_mail(mail: &str, conn: &DbConn) -> Option<Self> { let lower_mail = mail.to_lowercase(); users::table |