From 11cc5a79b747b040dceba21ae2925f4ab79909dd Mon Sep 17 00:00:00 2001 From: MFijak Date: Tue, 2 Aug 2022 14:07:38 +0200 Subject: [PATCH] collection endpoint support for groups implemented --- src/api/core/organizations.rs | 41 +++++++++++++++++++++++++++++++---- src/db/models/group.rs | 28 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 20c951ed..859eb19b 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -98,10 +98,19 @@ struct OrganizationUpdateData { Name: String, } -#[derive(Deserialize, Debug)] +#[derive(Deserialize)] #[allow(non_snake_case)] struct NewCollectionData { Name: String, + Groups: Vec, +} + +#[derive(Deserialize)] +#[allow(non_snake_case)] +struct NewCollectionGroupData { + HidePasswords: bool, + Id: String, + ReadOnly: bool, } #[derive(Deserialize)] @@ -338,6 +347,13 @@ async fn post_organization_collection_update( collection.name = data.Name; collection.save(&conn).await?; + CollectionGroup::delete_all_by_collection(&col_id, &conn).await?; + + for group in data.Groups { + CollectionGroup::new(col_id.clone(), group.Id, group.ReadOnly, group.HidePasswords) + .save(&conn).await?; + } + Ok(Json(collection.to_json())) } @@ -433,7 +449,16 @@ async fn get_org_collection_detail( err!("Collection is not owned by organization") } - Ok(Json(collection.to_json())) + let groups: Vec = CollectionGroup::find_by_collection(&collection.uuid, &conn).await + .iter() + .map(|collection_group| SelectionReadOnly::to_collection_group_details_read_only(collection_group).to_json()) + .collect(); + + let mut json_object = collection.to_json(); + json_object["Groups"] = json!(groups); + json_object["Object"] = json!("collectionGroupDetails"); + + Ok(Json(json_object)) } } } @@ -1570,7 +1595,7 @@ impl SelectionReadOnly { ) } - pub fn to_selection_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly { + pub fn to_group_details_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly { SelectionReadOnly { Id: collection_group.collections_uuid.clone(), ReadOnly: collection_group.read_only, @@ -1578,6 +1603,14 @@ impl SelectionReadOnly { } } + pub fn to_collection_group_details_read_only (collection_group: &CollectionGroup) -> SelectionReadOnly { + SelectionReadOnly { + Id: collection_group.groups_uuid.clone(), + ReadOnly: collection_group.read_only, + HidePasswords: collection_group.hide_passwords + } + } + pub fn to_json (&self) -> Value { json!(self) } @@ -1644,7 +1677,7 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea let collection_groups = CollectionGroup::find_by_group(&group_id, &conn).await .iter() - .map(|entry| SelectionReadOnly::to_selection_read_only(entry).to_json()) + .map(|entry| SelectionReadOnly::to_group_details_read_only(entry).to_json()) .collect::(); Ok(Json(json!({ diff --git a/src/db/models/group.rs b/src/db/models/group.rs index 6ebcbd2d..cbf308b1 100644 --- a/src/db/models/group.rs +++ b/src/db/models/group.rs @@ -294,6 +294,17 @@ impl CollectionGroup { }} } + pub async fn find_by_collection(collection_uuid: &str, conn: &DbConn) -> Vec { + db_run! { conn: { + collection_groups::table + .filter(collection_groups::collections_uuid.eq(collection_uuid)) + .select(collection_groups::all_columns) + .load::(conn) + .expect("Error loading collection groups") + .from_db() + }} + } + pub async fn delete(&self, conn: &DbConn) -> EmptyResult { let group_users = GroupUser::find_by_group(&self.groups_uuid, conn).await; for group_user in group_users { @@ -322,6 +333,23 @@ impl CollectionGroup { .map_res("Error deleting collection group") }} } + + pub async fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> EmptyResult { + let collection_assigned_to_groups = CollectionGroup::find_by_collection(collection_uuid, conn).await; + for collection_assigned_to_group in collection_assigned_to_groups { + let group_users = GroupUser::find_by_group(&collection_assigned_to_group.groups_uuid, conn).await; + for group_user in group_users { + group_user.update_user_revision(conn).await; + } + } + + db_run! { conn: { + diesel::delete(collection_groups::table) + .filter(collection_groups::collections_uuid.eq(collection_uuid)) + .execute(conn) + .map_res("Error deleting collection group") + }} + } } impl GroupUser {