Skip to main content

casdoor_rust_sdk/
role.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 serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::serde_util::null_to_default;
20
21/// Role has the same definition as
22/// <https://github.com/casdoor/casdoor/blob/master/object/role.go>.
23#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", default)]
25pub struct Role {
26    pub owner: String,
27    pub name: String,
28    pub created_time: String,
29    pub display_name: String,
30    pub description: String,
31
32    #[serde(deserialize_with = "null_to_default")]
33    pub users: Vec<String>,
34    #[serde(deserialize_with = "null_to_default")]
35    pub groups: Vec<String>,
36    #[serde(deserialize_with = "null_to_default")]
37    pub roles: Vec<String>,
38    #[serde(deserialize_with = "null_to_default")]
39    pub domains: Vec<String>,
40    pub is_enabled: bool,
41    #[serde(deserialize_with = "null_to_default")]
42    pub source_groups: Vec<String>,
43}
44
45impl Client {
46    /// Get the roles of the client's organization.
47    pub async fn get_roles(&self) -> Result<Vec<Role>> {
48        self.get_list("get-roles", &[("owner", &self.config.organization_name)])
49            .await
50    }
51
52    /// Get one page of the roles of the client's organization, together with the total
53    /// number of the roles.
54    pub async fn get_pagination_roles(
55        &self,
56        p: i32,
57        page_size: i32,
58        query: &[(&str, &str)],
59    ) -> Result<(Vec<Role>, i32)> {
60        self.get_pagination(
61            "get-roles",
62            &self.config.organization_name,
63            p,
64            page_size,
65            query,
66        )
67        .await
68    }
69
70    /// Get one role by name.
71    pub async fn get_role(&self, name: &str) -> Result<Option<Role>> {
72        self.get_object("get-role", &[("id", &self.get_id(name))])
73            .await
74    }
75
76    /// Add a role.
77    pub async fn add_role(&self, role: &Role) -> Result<bool> {
78        Ok(self.modify_role("add-role", role, &[]).await?.1)
79    }
80
81    /// Update a role.
82    pub async fn update_role(&self, role: &Role) -> Result<bool> {
83        Ok(self.modify_role("update-role", role, &[]).await?.1)
84    }
85
86    /// Update only the given columns of a role.
87    pub async fn update_role_for_columns(&self, role: &Role, columns: &[&str]) -> Result<bool> {
88        Ok(self.modify_role("update-role", role, columns).await?.1)
89    }
90
91    /// Delete a role.
92    pub async fn delete_role(&self, role: &Role) -> Result<bool> {
93        Ok(self.modify_role("delete-role", role, &[]).await?.1)
94    }
95
96    async fn modify_role(
97        &self,
98        action: &str,
99        role: &Role,
100        columns: &[&str],
101    ) -> Result<(Response, bool)> {
102        let mut role = role.clone();
103        role.owner = get_owner(&role.owner, &self.config.organization_name);
104
105        let id = format!("{}/{}", role.owner, role.name);
106        self.modify_object(action, &id, &role, columns).await
107    }
108}