1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::{
    Adapter, Effector, EnforceArgs, Event, EventEmitter, Filter, Model, Result,
    RoleManager, TryIntoAdapter, TryIntoModel,
};

#[cfg(feature = "watcher")]
use crate::Watcher;

#[cfg(feature = "logging")]
use crate::Logger;

#[cfg(feature = "incremental")]
use crate::emitter::EventData;

use async_trait::async_trait;
use rhai::ImmutableString;

use std::sync::{Arc, RwLock};

#[async_trait]
pub trait CoreApi: Send + Sync {
    async fn new_raw<M: TryIntoModel, A: TryIntoAdapter>(
        m: M,
        a: A,
    ) -> Result<Self>
    where
        Self: Sized;
    async fn new<M: TryIntoModel, A: TryIntoAdapter>(
        m: M,
        a: A,
    ) -> Result<Self>
    where
        Self: Sized;
    fn add_function(
        &mut self,
        fname: &str,
        f: fn(ImmutableString, ImmutableString) -> bool,
    );
    fn get_model(&self) -> &dyn Model;
    fn get_mut_model(&mut self) -> &mut dyn Model;
    fn get_adapter(&self) -> &dyn Adapter;
    fn get_mut_adapter(&mut self) -> &mut dyn Adapter;
    #[cfg(feature = "watcher")]
    fn set_watcher(&mut self, w: Box<dyn Watcher>);
    #[cfg(feature = "watcher")]
    fn get_watcher(&self) -> Option<&dyn Watcher>;
    #[cfg(feature = "watcher")]
    fn get_mut_watcher(&mut self) -> Option<&mut dyn Watcher>;
    fn get_role_manager(&self) -> Arc<RwLock<dyn RoleManager>>;
    fn set_role_manager(
        &mut self,
        rm: Arc<RwLock<dyn RoleManager>>,
    ) -> Result<()>;
    #[cfg(feature = "logging")]
    fn get_logger(&self) -> &dyn Logger;
    #[cfg(feature = "logging")]
    fn set_logger(&mut self, logger: Box<dyn Logger>);
    async fn set_model<M: TryIntoModel>(&mut self, m: M) -> Result<()>
    where
        Self: Sized;
    async fn set_adapter<A: TryIntoAdapter>(&mut self, a: A) -> Result<()>
    where
        Self: Sized;
    fn set_effector(&mut self, e: Box<dyn Effector>);
    fn enforce<ARGS: EnforceArgs>(&self, rvals: ARGS) -> Result<bool>
    where
        Self: Sized;
    fn enforce_mut<ARGS: EnforceArgs>(&mut self, rvals: ARGS) -> Result<bool>
    where
        Self: Sized;
    fn build_role_links(&mut self) -> Result<()>;
    #[cfg(feature = "incremental")]
    fn build_incremental_role_links(&mut self, d: EventData) -> Result<()>;
    async fn load_policy(&mut self) -> Result<()>;
    async fn load_filtered_policy<'a>(&mut self, f: Filter<'a>) -> Result<()>;
    fn is_filtered(&self) -> bool;
    fn is_enabled(&self) -> bool;
    async fn save_policy(&mut self) -> Result<()>;
    async fn clear_policy(&mut self) -> Result<()>;
    #[cfg(feature = "logging")]
    fn enable_log(&mut self, enabled: bool);
    fn enable_auto_save(&mut self, auto_save: bool);
    fn enable_enforce(&mut self, enabled: bool);
    fn enable_auto_build_role_links(&mut self, auto_build_role_links: bool);
    #[cfg(feature = "watcher")]
    fn enable_auto_notify_watcher(&mut self, auto_notify_watcher: bool);
    fn has_auto_save_enabled(&self) -> bool;
    #[cfg(feature = "watcher")]
    fn has_auto_notify_watcher_enabled(&self) -> bool;
    fn has_auto_build_role_links_enabled(&self) -> bool;
}

pub trait IEnforcer: CoreApi + EventEmitter<Event> {}

impl<T> IEnforcer for T where T: CoreApi + EventEmitter<Event> {}