MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / combine_policies

Function combine_policies

nodedb/src/control/security/predicate_eval.rs:126–161  ·  view source on GitHub ↗

Combine multiple policies according to their modes. Final result: `(any permissive passes) AND (all restrictive pass)`. Returns the combined `ScanFilter` list to inject into the query. Empty return = no RLS policies (allow all).

(
    policies: &[(RlsPredicate, PolicyMode)],
    auth: &AuthContext,
)

Source from the content-addressed store, hash-verified

124/// Returns the combined `ScanFilter` list to inject into the query.
125/// Empty return = no RLS policies (allow all).
126pub fn combine_policies(
127 policies: &[(RlsPredicate, PolicyMode)],
128 auth: &AuthContext,
129) -> Option<Vec<ScanFilter>> {
130 if policies.is_empty() {
131 return Some(Vec::new()); // No policies → allow all
132 }
133
134 let mut permissive: Vec<&RlsPredicate> = Vec::new();
135 let mut restrictive: Vec<&RlsPredicate> = Vec::new();
136
137 for (pred, mode) in policies {
138 match mode {
139 PolicyMode::Permissive => permissive.push(pred),
140 PolicyMode::Restrictive => restrictive.push(pred),
141 }
142 }
143
144 let mut combined = Vec::new();
145
146 // Permissive: OR-combine. If no permissive policies exist, default allow.
147 if permissive.len() == 1 {
148 combined.extend(substitute_to_scan_filters(permissive[0], auth)?);
149 } else if permissive.len() > 1 {
150 let or_children: Vec<RlsPredicate> = permissive.iter().map(|p| (*p).clone()).collect();
151 let or_pred = RlsPredicate::Or(or_children);
152 combined.extend(substitute_to_scan_filters(&or_pred, auth)?);
153 }
154
155 // Restrictive: AND-combine (each becomes additional filters).
156 for pred in &restrictive {
157 combined.extend(substitute_to_scan_filters(pred, auth)?);
158 }
159
160 Some(combined)
161}
162
163// ---------------------------------------------------------------------------
164// Internal helpers

Calls 8

collectMethod · 0.80
is_emptyMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45
extendMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45