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

use tera::{Tera,Context};
#[cfg(feature = "casbin")]
use casbin::{CoreApi,Enforcer};
#[cfg(feature = "casbin")]
use std::{
    path::Path,
    sync::Arc,
};
use tokio::sync::RwLock;

use crate::defs::{
    Config,
    SessionStoreDB,
    UserStore,
};
#[cfg(feature = "authstore")]
use crate::defs::AuthStore;

#[derive(Clone)]
pub struct AppDBs {
    pub config: Config,
    #[cfg(feature = "authstore")]
    pub auth_store: AuthStore,
    #[cfg(feature = "casbin")]
    pub enforcer: Arc<RwLock<Enforcer>>,
    pub sessions_store_db: SessionStoreDB,
    pub user_store: UserStore,
    pub tera: Tera,
    pub context: Context,
}
impl AppDBs {
    #[cfg(feature = "authstore")]
    pub fn new(config: &Config, store: SessionStoreDB, user_store: UserStore, tera: Tera, context: Context) -> Self {
        Self {
            config: config.to_owned(),
            auth_store: AuthStore::new(&config.authz_store_uri),
            sessions_store_db: store,
            user_store,
            tera,
            context,
        }
    } 
    #[cfg(feature = "casbin")]
    pub fn new(config: &Config, store: SessionStoreDB, user_store: UserStore, enforcer: Arc<RwLock<Enforcer>>, tera: Tera, context: Context) -> Self {
        Self {
            config: config.to_owned(),
            enforcer,
            sessions_store_db: store,
            user_store,
            tera,
            context,
        }
    } 
    #[cfg(feature = "casbin")]
    pub async fn create_enforcer(model_path: &'static str, policy_path: &'static str) -> Arc<RwLock<Enforcer>> {
        if ! Path::new(&model_path).exists() {
            panic!("model path: {} not exists",&model_path);
        }
        if ! Path::new(&policy_path).exists() {
            panic!("policy path: {} not exists",&policy_path);
        }
        let e = Enforcer::new(model_path, policy_path)
            .await
            .expect("can read casbin model and policy files");
        Arc::new(RwLock::new(e))
  }
}