1use super::privileges::TargetSelectPrivilegeRequest;
10use crate::{
11 catalog::{roles::RoleReference, security::table::TableAclPrivilege, stored_view::StoredView},
12 plan::{DeletePlan, InsertPlan, MergePlan, UpdatePlan},
13 SQLError,
14};
15use uqa_core::memory::{Produced, ProductionControl};
16
17mod production;
18pub use production::{
19 ensure_delete_with_control, ensure_insert_with_control, ensure_merge_with_control,
20 ensure_update_with_control, merge_privilege_expressions_with_control,
21 next_layer_privilege_subject_with_control,
22};
23
24pub fn next_layer_privilege_subject(
26 services: &dyn ViewPrivilegeCatalog,
27 name: &str,
28) -> Result<RoleReference, SQLError> {
29 Ok(next_layer_privilege_subject_with_control(
30 services,
31 name,
32 &ProductionControl::uncontrolled(),
33 )?
34 .into_uncontrolled()
35 .expect("ordinary view privilege subject"))
36}
37
38pub fn ensure_insert(
39 services: &dyn ViewPrivilegeCatalog,
40 statement: &InsertPlan,
41) -> Result<RoleReference, SQLError> {
42 Ok(
43 ensure_insert_with_control(services, statement, &ProductionControl::uncontrolled())?
44 .into_uncontrolled()
45 .expect("ordinary view privilege subject"),
46 )
47}
48
49pub fn ensure_update(
50 services: &dyn ViewPrivilegeCatalog,
51 statement: &UpdatePlan,
52) -> Result<RoleReference, SQLError> {
53 Ok(
54 ensure_update_with_control(services, statement, &ProductionControl::uncontrolled())?
55 .into_uncontrolled()
56 .expect("ordinary view privilege subject"),
57 )
58}
59
60pub fn ensure_delete(
61 services: &dyn ViewPrivilegeCatalog,
62 statement: &DeletePlan,
63) -> Result<RoleReference, SQLError> {
64 Ok(
65 ensure_delete_with_control(services, statement, &ProductionControl::uncontrolled())?
66 .into_uncontrolled()
67 .expect("ordinary view privilege subject"),
68 )
69}
70
71pub fn ensure_merge(
72 services: &dyn ViewPrivilegeCatalog,
73 statement: &MergePlan,
74) -> Result<RoleReference, SQLError> {
75 Ok(
76 ensure_merge_with_control(services, statement, &ProductionControl::uncontrolled())?
77 .into_uncontrolled()
78 .expect("ordinary view privilege subject"),
79 )
80}
81
82pub fn merge_privilege_expressions(statement: &MergePlan) -> Vec<&crate::ScalarExpr> {
83 merge_privilege_expressions_with_control(statement, &ProductionControl::uncontrolled())
84 .expect("ordinary merge privilege expressions")
85 .into_uncontrolled()
86 .expect("ordinary merge privilege expressions")
87}
88
89pub trait ViewPrivilegeCatalog {
91 fn ensure_target_select_with_control(
92 &self,
93 request: TargetSelectPrivilegeRequest<'_, '_>,
94 control: &ProductionControl<'_>,
95 ) -> Result<(), SQLError> {
96 control.check()?;
97 self.ensure_target_select(request)?;
98 Ok(control.check()?)
99 }
100 fn ensure_view_privilege_for_with_control(
101 &self,
102 name: &str,
103 view: &StoredView,
104 subject: &RoleReference,
105 privilege: TableAclPrivilege,
106 control: &ProductionControl<'_>,
107 ) -> Result<(), SQLError> {
108 control.check()?;
109 self.ensure_view_privilege_for(name, view, subject, privilege)?;
110 Ok(control.check()?)
111 }
112 fn ensure_view_column_privilege_for_with_control(
113 &self,
114 name: &str,
115 view: &StoredView,
116 column: &str,
117 subject: &RoleReference,
118 privilege: TableAclPrivilege,
119 control: &ProductionControl<'_>,
120 ) -> Result<(), SQLError> {
121 control.check()?;
122 self.ensure_view_column_privilege_for(name, view, column, subject, privilege)?;
123 Ok(control.check()?)
124 }
125 fn ensure_any_view_column_privilege_for_with_control(
126 &self,
127 name: &str,
128 view: &StoredView,
129 subject: &RoleReference,
130 privilege: TableAclPrivilege,
131 control: &ProductionControl<'_>,
132 ) -> Result<(), SQLError> {
133 control.check()?;
134 self.ensure_any_view_column_privilege_for(name, view, subject, privilege)?;
135 Ok(control.check()?)
136 }
137
138 fn with_view_definition_with_control(
140 &self,
141 name: &str,
142 control: &ProductionControl<'_>,
143 read: &mut dyn FnMut(Option<&StoredView>) -> Result<(), SQLError>,
144 ) -> Result<(), SQLError> {
145 control.check()?;
146 let view = self.view_definition(name)?;
147 read(view.as_ref())?;
148 Ok(control.check()?)
149 }
150 fn ensure_query_select_with_control(
151 &self,
152 query: &crate::plan::QueryPlan,
153 control: &ProductionControl<'_>,
154 ) -> Result<(), SQLError> {
155 control.check()?;
156 self.ensure_query_select(query)?;
157 Ok(control.check()?)
158 }
159 fn current_role_with_control(
160 &self,
161 control: &ProductionControl<'_>,
162 ) -> Result<Produced<RoleReference>, SQLError> {
163 control.check()?;
164 self.current_role().clone_with_control(control)
165 }
166 fn bound_role_with_control(
167 &self,
168 identity: crate::catalog::roles::RoleIdentity,
169 control: &ProductionControl<'_>,
170 ) -> Result<Produced<RoleReference>, SQLError> {
171 control.check()?;
172 self.bound_role(identity)?.clone_with_control(control)
173 }
174 fn ensure_query_select(&self, query: &crate::plan::QueryPlan) -> Result<(), SQLError>;
175 fn view_definition(&self, name: &str) -> Result<Option<StoredView>, SQLError>;
176 fn current_role(&self) -> RoleReference;
177 fn bound_role(
178 &self,
179 identity: crate::catalog::roles::RoleIdentity,
180 ) -> Result<RoleReference, SQLError>;
181 fn ensure_view_privilege_for(
182 &self,
183 name: &str,
184 view: &StoredView,
185 subject: &RoleReference,
186 privilege: TableAclPrivilege,
187 ) -> Result<(), SQLError>;
188 fn ensure_view_column_privilege_for(
189 &self,
190 name: &str,
191 view: &StoredView,
192 column: &str,
193 subject: &RoleReference,
194 privilege: TableAclPrivilege,
195 ) -> Result<(), SQLError>;
196 fn ensure_any_view_column_privilege_for(
197 &self,
198 name: &str,
199 view: &StoredView,
200 subject: &RoleReference,
201 privilege: TableAclPrivilege,
202 ) -> Result<(), SQLError>;
203 fn ensure_target_select(
204 &self,
205 request: TargetSelectPrivilegeRequest<'_, '_>,
206 ) -> Result<(), SQLError>;
207}
208
209#[cfg(test)]
210mod tests;