oxigdal_security/multitenancy/
isolation.rs1use crate::error::Result;
4use dashmap::DashMap;
5use std::collections::HashSet;
6use std::sync::Arc;
7
8pub struct IsolationManager {
10 tenant_resources: Arc<DashMap<String, HashSet<String>>>,
12}
13
14impl IsolationManager {
15 pub fn new() -> Self {
17 Self {
18 tenant_resources: Arc::new(DashMap::new()),
19 }
20 }
21
22 pub fn assign_resource(&self, tenant_id: String, resource_id: String) -> Result<()> {
24 self.tenant_resources
25 .entry(tenant_id)
26 .or_default()
27 .insert(resource_id);
28 Ok(())
29 }
30
31 pub fn owns_resource(&self, tenant_id: &str, resource_id: &str) -> bool {
33 self.tenant_resources
34 .get(tenant_id)
35 .is_some_and(|resources| resources.contains(resource_id))
36 }
37
38 pub fn get_resources(&self, tenant_id: &str) -> Vec<String> {
40 self.tenant_resources
41 .get(tenant_id)
42 .map(|resources| resources.iter().cloned().collect())
43 .unwrap_or_default()
44 }
45}
46
47impl Default for IsolationManager {
48 fn default() -> Self {
49 Self::new()
50 }
51}