This commit is contained in:
JMARyA 2024-08-11 02:57:16 +02:00
parent 9a9e77a3bb
commit e3a0d4075f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 42 additions and 4 deletions

View file

@ -1,7 +1,10 @@
use crate::library::user::Session;
use crate::library::user::User;
use crate::route::to_api;
use crate::route::ToAPI;
use mongod::Model;
use mongodb::bson::doc;
use rocket::get;
use rocket::http::Status;
use rocket::outcome::Outcome;
use rocket::post;
@ -14,6 +17,14 @@ use serde_json::json;
use super::api_error;
use super::FallibleApiResponse;
macro_rules! check_admin {
($u:ident) => {
if !$u.is_admin() {
return Err(api_error("Forbidden"));
}
};
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
type Error = ();
@ -67,11 +78,18 @@ pub async fn passwd_route(passwd: Json<PasswdData>, mut u: User) -> FallibleApiR
}))
}
#[get("/users")]
pub async fn users_route(u: User) -> FallibleApiResponse {
check_admin!(u);
let users: Vec<_> = to_api(&User::find(doc! {}, None).await.unwrap()).await;
Ok(json!({"users": users}))
}
#[post("/userCreate", data = "<user>")]
pub async fn user_create_route(user: Json<LoginData>, u: User) -> FallibleApiResponse {
if !u.is_admin() {
return Err(api_error("Forbidden"));
}
check_admin!(u);
let new_user = User::create(
&user.username,