aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlackDex <[email protected]>2023-02-16 17:29:12 +0100
committerDaniel GarcĂ­a <[email protected]>2023-02-21 21:29:09 +0100
commit54cc47b14e329e7c874d40fe4d0109418b987be1 (patch)
tree63e52160d20c50f8779dcd327072a770793d11e6
parentfac44888cdcf9b7844dfcb21e484e5e67c5fafa9 (diff)
downloadvaultwarden-54cc47b14e329e7c874d40fe4d0109418b987be1.tar.gz
vaultwarden-54cc47b14e329e7c874d40fe4d0109418b987be1.zip
Fix Organization delete when groups are configured
With existing groups configured within an org, deleting that org would fail because of Foreign Key issues. This PR fixes this by making sure the groups get deleted before the org does. Fixes #3247
-rw-r--r--src/db/models/group.rs7
-rw-r--r--src/db/models/organization.rs3
2 files changed, 9 insertions, 1 deletions
diff --git a/src/db/models/group.rs b/src/db/models/group.rs
index 1d2e6062..6f267c10 100644
--- a/src/db/models/group.rs
+++ b/src/db/models/group.rs
@@ -151,6 +151,13 @@ impl Group {
}
}
+ pub async fn delete_all_by_organization(org_uuid: &str, conn: &mut DbConn) -> EmptyResult {
+ for group in Self::find_by_organization(org_uuid, conn).await {
+ group.delete(conn).await?;
+ }
+ Ok(())
+ }
+
pub async fn find_by_organization(organizations_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
db_run! { conn: {
groups::table
diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs
index a6e4be21..34325b78 100644
--- a/src/db/models/organization.rs
+++ b/src/db/models/organization.rs
@@ -2,7 +2,7 @@ use num_traits::FromPrimitive;
use serde_json::Value;
use std::cmp::Ordering;
-use super::{CollectionUser, GroupUser, OrgPolicy, OrgPolicyType, TwoFactor, User};
+use super::{CollectionUser, Group, GroupUser, OrgPolicy, OrgPolicyType, TwoFactor, User};
use crate::CONFIG;
db_object! {
@@ -267,6 +267,7 @@ impl Organization {
Collection::delete_all_by_organization(&self.uuid, conn).await?;
UserOrganization::delete_all_by_organization(&self.uuid, conn).await?;
OrgPolicy::delete_all_by_organization(&self.uuid, conn).await?;
+ Group::delete_all_by_organization(&self.uuid, conn).await?;
db_run! { conn: {
diesel::delete(organizations::table.filter(organizations::uuid.eq(self.uuid)))