Skip to main content

casdoor_rust_sdk/
syncer.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#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase", default)]
23pub struct TableColumn {
24    pub name: String,
25    pub r#type: String,
26    pub casdoor_name: String,
27    pub is_key: bool,
28    pub is_hashed: bool,
29    #[serde(deserialize_with = "null_to_default")]
30    pub values: Vec<String>,
31}
32
33/// Syncer has the same definition as
34/// <https://github.com/casdoor/casdoor/blob/master/object/syncer.go>.
35#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(rename_all = "camelCase", default)]
37pub struct Syncer {
38    pub owner: String,
39    pub name: String,
40    pub created_time: String,
41
42    pub organization: String,
43    pub r#type: String,
44    pub database_type: String,
45    pub ssl_mode: String,
46    pub ssh_type: String,
47
48    pub host: String,
49    pub port: i32,
50    pub user: String,
51    pub password: String,
52    pub ssh_host: String,
53    pub ssh_port: i32,
54    pub ssh_user: String,
55    pub ssh_password: String,
56    pub cert: String,
57    pub database: String,
58    pub table: String,
59    #[serde(deserialize_with = "null_to_default")]
60    pub table_columns: Vec<TableColumn>,
61    pub affiliation_table: String,
62    pub avatar_base_url: String,
63    pub error_text: String,
64    pub sync_interval: i32,
65    pub is_read_only: bool,
66    pub is_enabled: bool,
67}
68
69impl Client {
70    /// Get the syncers of the client's organization.
71    pub async fn get_syncers(&self) -> Result<Vec<Syncer>> {
72        self.get_list("get-syncers", &[("owner", &self.config.organization_name)])
73            .await
74    }
75
76    /// Get one page of the syncers of the client's organization, together with the total
77    /// number of the syncers.
78    pub async fn get_pagination_syncers(
79        &self,
80        p: i32,
81        page_size: i32,
82        query: &[(&str, &str)],
83    ) -> Result<(Vec<Syncer>, i32)> {
84        self.get_pagination(
85            "get-syncers",
86            &self.config.organization_name,
87            p,
88            page_size,
89            query,
90        )
91        .await
92    }
93
94    /// Get one syncer by name.
95    pub async fn get_syncer(&self, name: &str) -> Result<Option<Syncer>> {
96        self.get_object("get-syncer", &[("id", &self.get_id(name))])
97            .await
98    }
99
100    /// Add a syncer.
101    pub async fn add_syncer(&self, syncer: &Syncer) -> Result<bool> {
102        Ok(self.modify_syncer("add-syncer", syncer, &[]).await?.1)
103    }
104
105    /// Update a syncer.
106    pub async fn update_syncer(&self, syncer: &Syncer) -> Result<bool> {
107        Ok(self.modify_syncer("update-syncer", syncer, &[]).await?.1)
108    }
109
110    /// Delete a syncer.
111    pub async fn delete_syncer(&self, syncer: &Syncer) -> Result<bool> {
112        Ok(self.modify_syncer("delete-syncer", syncer, &[]).await?.1)
113    }
114
115    async fn modify_syncer(
116        &self,
117        action: &str,
118        syncer: &Syncer,
119        columns: &[&str],
120    ) -> Result<(Response, bool)> {
121        let mut syncer = syncer.clone();
122        syncer.owner = get_owner(&syncer.owner, &self.config.organization_name);
123
124        let id = format!("{}/{}", syncer.owner, syncer.name);
125        self.modify_object(action, &id, &syncer, columns).await
126    }
127}