From 71c3c06c01807810bd03fdf6d301078e93c93af7 Mon Sep 17 00:00:00 2001 From: mdecimus Date: Sun, 22 Sep 2024 10:36:40 +0200 Subject: [PATCH] Add enterprise feature to directory crate (#783 ). --- crates/directory/Cargo.toml | 1 + crates/directory/src/backend/internal/manage.rs | 14 +++++++++++--- crates/directory/src/backend/internal/mod.rs | 8 ++++++++ crates/main/Cargo.toml | 2 +- install.sh | 1 - resources/systemd/stalwart-mail.service | 1 - tests/Cargo.toml | 2 +- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/directory/Cargo.toml b/crates/directory/Cargo.toml index 222114ee..eb3c849b 100644 --- a/crates/directory/Cargo.toml +++ b/crates/directory/Cargo.toml @@ -42,3 +42,4 @@ tokio = { version = "1.23", features = ["full"] } [features] test_mode = [] +enterprise = [] diff --git a/crates/directory/src/backend/internal/manage.rs b/crates/directory/src/backend/internal/manage.rs index fbc150f1..134da198 100644 --- a/crates/directory/src/backend/internal/manage.rs +++ b/crates/directory/src/backend/internal/manage.rs @@ -186,13 +186,14 @@ impl ManageDirectory for Store { if name.is_empty() { return Err(err_missing(PrincipalField::Name)); } + let mut valid_domains: AHashSet = AHashSet::new(); // SPDX-SnippetBegin // SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd // SPDX-License-Identifier: LicenseRef-SEL // Validate tenant - let mut valid_domains = AHashSet::new(); + #[cfg(feature = "enterprise")] if let Some(tenant_id) = tenant_id { let tenant = self .query(QueryBy::Id(tenant_id), false) @@ -246,6 +247,7 @@ impl ManageDirectory for Store { // SPDX-License-Identifier: LicenseRef-SEL // Obtain tenant id, only if no default tenant is provided + #[cfg(feature = "enterprise")] if let (Some(tenant_name), None) = (principal.take_str(PrincipalField::Tenant), tenant_id) { tenant_id = self .get_principal_info(&tenant_name) @@ -258,6 +260,7 @@ impl ManageDirectory for Store { } // Tenants must provide principal names including a valid domain + #[cfg(feature = "enterprise")] if let Some(tenant_id) = tenant_id { if matches!(principal.typ, Type::Tenant) { return Err(error( @@ -475,13 +478,14 @@ impl ManageDirectory for Store { .await .caused_by(trc::location!())? .ok_or_else(|| not_found(principal_id.to_string()))?; + let mut batch = BatchBuilder::new(); // SPDX-SnippetBegin // SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd // SPDX-License-Identifier: LicenseRef-SEL // Make sure tenant has no data - let mut batch = BatchBuilder::new(); + #[cfg(feature = "enterprise")] match principal.typ { Type::Individual | Type::Group => { // Update tenant quota @@ -676,12 +680,14 @@ impl ManageDirectory for Store { ); } + let mut used_quota: Option = None; + // SPDX-SnippetBegin // SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd // SPDX-License-Identifier: LicenseRef-SEL // Obtain used quota - let mut used_quota = None; + #[cfg(feature = "enterprise")] if tenant_id.is_none() && changes .iter() @@ -781,6 +787,7 @@ impl ManageDirectory for Store { // SPDX-SnippetBegin // SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd // SPDX-License-Identifier: LicenseRef-SEL + #[cfg(feature = "enterprise")] ( PrincipalAction::Set, PrincipalField::Tenant, @@ -1679,6 +1686,7 @@ impl ManageDirectory for Store { // SPDX-License-Identifier: LicenseRef-SEL // Map tenant name + #[cfg(feature = "enterprise")] if let Some(tenant_id) = principal.take_int(PrincipalField::Tenant) { if fields.is_empty() || fields.contains(&PrincipalField::Tenant) { if let Some(name) = self diff --git a/crates/directory/src/backend/internal/mod.rs b/crates/directory/src/backend/internal/mod.rs index e41cd898..48e49772 100644 --- a/crates/directory/src/backend/internal/mod.rs +++ b/crates/directory/src/backend/internal/mod.rs @@ -98,6 +98,7 @@ impl Deserialize for Principal { } } +#[cfg(feature = "enterprise")] impl PrincipalInfo { // SPDX-SnippetBegin // SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd @@ -113,6 +114,13 @@ impl PrincipalInfo { // SPDX-SnippetEnd } +#[cfg(not(feature = "enterprise"))] +impl PrincipalInfo { + pub fn has_tenant_access(&self, _tenant_id: Option) -> bool { + true + } +} + impl Serialize for PrincipalInfo { fn serialize(self) -> Vec { if let Some(tenant) = self.tenant { diff --git a/crates/main/Cargo.toml b/crates/main/Cargo.toml index 825e319d..170f90da 100644 --- a/crates/main/Cargo.toml +++ b/crates/main/Cargo.toml @@ -44,4 +44,4 @@ rocks = ["store/rocks"] elastic = ["store/elastic"] s3 = ["store/s3"] redis = ["store/redis"] -enterprise = ["jmap/enterprise", "common/enterprise", "store/enterprise", "managesieve/enterprise"] +enterprise = ["jmap/enterprise", "common/enterprise", "store/enterprise", "managesieve/enterprise", "directory/enterprise"] diff --git a/install.sh b/install.sh index 4b39096f..f4a78fb0 100644 --- a/install.sh +++ b/install.sh @@ -151,7 +151,6 @@ KillSignal=SIGINT Restart=on-failure RestartSec=5 ExecStart=__PATH__/bin/stalwart-mail --config=__PATH__/etc/config.toml -PermissionsStartOnly=true SyslogIdentifier=stalwart-mail User=stalwart-mail Group=stalwart-mail diff --git a/resources/systemd/stalwart-mail.service b/resources/systemd/stalwart-mail.service index a4d8e552..12357ab0 100644 --- a/resources/systemd/stalwart-mail.service +++ b/resources/systemd/stalwart-mail.service @@ -12,7 +12,6 @@ KillSignal=SIGINT Restart=on-failure RestartSec=5 ExecStart=__PATH__/bin/stalwart-mail --config=__PATH__/etc/config.toml -PermissionsStartOnly=true SyslogIdentifier=stalwart-mail User=stalwart-mail Group=stalwart-mail diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 76f51478..1c2a085d 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -19,7 +19,7 @@ redis = ["store/redis"] [dev-dependencies] store = { path = "../crates/store", features = ["test_mode", "enterprise"] } nlp = { path = "../crates/nlp" } -directory = { path = "../crates/directory", features = ["test_mode"] } +directory = { path = "../crates/directory", features = ["test_mode", "enterprise"] } jmap = { path = "../crates/jmap", features = ["test_mode", "enterprise"] } jmap_proto = { path = "../crates/jmap-proto" } imap = { path = "../crates/imap", features = ["test_mode"] }