Skip to main content

uqa_sql/catalog/security/table_binding/
production.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retained read-only authorization keeps ACL contents independently of catalog mutation revisions.
8
9use super::{BoundTableAclEntry, BoundTableSecurity, RoleDefinition, RoleIdentity, TableSecurity};
10use crate::{plan::copy::SyntaxProduction, SQLError};
11use std::collections::BTreeMap;
12use uqa_core::memory::{Produced, ProductionControl};
13
14#[derive(Debug)]
15pub struct CapturedTablePrivileges {
16    pub role_owner: RoleIdentity,
17    acl: Option<Vec<BoundTableAclEntry>>,
18    column_acls: Vec<(String, Vec<BoundTableAclEntry>)>,
19}
20
21impl BoundTableSecurity {
22    /// Retain every ACL path used by authorization, preserving absence and lexical column order.
23    pub fn capture_privileges(
24        &self,
25        control: &ProductionControl<'_>,
26    ) -> Result<Produced<CapturedTablePrivileges>, SQLError> {
27        let output = SyntaxProduction::new(*control);
28        let acl = self
29            .acl
30            .as_deref()
31            .map(|acl| copy_acl(acl, &output))
32            .transpose()?;
33        let mut columns = output.vector(self.column_acls.len())?;
34        for (name, entries) in &self.column_acls {
35            let name = output.text(name)?;
36            let entries = copy_acl(entries, &output)?;
37            columns.push((name, entries))?;
38        }
39        let captured = CapturedTablePrivileges {
40            role_owner: self.role_owner,
41            acl,
42            column_acls: columns.finish()?,
43        };
44        output.finish(captured)
45    }
46}
47
48impl CapturedTablePrivileges {
49    pub fn resolve(
50        &self,
51        roles: &BTreeMap<String, RoleDefinition>,
52    ) -> Result<TableSecurity, String> {
53        super::resolve_privileges(
54            self.role_owner,
55            self.acl.as_deref(),
56            self.column_acls.iter().map(|(name, acl)| (name, acl)),
57            roles,
58        )
59    }
60}
61
62fn copy_acl(
63    entries: &[BoundTableAclEntry],
64    output: &SyntaxProduction<'_>,
65) -> Result<Vec<BoundTableAclEntry>, SQLError> {
66    let mut copied = output.vector(entries.len())?;
67    for entry in entries {
68        output.check()?;
69        copied.push(entry.clone())?;
70    }
71    copied.finish()
72}