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
// Copyright 2026 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::client::{Client, PostBody, Response};
use crate::error::{CasdoorError, Result};
/// PermissionRule has the same definition as
/// <https://github.com/casdoor/casdoor/blob/master/object/permission_rule.go>.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct PermissionRule {
pub ptype: String,
pub v0: String,
pub v1: String,
pub v2: String,
pub v3: String,
pub v4: String,
pub v5: String,
pub id: String,
}
/// A Casbin request, such as `["alice", "data1", "read"]`.
pub type CasbinRequest = Vec<Value>;
impl Client {
/// Check whether a Casbin request is allowed by a permission, a model, a resource or
/// an enforcer. Only one of `permission_id`, `model_id`, `resource_id` and
/// `enforcer_id` needs to be given, the others can be empty.
pub async fn enforce(
&self,
permission_id: &str,
model_id: &str,
resource_id: &str,
enforcer_id: &str,
owner: &str,
casbin_request: &CasbinRequest,
) -> Result<bool> {
let post_bytes = serde_json::to_vec(casbin_request)?;
let response = self
.do_enforce(
"enforce",
permission_id,
model_id,
resource_id,
enforcer_id,
owner,
post_bytes,
)
.await?;
let results = response.data.as_array().ok_or(CasdoorError::InvalidData)?;
for result in results {
let is_allowed = result.as_bool().ok_or(CasdoorError::InvalidData)?;
if is_allowed {
return Ok(true);
}
}
Ok(false)
}
/// Check a batch of Casbin requests at once, the returned matrix has one row per
/// permission and one column per request.
pub async fn batch_enforce(
&self,
permission_id: &str,
model_id: &str,
resource_id: &str,
enforcer_id: &str,
owner: &str,
casbin_requests: &[CasbinRequest],
) -> Result<Vec<Vec<bool>>> {
let post_bytes = serde_json::to_vec(casbin_requests)?;
let response = self
.do_enforce(
"batch-enforce",
permission_id,
model_id,
resource_id,
enforcer_id,
owner,
post_bytes,
)
.await?;
serde_json::from_value(response.data).map_err(|_| CasdoorError::InvalidData)
}
#[allow(clippy::too_many_arguments)]
async fn do_enforce(
&self,
action: &str,
permission_id: &str,
model_id: &str,
resource_id: &str,
enforcer_id: &str,
owner: &str,
post_bytes: Vec<u8>,
) -> Result<Response> {
self.do_post(
action,
&[
("permissionId", permission_id),
("modelId", model_id),
("resourceId", resource_id),
("enforcerId", enforcer_id),
("owner", owner),
],
PostBody::Raw(post_bytes),
)
.await
}
}