-
Notifications
You must be signed in to change notification settings - Fork 349
/
mod.rs
303 lines (289 loc) 路 9.61 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2024 Zinc Labs Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::io::Error;
use actix_web::{cookie, delete, get, http, post, put, web, HttpResponse};
use config::{
utils::{base64, json},
CONFIG,
};
use strum::IntoEnumIterator;
use crate::{
common::{
meta::{
self,
user::{
AuthTokens, RolesResponse, SignInResponse, SignInUser, UpdateUser, UserOrgRole,
UserRequest, UserRole,
},
},
utils::auth::UserEmail,
},
service::users,
};
/// ListUsers
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "UserList",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
),
responses(
(status = 200, description = "Success", content_type = "application/json", body = UserList),
)
)]
#[get("/{org_id}/users")]
pub async fn list(org_id: web::Path<String>) -> Result<HttpResponse, Error> {
let org_id = org_id.into_inner();
users::list_users(&org_id).await
}
/// CreateUser
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "UserSave",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
),
request_body(content = UserRequest, description = "User data", content_type = "application/json"),
responses(
(status = 200, description = "Success", content_type = "application/json", body = HttpResponse),
)
)]
#[post("/{org_id}/users")]
pub async fn save(
org_id: web::Path<String>,
user: web::Json<UserRequest>,
user_email: UserEmail,
) -> Result<HttpResponse, Error> {
let org_id = org_id.into_inner();
let initiator_id = user_email.user_id;
let mut user = user.into_inner();
user.email = user.email.trim().to_string();
if user.role.eq(&meta::user::UserRole::Root) {
return Ok(
HttpResponse::BadRequest().json(meta::http::HttpResponse::error(
http::StatusCode::BAD_REQUEST.into(),
"Not allowed".to_string(),
)),
);
}
#[cfg(not(feature = "enterprise"))]
{
user.role = meta::user::UserRole::Admin;
}
users::post_user(&org_id, user, &initiator_id).await
}
/// UpdateUser
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "UserUpdate",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
("email_id" = String, Path, description = "User's email id"),
),
request_body(content = UpdateUser, description = "User data", content_type = "application/json"),
responses(
(status = 200, description = "Success", content_type = "application/json", body = HttpResponse),
)
)]
#[put("/{org_id}/users/{email_id}")]
pub async fn update(
params: web::Path<(String, String)>,
user: web::Json<UpdateUser>,
user_email: UserEmail,
) -> Result<HttpResponse, Error> {
let (org_id, email_id) = params.into_inner();
let email_id = email_id.trim().to_string();
#[cfg(not(feature = "enterprise"))]
let mut user = user.into_inner();
#[cfg(feature = "enterprise")]
let user = user.into_inner();
if user.eq(&UpdateUser::default()) {
return Ok(
HttpResponse::BadRequest().json(meta::http::HttpResponse::error(
http::StatusCode::BAD_REQUEST.into(),
"Please specify appropriate fields to update user".to_string(),
)),
);
}
#[cfg(not(feature = "enterprise"))]
{
user.role = Some(meta::user::UserRole::Admin);
}
let initiator_id = &user_email.user_id;
let self_update = user_email.user_id.eq(&email_id);
users::update_user(&org_id, &email_id, self_update, initiator_id, user).await
}
/// AddUserToOrganization
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "AddUserToOrg",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
("email_id" = String, Path, description = "User's email id"),
),
request_body(content = UserOrgRole, description = "User role", content_type = "application/json"),
responses(
(status = 200, description = "Success", content_type = "application/json", body = HttpResponse),
)
)]
#[post("/{org_id}/users/{email_id}")]
pub async fn add_user_to_org(
params: web::Path<(String, String)>,
_role: web::Json<UserOrgRole>,
user_email: UserEmail,
) -> Result<HttpResponse, Error> {
let (org_id, email_id) = params.into_inner();
// let role = role.into_inner().role;
let role = meta::user::UserRole::Admin;
let initiator_id = user_email.user_id;
users::add_user_to_org(&org_id, &email_id, role, &initiator_id).await
}
/// RemoveUserFromOrganization
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "RemoveUserFromOrg",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
("email_id" = String, Path, description = "User name"),
),
responses(
(status = 200, description = "Success", content_type = "application/json", body = HttpResponse),
(status = 404, description = "NotFound", content_type = "application/json", body = HttpResponse),
)
)]
#[delete("/{org_id}/users/{email_id}")]
pub async fn delete(
path: web::Path<(String, String)>,
user_email: UserEmail,
) -> Result<HttpResponse, Error> {
let (org_id, email_id) = path.into_inner();
let initiator_id = user_email.user_id;
users::remove_user_from_org(&org_id, &email_id, &initiator_id).await
}
/// AuthenticateUser
#[utoipa::path(
context_path = "/auth",
tag = "Auth",
operation_id = "UserLoginCheck",
request_body(content = SignInUser, description = "User login", content_type = "application/json"),
responses(
(status = 200, description = "Success", content_type = "application/json", body = SignInResponse),
)
)]
#[post("/login")]
pub async fn authentication(auth: web::Json<SignInUser>) -> Result<HttpResponse, Error> {
#[cfg(feature = "enterprise")]
use o2_enterprise::enterprise::common::infra::config::O2_CONFIG;
#[cfg(feature = "enterprise")]
let native_login_enabled = O2_CONFIG.dex.native_login_enabled;
#[cfg(not(feature = "enterprise"))]
let native_login_enabled = true;
if !native_login_enabled {
return Ok(HttpResponse::Forbidden().json("Not Supported"));
}
let mut resp = SignInResponse::default();
match crate::handler::http::auth::validator::validate_user(&auth.name, &auth.password).await {
Ok(v) => {
if v.is_valid {
resp.status = true;
} else {
resp.status = false;
resp.message = "Invalid credentials".to_string();
}
}
Err(_e) => {
resp.status = false;
resp.message = "Invalid credentials".to_string();
}
};
if resp.status {
let access_token = format!(
"Basic {}",
base64::encode(&format!("{}:{}", auth.name, auth.password))
);
let tokens = json::to_string(&AuthTokens {
access_token,
refresh_token: "".to_string(),
})
.unwrap();
let mut auth_cookie = cookie::Cookie::new("auth_tokens", tokens);
auth_cookie.set_expires(
cookie::time::OffsetDateTime::now_utc()
+ cookie::time::Duration::seconds(CONFIG.auth.cookie_max_age),
);
auth_cookie.set_http_only(true);
auth_cookie.set_secure(CONFIG.auth.cookie_secure_only);
auth_cookie.set_path("/");
if CONFIG.auth.cookie_same_site_lax {
auth_cookie.set_same_site(cookie::SameSite::Lax);
} else {
auth_cookie.set_same_site(cookie::SameSite::None);
}
Ok(HttpResponse::Ok().cookie(auth_cookie).json(resp))
} else {
Ok(HttpResponse::Unauthorized().json(resp))
}
}
/// ListUsers
#[utoipa::path(
context_path = "/api",
tag = "Users",
operation_id = "UserRoles",
security(
("Authorization"= [])
),
params(
("org_id" = String, Path, description = "Organization name"),
),
responses(
(status = 200, description = "Success", content_type = "application/json", body = UserList),
)
)]
#[get("/{org_id}/users/roles")]
pub async fn list_roles(_org_id: web::Path<String>) -> Result<HttpResponse, Error> {
let roles = UserRole::iter()
.filter_map(|role| {
if role.eq(&UserRole::Root) || role.eq(&UserRole::Member) {
None
} else {
Some(RolesResponse {
label: role.get_label(),
value: role.to_string(),
})
}
})
.collect::<Vec<RolesResponse>>();
Ok(HttpResponse::Ok().json(roles))
}