From 3cf67e0b8ddc8d4f00dc122de511cbff6b650d58 Mon Sep 17 00:00:00 2001 From: Stefan Melmuk Date: Sat, 18 Mar 2023 21:13:06 +0100 Subject: [PATCH] add endpoint to bulk delete groups --- src/api/core/organizations.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 9c46617a..172c7f90 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -81,6 +81,7 @@ pub fn routes() -> Vec { get_group_details, delete_group, post_delete_group, + bulk_delete_groups, get_group_users, put_group_users, get_user_groups, @@ -2363,17 +2364,21 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea } #[post("/organizations//groups//delete")] -async fn post_delete_group(org_id: String, group_id: String, headers: AdminHeaders, conn: DbConn) -> EmptyResult { - delete_group(org_id, group_id, headers, conn).await +async fn post_delete_group(org_id: String, group_id: String, headers: AdminHeaders, mut conn: DbConn) -> EmptyResult { + _delete_group(org_id, group_id, &headers, &mut conn).await } #[delete("/organizations//groups/")] async fn delete_group(org_id: String, group_id: String, headers: AdminHeaders, mut conn: DbConn) -> EmptyResult { + _delete_group(org_id, group_id, &headers, &mut conn).await +} + +async fn _delete_group(org_id: String, group_id: String, headers: &AdminHeaders, conn: &mut DbConn) -> EmptyResult { if !CONFIG.org_groups_enabled() { err!("Group support is disabled"); } - let group = match Group::find_by_uuid(&group_id, &mut conn).await { + let group = match Group::find_by_uuid(&group_id, conn).await { Some(group) => group, _ => err!("Group not found"), }; @@ -2385,11 +2390,30 @@ async fn delete_group(org_id: String, group_id: String, headers: AdminHeaders, m headers.user.uuid.clone(), headers.device.atype, &headers.ip.ip, - &mut conn, + conn, ) .await; - group.delete(&mut conn).await + group.delete(conn).await +} + +#[delete("/organizations//groups", data = "")] +async fn bulk_delete_groups( + org_id: String, + data: JsonUpcase, + headers: AdminHeaders, + mut conn: DbConn, +) -> EmptyResult { + if !CONFIG.org_groups_enabled() { + err!("Group support is disabled"); + } + + let data: OrgBulkIds = data.into_inner().data; + + for group_id in data.Ids { + _delete_group(org_id.clone(), group_id, &headers, &mut conn).await? + } + Ok(()) } #[get("/organizations/<_org_id>/groups/")]