From 54f54ee8450acec8b3eb1e10532eff101823ac32 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Mon, 1 Oct 2018 16:52:36 +0100 Subject: [PATCH 1/2] Update revision for users on collection save --- src/db/models/collection.rs | 19 ++++++++++++------- src/db/models/organization.rs | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index 2bf116c5..ddc4d2e9 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -42,13 +42,18 @@ use db::schema::*; /// Database methods impl Collection { - pub fn save(&mut self, conn: &DbConn) -> bool { - match diesel::replace_into(collections::table) - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { + // Update affected users revision + UserOrganization::find_by_collection_and_org(&self.uuid, &self.org_uuid, conn) + .iter() + .for_each(|user_org| { + User::update_uuid_revision(&user_org.user_uuid, conn); + }); + + diesel::replace_into(collections::table) + .values(&*self) + .execute(&**conn) + .and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 7f1d297b..9dd9cc99 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -331,6 +331,22 @@ impl UserOrganization { .select(users_organizations::all_columns) .load::(&**conn).expect("Error loading user organizations") } + + pub fn find_by_collection_and_org(collection_uuid: &str, org_uuid: &str, conn: &DbConn) -> Vec { + 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) + )) + .filter( + users_organizations::access_all.eq(true).or( // AccessAll.. + users_collections::collection_uuid.eq(&collection_uuid) // ..or access to collection with cipher + ) + ) + .select(users_organizations::all_columns) + .load::(&**conn).expect("Error loading user organizations") + } + } From 86b49856a7a95ae0bab2067b3a19cdca0651ba33 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Mon, 1 Oct 2018 17:50:31 +0100 Subject: [PATCH 2/2] Handle return value from Collection::save() --- src/api/core/organizations.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 687ee35c..d8074728 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -51,7 +51,9 @@ fn create_organization(headers: Headers, data: JsonUpcase, conn: DbConn org.save(&conn); user_org.save(&conn); - collection.save(&conn); + if collection.save(&conn).is_err() { + err!("Failed creating Collection"); + } Ok(Json(org.to_json())) } @@ -168,7 +170,9 @@ fn post_organization_collections(org_id: String, _headers: AdminHeaders, data: J let mut collection = Collection::new(org.uuid.clone(), data.Name); - collection.save(&conn); + if collection.save(&conn).is_err() { + err!("Failed saving Collection"); + } Ok(Json(collection.to_json())) } @@ -197,7 +201,9 @@ fn post_organization_collection_update(org_id: String, col_id: String, _headers: } collection.name = data.Name.clone(); - collection.save(&conn); + if collection.save(&conn).is_err() { + err!("Failed updating Collection"); + } Ok(Json(collection.to_json())) } @@ -618,8 +624,11 @@ fn post_org_import(query: OrgIdData, data: JsonUpcase, headers: Head // Read and create the collections let collections: Vec<_> = data.Collections.into_iter().map(|coll| { let mut collection = Collection::new(org_id.clone(), coll.Name); - collection.save(&conn); - collection + if collection.save(&conn).is_err() { + err!("Failed to create Collection"); + } + + Ok(collection) }).collect(); // Read the relations between collections and ciphers @@ -638,8 +647,12 @@ fn post_org_import(query: OrgIdData, data: JsonUpcase, headers: Head // Assign the collections for (cipher_index, coll_index) in relations { let cipher_id = &ciphers[cipher_index].uuid; - let coll_id = &collections[coll_index].uuid; - + let coll = &collections[coll_index]; + let coll_id = match coll { + Ok(coll) => coll.uuid.as_str(), + Err(_) => err!("Failed to assign to collection") + }; + CollectionCipher::save(cipher_id, coll_id, &conn); }