casdoor_sdk_rust/authz/
models.rs

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::{utils::null_to_default, Model};

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct Enforcer {
    pub owner: String,
    pub name: String,
    pub display_name: String,
    pub description: String,
    pub model: String,
    pub adapter: String,
    #[serde(deserialize_with = "null_to_default")]
    pub model_cfg: HashMap<String, String>,
    pub created_time: String,
    pub updated_time: String,
}
impl Model for Enforcer {
    fn ident() -> &'static str {
        "enforcer"
    }
    fn plural_ident() -> &'static str {
        "enforcers"
    }
    fn support_update_columns() -> bool {
        false
    }
    fn owner(&self) -> &str {
        &self.owner
    }
    fn name(&self) -> &str {
        &self.name
    }
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase", default)]
pub struct Permission {
    #[serde(deserialize_with = "null_to_default")]
    pub actions: Vec<String>,
    pub adapter: String,
    pub approve_time: String,
    pub approver: String,
    pub created_time: String,
    pub description: String,
    pub display_name: String,
    #[serde(deserialize_with = "null_to_default")]
    pub domains: Vec<String>,
    pub effect: String,
    #[serde(deserialize_with = "null_to_default")]
    pub groups: Vec<String>,
    pub is_enabled: bool,
    pub model: String,
    pub name: String,
    pub owner: String,
    pub resource_type: String,
    #[serde(deserialize_with = "null_to_default")]
    pub resources: Vec<String>,
    #[serde(deserialize_with = "null_to_default")]
    pub roles: Vec<String>,
    pub state: String,
    pub submitter: String,
    #[serde(deserialize_with = "null_to_default")]
    pub users: Vec<String>,
}
impl Model for Permission {
    fn ident() -> &'static str {
        "permission"
    }
    fn plural_ident() -> &'static str {
        "permissions"
    }
    fn support_update_columns() -> bool {
        false
    }
    fn owner(&self) -> &str {
        &self.owner
    }
    fn name(&self) -> &str {
        &self.name
    }
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase", default)]
pub struct Role {
    pub created_time: String,
    pub description: String,
    pub display_name: String,
    #[serde(deserialize_with = "null_to_default")]
    pub domains: Vec<String>,
    #[serde(deserialize_with = "null_to_default")]
    pub groups: Vec<String>,
    pub is_enabled: bool,
    pub name: String,
    pub owner: String,
    #[serde(deserialize_with = "null_to_default")]
    pub roles: Vec<String>,
    #[serde(deserialize_with = "null_to_default")]
    pub users: Vec<String>,
}
impl Model for Role {
    fn ident() -> &'static str {
        "role"
    }
    fn plural_ident() -> &'static str {
        "roles"
    }
    fn support_update_columns() -> bool {
        false
    }
    fn owner(&self) -> &str {
        &self.owner
    }
    fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EnforceQueryArgs {
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_id: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model_id: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_id: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforcer_id: Option<String>,
}

pub type CasbinRequest = Vec<String>;


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EnforceArgs {
    pub query: EnforceQueryArgs,
    pub casbin_request: CasbinRequest,
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BatchEnforceQueryArgs {
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_id: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model_id: Option<String>,
    
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforcer_id: Option<String>,
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BatchEnforceArgs {
    pub query: BatchEnforceQueryArgs,
    pub casbin_requests: Vec<CasbinRequest>,
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EnforceResult {
    pub allow: bool,
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BatchEnforceResult {
    pub allow_list: Vec<bool>,
}


#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "PascalCase", default)]
pub struct CasbinRule {
    pub id: i64,
    pub ptype: String,
    pub v0: String,
    pub v1: String,
    pub v2: String,
    pub v3: String,
    pub v4: String,
    pub v5: String,
}

pub struct Filter {
    pub ptype: Vec<String>,
    pub v0: Vec<String>,
    pub v1: Vec<String>,
    pub v2: Vec<String>,
    pub v3: Vec<String>,
    pub v4: Vec<String>,
    pub v5: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_enforce_query_args() {
        let mut args = EnforceQueryArgs::default();
        let query_part = serde_urlencoded::to_string(&args).unwrap();
        assert_eq!("", query_part);

        args.permission_id = Some("0".to_owned());
        args.model_id = Some("1".to_owned());
        let query_part = serde_urlencoded::to_string(&args).unwrap();
        assert_eq!("permissionId=0&modelId=1", query_part);
    }
}