From e47a2fd0f3c0728a6fdf6f552a3ee78592f70414 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Mon, 12 Sep 2022 16:08:36 +0200 Subject: [PATCH] Rename/Fix revoke/restore endpoints In web-vault v2022.9.x it seems the endpoints changed. - activate > restore - deactivate > revoke This PR adds those endpoints and renames the functions. It also keeps the previous endpoints for now to be compatible with previous vault verions for now, just in case. --- src/api/core/organizations.rs | 64 ++++++++++++++++++++++++++++++----- src/db/models/organization.rs | 2 +- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 9b7d264c..99a3e0be 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -63,8 +63,12 @@ pub fn routes() -> Vec { bulk_public_keys, deactivate_organization_user, bulk_deactivate_organization_user, + revoke_organization_user, + bulk_revoke_organization_user, activate_organization_user, - bulk_activate_organization_user + bulk_activate_organization_user, + restore_organization_user, + bulk_restore_organization_user ] } @@ -1480,6 +1484,7 @@ async fn import(org_id: String, data: JsonUpcase, headers: Header Ok(()) } +// Pre web-vault v2022.9.x endpoint #[put("/organizations//users//deactivate")] async fn deactivate_organization_user( org_id: String, @@ -1487,15 +1492,36 @@ async fn deactivate_organization_user( headers: AdminHeaders, conn: DbConn, ) -> EmptyResult { - _deactivate_organization_user(&org_id, &org_user_id, &headers, &conn).await + _revoke_organization_user(&org_id, &org_user_id, &headers, &conn).await } +// Pre web-vault v2022.9.x endpoint #[put("/organizations//users/deactivate", data = "")] async fn bulk_deactivate_organization_user( org_id: String, data: JsonUpcase, headers: AdminHeaders, conn: DbConn, +) -> Json { + bulk_revoke_organization_user(org_id, data, headers, conn).await +} + +#[put("/organizations//users//revoke")] +async fn revoke_organization_user( + org_id: String, + org_user_id: String, + headers: AdminHeaders, + conn: DbConn, +) -> EmptyResult { + _revoke_organization_user(&org_id, &org_user_id, &headers, &conn).await +} + +#[put("/organizations//users/revoke", data = "")] +async fn bulk_revoke_organization_user( + org_id: String, + data: JsonUpcase, + headers: AdminHeaders, + conn: DbConn, ) -> Json { let data = data.into_inner().data; @@ -1504,7 +1530,7 @@ async fn bulk_deactivate_organization_user( Some(org_users) => { for org_user_id in org_users { let org_user_id = org_user_id.as_str().unwrap_or_default(); - let err_msg = match _deactivate_organization_user(&org_id, org_user_id, &headers, &conn).await { + let err_msg = match _revoke_organization_user(&org_id, org_user_id, &headers, &conn).await { Ok(_) => String::from(""), Err(e) => format!("{:?}", e), }; @@ -1528,7 +1554,7 @@ async fn bulk_deactivate_organization_user( })) } -async fn _deactivate_organization_user( +async fn _revoke_organization_user( org_id: &str, org_user_id: &str, headers: &AdminHeaders, @@ -1557,6 +1583,7 @@ async fn _deactivate_organization_user( Ok(()) } +// Pre web-vault v2022.9.x endpoint #[put("/organizations//users//activate")] async fn activate_organization_user( org_id: String, @@ -1564,15 +1591,36 @@ async fn activate_organization_user( headers: AdminHeaders, conn: DbConn, ) -> EmptyResult { - _activate_organization_user(&org_id, &org_user_id, &headers, &conn).await + _restore_organization_user(&org_id, &org_user_id, &headers, &conn).await } +// Pre web-vault v2022.9.x endpoint #[put("/organizations//users/activate", data = "")] async fn bulk_activate_organization_user( org_id: String, data: JsonUpcase, headers: AdminHeaders, conn: DbConn, +) -> Json { + bulk_restore_organization_user(org_id, data, headers, conn).await +} + +#[put("/organizations//users//restore")] +async fn restore_organization_user( + org_id: String, + org_user_id: String, + headers: AdminHeaders, + conn: DbConn, +) -> EmptyResult { + _restore_organization_user(&org_id, &org_user_id, &headers, &conn).await +} + +#[put("/organizations//users/restore", data = "")] +async fn bulk_restore_organization_user( + org_id: String, + data: JsonUpcase, + headers: AdminHeaders, + conn: DbConn, ) -> Json { let data = data.into_inner().data; @@ -1581,7 +1629,7 @@ async fn bulk_activate_organization_user( Some(org_users) => { for org_user_id in org_users { let org_user_id = org_user_id.as_str().unwrap_or_default(); - let err_msg = match _activate_organization_user(&org_id, org_user_id, &headers, &conn).await { + let err_msg = match _restore_organization_user(&org_id, org_user_id, &headers, &conn).await { Ok(_) => String::from(""), Err(e) => format!("{:?}", e), }; @@ -1605,7 +1653,7 @@ async fn bulk_activate_organization_user( })) } -async fn _activate_organization_user( +async fn _restore_organization_user( org_id: &str, org_user_id: &str, headers: &AdminHeaders, @@ -1634,7 +1682,7 @@ async fn _activate_organization_user( } } - user_org.activate(); + user_org.restore(); user_org.save(conn).await?; } Some(_) => err!("User is already active"), diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index eb2de71a..99787eb8 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -196,7 +196,7 @@ impl UserOrganization { } } - pub fn activate(&mut self) { + pub fn restore(&mut self) { if self.status < UserOrgStatus::Accepted as i32 { self.status += ACTIVATE_REVOKE_DIFF; }