Skip to main content

casdoor_rust_sdk/
invitation.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;
19
20/// Invitation has the same definition as
21/// <https://github.com/casdoor/casdoor/blob/master/object/invitation.go>.
22#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase", default)]
24pub struct Invitation {
25    pub owner: String,
26    pub name: String,
27    pub created_time: String,
28    pub updated_time: String,
29    pub display_name: String,
30
31    pub code: String,
32    pub is_regexp: bool,
33    pub quota: i32,
34    pub used_count: i32,
35
36    pub application: String,
37    pub username: String,
38    pub email: String,
39    pub phone: String,
40
41    pub signup_group: String,
42    pub default_code: String,
43
44    pub state: String,
45}
46
47impl Invitation {
48    /// Return the Casdoor object ID (`"owner/name"`) of the invitation.
49    pub fn get_id(&self) -> String {
50        format!("{}/{}", self.owner, self.name)
51    }
52}
53
54impl Client {
55    /// Get the invitations of the client's organization.
56    pub async fn get_invitations(&self) -> Result<Vec<Invitation>> {
57        self.get_list(
58            "get-invitations",
59            &[("owner", &self.config.organization_name)],
60        )
61        .await
62    }
63
64    /// Get one page of the invitations of the client's organization, together with the
65    /// total number of the invitations.
66    pub async fn get_pagination_invitations(
67        &self,
68        p: i32,
69        page_size: i32,
70        query: &[(&str, &str)],
71    ) -> Result<(Vec<Invitation>, i32)> {
72        self.get_pagination(
73            "get-invitations",
74            &self.config.organization_name,
75            p,
76            page_size,
77            query,
78        )
79        .await
80    }
81
82    /// Get one invitation by name.
83    pub async fn get_invitation(&self, name: &str) -> Result<Option<Invitation>> {
84        self.get_object("get-invitation", &[("id", &self.get_id(name))])
85            .await
86    }
87
88    /// Get the invitation of an invitation code in an application.
89    pub async fn get_invitation_info(
90        &self,
91        code: &str,
92        application_name: &str,
93    ) -> Result<Option<Invitation>> {
94        let application_id = format!("admin/{application_name}");
95
96        self.get_object(
97            "get-invitation-info",
98            &[("applicationId", &application_id), ("code", code)],
99        )
100        .await
101    }
102
103    /// Add an invitation.
104    pub async fn add_invitation(&self, invitation: &Invitation) -> Result<bool> {
105        Ok(self
106            .modify_invitation("add-invitation", invitation, &[])
107            .await?
108            .1)
109    }
110
111    /// Update an invitation.
112    pub async fn update_invitation(&self, invitation: &Invitation) -> Result<bool> {
113        Ok(self
114            .modify_invitation("update-invitation", invitation, &[])
115            .await?
116            .1)
117    }
118
119    /// Update only the given columns of an invitation.
120    pub async fn update_invitation_for_columns(
121        &self,
122        invitation: &Invitation,
123        columns: &[&str],
124    ) -> Result<bool> {
125        Ok(self
126            .modify_invitation("update-invitation", invitation, columns)
127            .await?
128            .1)
129    }
130
131    /// Delete an invitation.
132    pub async fn delete_invitation(&self, invitation: &Invitation) -> Result<bool> {
133        Ok(self
134            .modify_invitation("delete-invitation", invitation, &[])
135            .await?
136            .1)
137    }
138
139    async fn modify_invitation(
140        &self,
141        action: &str,
142        invitation: &Invitation,
143        columns: &[&str],
144    ) -> Result<(Response, bool)> {
145        let mut invitation = invitation.clone();
146        invitation.owner = get_owner(&invitation.owner, &self.config.organization_name);
147
148        let id = format!("{}/{}", invitation.owner, invitation.name);
149        self.modify_object(action, &id, &invitation, columns).await
150    }
151}