1use crate::{core_api::IEnforcer, Result};
2
3#[cfg(any(
4 feature = "watcher",
5 feature = "cached",
6 feature = "logging",
7 feature = "incremental"
8))]
9use crate::emitter::EventData;
10
11#[cfg(any(feature = "watcher", feature = "cached", feature = "logging",))]
12use crate::emitter::Event;
13
14use async_trait::async_trait;
15
16#[async_trait]
17pub trait InternalApi: IEnforcer {
18 async fn add_policy_internal(
19 &mut self,
20 sec: &str,
21 ptype: &str,
22 rule: Vec<String>,
23 ) -> Result<bool>;
24 async fn add_policies_internal(
25 &mut self,
26 sec: &str,
27 ptype: &str,
28 rules: Vec<Vec<String>>,
29 ) -> Result<bool>;
30 async fn remove_policy_internal(
31 &mut self,
32 sec: &str,
33 ptype: &str,
34 rule: Vec<String>,
35 ) -> Result<bool>;
36 async fn remove_policies_internal(
37 &mut self,
38 sec: &str,
39 ptype: &str,
40 rules: Vec<Vec<String>>,
41 ) -> Result<bool>;
42 async fn remove_filtered_policy_internal(
43 &mut self,
44 sec: &str,
45 ptype: &str,
46 field_index: usize,
47 field_values: Vec<String>,
48 ) -> Result<(bool, Vec<Vec<String>>)>;
49}
50
51#[async_trait]
52impl<T> InternalApi for T
53where
54 T: IEnforcer,
55{
56 async fn add_policy_internal(
57 &mut self,
58 sec: &str,
59 ptype: &str,
60 rule: Vec<String>,
61 ) -> Result<bool> {
62 if self.has_auto_save_enabled()
63 && !self
64 .get_mut_adapter()
65 .add_policy(sec, ptype, rule.clone())
66 .await?
67 {
68 return Ok(false);
69 }
70
71 let rule_added = self.get_mut_model().add_policy(sec, ptype, {
72 #[cfg(any(
73 feature = "watcher",
74 feature = "logging",
75 feature = "incremental"
76 ))]
77 {
78 rule.clone()
79 }
80 #[cfg(all(
81 not(feature = "watcher"),
82 not(feature = "logging"),
83 not(feature = "incremental")
84 ))]
85 {
86 rule
87 }
88 });
89 #[cfg(any(feature = "watcher", feature = "logging"))]
90 {
91 let event_data =
92 EventData::AddPolicy(sec.to_owned(), ptype.to_owned(), {
93 #[cfg(feature = "incremental")]
94 {
95 rule.clone()
96 }
97 #[cfg(not(feature = "incremental"))]
98 {
99 rule
100 }
101 });
102 #[cfg(feature = "watcher")]
103 {
104 if rule_added && self.has_auto_notify_watcher_enabled() {
105 self.emit(Event::PolicyChange, event_data);
106 }
107 }
108 #[cfg(not(feature = "watcher"))]
109 {
110 if rule_added {
111 self.emit(Event::PolicyChange, event_data);
112 }
113 }
114 }
115 #[cfg(feature = "cached")]
116 {
117 if rule_added {
118 self.emit(Event::ClearCache, EventData::ClearCache);
119 }
120 }
121 if sec != "g" || !self.has_auto_build_role_links_enabled() {
122 return Ok(rule_added);
123 }
124 #[cfg(not(feature = "incremental"))]
125 {
126 self.build_role_links()?;
127 }
128 #[cfg(feature = "incremental")]
129 {
130 self.build_incremental_role_links(EventData::AddPolicy(
131 sec.to_owned(),
132 ptype.to_owned(),
133 rule,
134 ))?;
135 }
136
137 Ok(rule_added)
138 }
139
140 async fn add_policies_internal(
141 &mut self,
142 sec: &str,
143 ptype: &str,
144 rules: Vec<Vec<String>>,
145 ) -> Result<bool> {
146 if self.has_auto_save_enabled()
147 && !self
148 .get_mut_adapter()
149 .add_policies(sec, ptype, rules.clone())
150 .await?
151 {
152 return Ok(false);
153 }
154
155 let rules_added = self.get_mut_model().add_policies(sec, ptype, {
156 #[cfg(any(
157 feature = "watcher",
158 feature = "logging",
159 feature = "incremental"
160 ))]
161 {
162 rules.clone()
163 }
164 #[cfg(all(
165 not(feature = "watcher"),
166 not(feature = "logging"),
167 not(feature = "incremental")
168 ))]
169 {
170 rules
171 }
172 });
173 #[cfg(any(feature = "watcher", feature = "logging"))]
174 {
175 let event_data =
176 EventData::AddPolicies(sec.to_owned(), ptype.to_owned(), {
177 #[cfg(feature = "incremental")]
178 {
179 rules.clone()
180 }
181 #[cfg(not(feature = "incremental"))]
182 {
183 rules
184 }
185 });
186 #[cfg(feature = "watcher")]
187 {
188 if rules_added && self.has_auto_notify_watcher_enabled() {
189 self.emit(Event::PolicyChange, event_data);
190 }
191 }
192 #[cfg(not(feature = "watcher"))]
193 {
194 if rules_added {
195 self.emit(Event::PolicyChange, event_data);
196 }
197 }
198 }
199 #[cfg(feature = "cached")]
200 {
201 if rules_added {
202 self.emit(Event::ClearCache, EventData::ClearCache);
203 }
204 }
205 if sec != "g" || !self.has_auto_build_role_links_enabled() {
206 return Ok(rules_added);
207 }
208 #[cfg(not(feature = "incremental"))]
209 {
210 self.build_role_links()?;
211 }
212 #[cfg(feature = "incremental")]
213 {
214 self.build_incremental_role_links(EventData::AddPolicies(
215 sec.to_owned(),
216 ptype.to_owned(),
217 rules,
218 ))?;
219 }
220
221 Ok(rules_added)
222 }
223
224 async fn remove_policy_internal(
225 &mut self,
226 sec: &str,
227 ptype: &str,
228 rule: Vec<String>,
229 ) -> Result<bool> {
230 if self.has_auto_save_enabled()
231 && !self
232 .get_mut_adapter()
233 .remove_policy(sec, ptype, rule.clone())
234 .await?
235 {
236 return Ok(false);
237 }
238
239 let rule_removed = self.get_mut_model().remove_policy(sec, ptype, {
240 #[cfg(any(
241 feature = "watcher",
242 feature = "logging",
243 feature = "incremental"
244 ))]
245 {
246 rule.clone()
247 }
248 #[cfg(all(
249 not(feature = "watcher"),
250 not(feature = "logging"),
251 not(feature = "incremental")
252 ))]
253 {
254 rule
255 }
256 });
257 #[cfg(any(feature = "watcher", feature = "logging"))]
258 {
259 let event_data =
260 EventData::RemovePolicy(sec.to_owned(), ptype.to_owned(), {
261 #[cfg(feature = "incremental")]
262 {
263 rule.clone()
264 }
265 #[cfg(not(feature = "incremental"))]
266 {
267 rule
268 }
269 });
270 #[cfg(feature = "watcher")]
271 {
272 if rule_removed && self.has_auto_notify_watcher_enabled() {
273 self.emit(Event::PolicyChange, event_data);
274 }
275 }
276 #[cfg(not(feature = "watcher"))]
277 {
278 if rule_removed {
279 self.emit(Event::PolicyChange, event_data);
280 }
281 }
282 }
283 #[cfg(feature = "cached")]
284 {
285 if rule_removed {
286 self.emit(Event::ClearCache, EventData::ClearCache);
287 }
288 }
289 if sec != "g" || !self.has_auto_build_role_links_enabled() {
290 return Ok(rule_removed);
291 }
292 #[cfg(not(feature = "incremental"))]
293 {
294 self.build_role_links()?;
295 }
296 #[cfg(feature = "incremental")]
297 {
298 self.build_incremental_role_links(EventData::RemovePolicy(
299 sec.to_owned(),
300 ptype.to_owned(),
301 rule,
302 ))?;
303 }
304
305 Ok(rule_removed)
306 }
307
308 async fn remove_policies_internal(
309 &mut self,
310 sec: &str,
311 ptype: &str,
312 rules: Vec<Vec<String>>,
313 ) -> Result<bool> {
314 if self.has_auto_save_enabled()
315 && !self
316 .get_mut_adapter()
317 .remove_policies(sec, ptype, rules.clone())
318 .await?
319 {
320 return Ok(false);
321 }
322
323 let rules_removed = self.get_mut_model().remove_policies(sec, ptype, {
324 #[cfg(any(
325 feature = "watcher",
326 feature = "logging",
327 feature = "incremental"
328 ))]
329 {
330 rules.clone()
331 }
332 #[cfg(all(
333 not(feature = "watcher"),
334 not(feature = "logging"),
335 not(feature = "incremental")
336 ))]
337 {
338 rules
339 }
340 });
341 #[cfg(any(feature = "watcher", feature = "logging"))]
342 {
343 let event_data =
344 EventData::RemovePolicies(sec.to_owned(), ptype.to_owned(), {
345 #[cfg(feature = "incremental")]
346 {
347 rules.clone()
348 }
349 #[cfg(not(feature = "incremental"))]
350 {
351 rules
352 }
353 });
354 #[cfg(feature = "watcher")]
355 {
356 if rules_removed && self.has_auto_notify_watcher_enabled() {
357 self.emit(Event::PolicyChange, event_data);
358 }
359 }
360 #[cfg(not(feature = "watcher"))]
361 {
362 if rules_removed {
363 self.emit(Event::PolicyChange, event_data);
364 }
365 }
366 }
367 #[cfg(feature = "cached")]
368 {
369 if rules_removed {
370 self.emit(Event::ClearCache, EventData::ClearCache);
371 }
372 }
373 if sec != "g" || !self.has_auto_build_role_links_enabled() {
374 return Ok(rules_removed);
375 }
376 #[cfg(not(feature = "incremental"))]
377 {
378 self.build_role_links()?;
379 }
380 #[cfg(feature = "incremental")]
381 {
382 self.build_incremental_role_links(EventData::RemovePolicies(
383 sec.to_owned(),
384 ptype.to_owned(),
385 rules,
386 ))?;
387 }
388
389 Ok(rules_removed)
390 }
391
392 async fn remove_filtered_policy_internal(
393 &mut self,
394 sec: &str,
395 ptype: &str,
396 field_index: usize,
397 field_values: Vec<String>,
398 ) -> Result<(bool, Vec<Vec<String>>)> {
399 if self.has_auto_save_enabled()
400 && !self
401 .get_mut_adapter()
402 .remove_filtered_policy(
403 sec,
404 ptype,
405 field_index,
406 field_values.clone(),
407 )
408 .await?
409 {
410 return Ok((false, vec![]));
411 }
412
413 let (rules_removed, rules) = self
414 .get_mut_model()
415 .remove_filtered_policy(sec, ptype, field_index, field_values);
416 #[cfg(any(feature = "watcher", feature = "logging"))]
417 {
418 let event_data = EventData::RemoveFilteredPolicy(
419 sec.to_owned(),
420 ptype.to_owned(),
421 rules.clone(),
422 );
423 #[cfg(feature = "watcher")]
424 {
425 if rules_removed && self.has_auto_notify_watcher_enabled() {
426 self.emit(Event::PolicyChange, event_data);
427 }
428 }
429 #[cfg(not(feature = "watcher"))]
430 {
431 if rules_removed {
432 self.emit(Event::PolicyChange, event_data);
433 }
434 }
435 }
436 #[cfg(feature = "cached")]
437 {
438 if rules_removed {
439 self.emit(Event::ClearCache, EventData::ClearCache);
440 }
441 }
442 if sec != "g" || !self.has_auto_build_role_links_enabled() {
443 return Ok((rules_removed, rules));
444 }
445 #[cfg(not(feature = "incremental"))]
446 {
447 self.build_role_links()?;
448 }
449 #[cfg(feature = "incremental")]
450 {
451 self.build_incremental_role_links(
452 EventData::RemoveFilteredPolicy(
453 sec.to_owned(),
454 ptype.to_owned(),
455 rules.clone(),
456 ),
457 )?;
458 }
459
460 Ok((rules_removed, rules))
461 }
462}