casdoor_rust_sdk/service/
user.rs

1// Copyright 2022 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::fmt::Display;
16
17use http::StatusCode;
18
19use crate::entity::{CasdoorConfig, CasdoorUser};
20
21pub struct UserService<'a> {
22    config: &'a CasdoorConfig,
23}
24
25enum Op {
26    Add,
27    Delete,
28    Update,
29}
30
31impl Display for Op {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Op::Add => write!(f, "add-user"),
35            Op::Delete => write!(f, "delete-user"),
36            Op::Update => write!(f, "update-user"),
37        }
38    }
39}
40
41#[allow(dead_code)]
42impl<'a> UserService<'a> {
43    pub fn new(config: &'a CasdoorConfig) -> Self {
44        Self { config }
45    }
46
47    pub async fn get_users(&self) -> Result<Vec<CasdoorUser>, Box<dyn std::error::Error>> {
48        let url = format!(
49            "{}/api/get-users?owner={}&clientId={}&clientSecret={}",
50            self.config.endpoint,
51            self.config.org_name,
52            self.config.client_id,
53            self.config.client_secret
54        );
55
56        let json = reqwest::Client::new().get(url).send().await?.json().await?;
57        Ok(serde_json::from_value(json)?)
58    }
59
60    pub async fn get_sorted_users(
61        &self,
62        sorter: String,
63        limit: i32,
64    ) -> Result<Vec<CasdoorUser>, Box<dyn std::error::Error>> {
65        let url = format!(
66            "{}/api/get-sorted-users?owner={}&clientId={}&clientSecret={}&sorter={}&limit={}",
67            self.config.endpoint,
68            self.config.org_name,
69            self.config.client_id,
70            self.config.client_secret,
71            sorter,
72            limit
73        );
74
75        let json = reqwest::Client::new().get(url).send().await?.json().await?;
76        Ok(serde_json::from_value(json)?)
77    }
78
79    pub async fn get_user_count(
80        &self,
81        is_online: String,
82    ) -> Result<i64, Box<dyn std::error::Error>> {
83        let url = format!(
84            "{}/api/get-user-count?owner={}&clientId={}&clientSecret={}&isOnline={}",
85            self.config.endpoint,
86            self.config.org_name,
87            self.config.client_id,
88            self.config.client_secret,
89            is_online
90        );
91
92        let json = reqwest::Client::new().get(url).send().await?.json().await?;
93        Ok(serde_json::from_value(json)?)
94    }
95
96    pub async fn get_user(&self, name: String) -> Result<CasdoorUser, Box<dyn std::error::Error>> {
97        let url = format!(
98            "{}/api/get-user?id={}/{}&clientId={}&clientSecret={}",
99            self.config.endpoint,
100            self.config.org_name,
101            name,
102            self.config.client_id,
103            self.config.client_secret
104        );
105
106        let json = reqwest::Client::new().get(url).send().await?.json().await?;
107        Ok(serde_json::from_value(json)?)
108    }
109
110    pub async fn get_user_with_email(
111        &self,
112        name: String,
113        email: String,
114    ) -> Result<CasdoorUser, Box<dyn std::error::Error>> {
115        let url = format!(
116            "{}/api/get-user?id={}/{}&owner={}&clientId={}&clientSecret={}&email={}",
117            self.config.endpoint,
118            self.config.org_name,
119            name,
120            self.config.org_name,
121            self.config.client_id,
122            self.config.client_secret,
123            email
124        );
125
126        println!("{}", url);
127
128        let json = reqwest::Client::new().get(url).send().await?.json().await?;
129        Ok(serde_json::from_value(json)?)
130    }
131
132    async fn modify_user(
133        &self,
134        op: Op,
135        user: CasdoorUser,
136    ) -> Result<StatusCode, Box<dyn std::error::Error>> {
137        let url = format!(
138            "{}/api/{}?id={}/{}&clientId={}&clientSecret={}",
139            self.config.endpoint,
140            op,
141            user.owner,
142            user.name,
143            self.config.client_id,
144            self.config.client_secret
145        );
146
147        let res = reqwest::Client::new().post(url).json(&user).send().await?;
148        let status = res.status();
149        Ok(status)
150    }
151
152    pub async fn add_user(
153        &self,
154        user: CasdoorUser,
155    ) -> Result<StatusCode, Box<dyn std::error::Error>> {
156        self.modify_user(Op::Add, user).await
157    }
158
159    pub async fn delete_user(
160        &self,
161        user: CasdoorUser,
162    ) -> Result<StatusCode, Box<dyn std::error::Error>> {
163        self.modify_user(Op::Delete, user).await
164    }
165
166    pub async fn update_user(
167        &self,
168        user: CasdoorUser,
169    ) -> Result<StatusCode, Box<dyn std::error::Error>> {
170        self.modify_user(Op::Update, user).await
171    }
172}