Skip to main content

casdoor_rust_sdk/
plan.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/// Plan has the same definition as
22/// <https://github.com/casdoor/casdoor/blob/master/object/plan.go>.
23#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", default)]
25pub struct Plan {
26    pub owner: String,
27    pub name: String,
28    pub created_time: String,
29    pub display_name: String,
30    pub description: String,
31
32    pub price: f64,
33    pub currency: String,
34    pub period: String,
35    pub product: String,
36    /// The payment providers of the related product.
37    #[serde(deserialize_with = "null_to_default")]
38    pub payment_providers: Vec<String>,
39    pub is_enabled: bool,
40    /// If true, a user can only have at most one subscription of this plan.
41    pub is_exclusive: bool,
42
43    pub role: String,
44    #[serde(deserialize_with = "null_to_default")]
45    pub options: Vec<String>,
46}
47
48impl Client {
49    /// Get the plans of the client's organization.
50    pub async fn get_plans(&self) -> Result<Vec<Plan>> {
51        self.get_list("get-plans", &[("owner", &self.config.organization_name)])
52            .await
53    }
54
55    /// Get one page of the plans of the client's organization, together with the total
56    /// number of the plans.
57    pub async fn get_pagination_plans(
58        &self,
59        p: i32,
60        page_size: i32,
61        query: &[(&str, &str)],
62    ) -> Result<(Vec<Plan>, i32)> {
63        self.get_pagination(
64            "get-plans",
65            &self.config.organization_name,
66            p,
67            page_size,
68            query,
69        )
70        .await
71    }
72
73    /// Get one plan by name.
74    pub async fn get_plan(&self, name: &str) -> Result<Option<Plan>> {
75        self.get_object("get-plan", &[("id", &self.get_id(name))])
76            .await
77    }
78
79    /// Add a plan.
80    pub async fn add_plan(&self, plan: &Plan) -> Result<bool> {
81        Ok(self.modify_plan("add-plan", plan, &[]).await?.1)
82    }
83
84    /// Update a plan.
85    pub async fn update_plan(&self, plan: &Plan) -> Result<bool> {
86        Ok(self.modify_plan("update-plan", plan, &[]).await?.1)
87    }
88
89    /// Delete a plan.
90    pub async fn delete_plan(&self, plan: &Plan) -> Result<bool> {
91        Ok(self.modify_plan("delete-plan", plan, &[]).await?.1)
92    }
93
94    async fn modify_plan(
95        &self,
96        action: &str,
97        plan: &Plan,
98        columns: &[&str],
99    ) -> Result<(Response, bool)> {
100        let mut plan = plan.clone();
101        plan.owner = get_owner(&plan.owner, &self.config.organization_name);
102
103        let id = format!("{}/{}", plan.owner, plan.name);
104        self.modify_object(action, &id, &plan, columns).await
105    }
106}