Skip to main content

uqa_sql/catalog/security/schema/
production.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Read persisted schema ACLs without materializing names or unowned traversal scratch.
8
9use super::{RoleAttribute, SchemaAclPrivilege};
10use crate::{
11    catalog::{
12        roles::{
13            identity::RoleSubject, memberships::role_inherits_with_control, RoleDefinition,
14            RoleIdentity, RoleMembership, RoleMembershipKey,
15        },
16        security::BoundSchemaSecurity,
17    },
18    SQLError,
19};
20use std::collections::BTreeMap;
21use uqa_core::{
22    catalog_role::BoundAclEntry, catalog_schema::SchemaPrivileges, memory::ProductionControl,
23};
24
25/// A retained registry row or a stack-backed builtin ACL has the same privilege rules.
26pub struct SchemaPrivilegeView<'a> {
27    pub role_owner: RoleIdentity,
28    pub acl: Option<&'a [BoundAclEntry<SchemaPrivileges>]>,
29}
30
31impl<'a> From<&'a BoundSchemaSecurity> for SchemaPrivilegeView<'a> {
32    fn from(security: &'a BoundSchemaSecurity) -> Self {
33        Self {
34            role_owner: security.role_owner,
35            acl: security.acl.as_deref(),
36        }
37    }
38}
39
40pub fn role_has_schema_privilege_with_control(
41    security: SchemaPrivilegeView<'_>,
42    subject: &(impl RoleSubject + ?Sized),
43    privilege: SchemaAclPrivilege,
44    roles: &BTreeMap<String, RoleDefinition>,
45    memberships: &BTreeMap<RoleMembershipKey, RoleMembership>,
46    control: &ProductionControl<'_>,
47) -> Result<bool, SQLError> {
48    // Ordinary namespace lookup resolves the complete ACL before considering even PUBLIC or superuser access. Invalid persisted references therefore keep the namespace unusable.
49    require_role(security.role_owner, roles, control)?;
50    for entry in security.acl.into_iter().flatten() {
51        control.check()?;
52        if let Some(role) = entry.role {
53            require_role(role, roles, control)?;
54        }
55        require_role(entry.grantor, roles, control)?;
56    }
57    if subject
58        .role_definition_with_control(roles, control)?
59        .is_some_and(|role| role.has(RoleAttribute::Superuser))
60    {
61        return Ok(true);
62    }
63    match security.acl {
64        None => {
65            role_inherits_with_control(roles, memberships, subject, &security.role_owner, control)
66        }
67        Some(acl) => {
68            for entry in acl {
69                control.check()?;
70                if entry.privileges.intersects(privilege.mask())
71                    && match entry.role {
72                        None => true,
73                        Some(role) => {
74                            role_inherits_with_control(roles, memberships, subject, &role, control)?
75                        }
76                    }
77                {
78                    return Ok(true);
79                }
80            }
81            Ok(false)
82        }
83    }
84}
85
86fn require_role(
87    role: RoleIdentity,
88    roles: &BTreeMap<String, RoleDefinition>,
89    control: &ProductionControl<'_>,
90) -> Result<(), SQLError> {
91    control.check()?;
92    if !role.is_valid() {
93        return Err(SQLError::Internal(
94            "invalid persisted schema role identity".into(),
95        ));
96    }
97    if role.role_definition_with_control(roles, control)?.is_none() {
98        return Err(SQLError::Internal(format!(
99            "persisted schema privileges reference missing role incarnation {}",
100            role.oid
101        )));
102    }
103    Ok(())
104}
105
106#[cfg(test)]
107mod tests;