Skip to main content

casdoor_rust_sdk/
enforcer.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16
17use serde::{Deserialize, Serialize};
18
19use crate::client::{get_owner, Client, Response};
20use crate::error::Result;
21use crate::serde_util::null_to_default;
22
23/// Enforcer has the same definition as
24/// <https://github.com/casdoor/casdoor/blob/master/object/enforcer.go>.
25#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase", default)]
27pub struct Enforcer {
28    pub owner: String,
29    pub name: String,
30    pub created_time: String,
31    pub updated_time: String,
32    pub display_name: String,
33    pub description: String,
34
35    pub model: String,
36    pub adapter: String,
37
38    #[serde(deserialize_with = "null_to_default")]
39    pub model_cfg: HashMap<String, String>,
40}
41
42impl Client {
43    /// Get the enforcers of the client's organization.
44    pub async fn get_enforcers(&self) -> Result<Vec<Enforcer>> {
45        self.get_list(
46            "get-enforcers",
47            &[("owner", &self.config.organization_name)],
48        )
49        .await
50    }
51
52    /// Get one page of the enforcers of the client's organization, together with the total
53    /// number of the enforcers.
54    pub async fn get_pagination_enforcers(
55        &self,
56        p: i32,
57        page_size: i32,
58        query: &[(&str, &str)],
59    ) -> Result<(Vec<Enforcer>, i32)> {
60        self.get_pagination(
61            "get-enforcers",
62            &self.config.organization_name,
63            p,
64            page_size,
65            query,
66        )
67        .await
68    }
69
70    /// Get one enforcer by name.
71    pub async fn get_enforcer(&self, name: &str) -> Result<Option<Enforcer>> {
72        self.get_object("get-enforcer", &[("id", &self.get_id(name))])
73            .await
74    }
75
76    /// Add an enforcer.
77    pub async fn add_enforcer(&self, enforcer: &Enforcer) -> Result<bool> {
78        Ok(self.modify_enforcer("add-enforcer", enforcer, &[]).await?.1)
79    }
80
81    /// Update an enforcer.
82    pub async fn update_enforcer(&self, enforcer: &Enforcer) -> Result<bool> {
83        Ok(self
84            .modify_enforcer("update-enforcer", enforcer, &[])
85            .await?
86            .1)
87    }
88
89    /// Delete an enforcer.
90    pub async fn delete_enforcer(&self, enforcer: &Enforcer) -> Result<bool> {
91        Ok(self
92            .modify_enforcer("delete-enforcer", enforcer, &[])
93            .await?
94            .1)
95    }
96
97    async fn modify_enforcer(
98        &self,
99        action: &str,
100        enforcer: &Enforcer,
101        columns: &[&str],
102    ) -> Result<(Response, bool)> {
103        let mut enforcer = enforcer.clone();
104        enforcer.owner = get_owner(&enforcer.owner, &self.config.organization_name);
105
106        let id = format!("{}/{}", enforcer.owner, enforcer.name);
107        self.modify_object(action, &id, &enforcer, columns).await
108    }
109}