This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_nextcloud_user.rs
68 lines (66 loc) · 2.34 KB
/
post_nextcloud_user.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
use super::{
defaults, execute_with_envs,
structs::{ExternalResponse, NextCloudUser, Response, ResponseTypes},
};
use crate::library::security::{get_token, validate_token};
use actix_web::{web::Json, HttpRequest, HttpResponse};
use actix_web_codegen::post;
use arthur_hugon_tools::is_env;
/**
* @todo Create user in nextcloud
*/
#[post("/nextcloud/user")]
pub fn sync_post_nextcloud_user(
req: HttpRequest,
params: Json<NextCloudUser>,
) -> actix_web::Result<HttpResponse> {
is_env("NC_ADMIN_USERNAME", &|_| {}, &|_| {
panic!("Error NC_ADMIN_USERNAME not set");
})
.unwrap();
is_env("NC_ADMIN_PASSWORD", &|_| {}, &|_| {
panic!("Error NC_ADMIN_PASSWORD not set");
})
.unwrap();
match get_token(req.headers()) {
Ok(x) => {
if validate_token(x.as_str()).unwrap() {
let resp = execute_with_envs(
defaults::NEXT,
&[
"create",
"-u",
params.username.as_str(),
"-p",
params.password.as_str(),
],
vec![
(
"NC_ADMIN_USERNAME",
crate::var("NC_ADMIN_USERNAME").unwrap().as_str(),
),
(
"NC_ADMIN_PASSWORD",
crate::var("NC_ADMIN_PASSWORD").unwrap().as_str(),
),
],
);
Ok(match resp {
Ok(x) => {
let x: ExternalResponse<bool> = ss!(ExternalResponse<bool>, x.as_str());
if x.value.is_some() {
let x = Response::ok(200, ResponseTypes::Boolean(x.value.unwrap()));
response!(Ok, x)
} else {
response!(InternalServerError, defaults::ERROR_500)
}
}
Err(e) => response!(InternalServerError, defaults::ERROR_500),
})
} else {
Ok(response!(Unauthorized, defaults::ERROR_401))
}
}
Err(e) => Ok(response!(BadRequest, defaults::ERROR_400)),
}
}