uqa_engine/capabilities/
database_privileges.rs1use crate::Engine;
10use uqa_sql::catalog::roles::identity::RoleSubject;
11use uqa_sql::{
12 catalog::security::{
13 database::DatabaseAclPrivilege,
14 database_inquiry::{
15 DatabasePrivilegeCatalog, DatabasePrivilegeInquiry, DatabaseSecurityRead,
16 },
17 },
18 SQLError,
19};
20
21impl DatabasePrivilegeCatalog for Engine {
22 fn refresh_privilege_catalog(&self) -> Result<(), SQLError> {
23 self.synchronize_catalog_registries().map_err(|error| {
24 SQLError::Internal(format!("load database privileges for inquiry: {error}"))
25 })
26 }
27 fn security(&self) -> DatabaseSecurityRead<'_> {
28 Box::new(self.durable.database_security.read())
29 }
30}
31impl Engine {
32 pub(crate) fn database_privilege_inquiry(&self) -> DatabasePrivilegeInquiry<'_> {
33 DatabasePrivilegeInquiry {
34 catalog: self,
35 names: self,
36 roles: self,
37 }
38 }
39 pub(crate) fn ensure_database_privilege(
40 &self,
41 role: &(impl RoleSubject + ?Sized),
42 privilege: DatabaseAclPrivilege,
43 ) -> Result<(), SQLError> {
44 self.database_privilege_inquiry()
45 .ensure_database_privilege(role, privilege)
46 }
47}
48
49use uqa_execution::catalog::security::database_lifecycle::{
50 self, DatabasePrivilegeContext, DatabasePrivilegePublication, DatabaseSecurityRegistry,
51 DatabaseSecurityWrite, DATABASE_SECURITY_METADATA_KEY,
52};
53use uqa_storage::{CatalogFacade, StorageBackendResult};
54
55impl DatabaseSecurityRegistry for Engine {
56 fn security_read(&self) -> DatabaseSecurityRead<'_> {
57 Box::new(self.durable.database_security.read())
58 }
59 fn security_write(&self) -> DatabaseSecurityWrite<'_> {
60 Box::new(self.durable.database_security.write())
61 }
62}
63impl DatabasePrivilegePublication for Engine {
64 fn prepare_writer(&self) -> Result<(), SQLError> {
65 self.prepare_explicit_transaction_writer().map(|_| ())
66 }
67 fn refresh_catalog(&self) -> StorageBackendResult<()> {
68 self.synchronize_catalog_registries()
69 }
70 fn persist_security(&self, json: &str) -> Result<(), SQLError> {
71 let Some(catalog) = self.storage.catalog.as_ref() else {
72 return Ok(());
73 };
74 catalog
75 .set_metadata(DATABASE_SECURITY_METADATA_KEY, json)
76 .map_err(|error| SQLError::Internal(format!("persist database privileges: {error}")))
77 }
78
79 fn catalog_changed(&self) {
80 self.note_catalog_registry_changed();
81 }
82 fn notice(&self, notice: uqa_sql::SQLNotice) {
83 self.push_sql_notice(notice);
84 }
85}
86impl Engine {
87 pub(crate) fn database_privilege_context(&self) -> DatabasePrivilegeContext<'_> {
88 DatabasePrivilegeContext {
89 locks: self,
90 names: self,
91 roles: self,
92 registry: self,
93 publication: self,
94 }
95 }
96 pub(crate) fn restore_database_security_from_metadata(
97 &self,
98 catalog: &dyn CatalogFacade,
99 allow_migration: bool,
100 ) -> StorageBackendResult<()> {
101 database_lifecycle::restore_database_security_from_metadata(
102 &self.database_privilege_context(),
103 catalog,
104 allow_migration,
105 )
106 }
107}