update GET /perms/<username> error messages

This commit is contained in:
SadlyNotSappho 2024-03-19 13:23:38 -07:00
parent c5934c6fb8
commit ab7b102045
1 changed files with 41 additions and 33 deletions

View File

@ -158,33 +158,48 @@ async fn logout(cookies: &CookieJar<'_>) -> status::Custom<&'static str> {
}
#[get("/adminpanel")]
async fn adminpanel(mut db: Connection<Db>, cookies: &CookieJar<'_>) -> status::Custom<RawHtml<String>> {
async fn adminpanel(
mut db: Connection<Db>,
cookies: &CookieJar<'_>,
) -> status::Custom<RawHtml<String>> {
let token = cookies.get_private("token");
match token {
Some(t) => match User::get_by_token(&mut db, t).await {
Some(user) => match user.admin {
true => status::Custom(Status::Ok, RawHtml(
fs::read_to_string("/srv/web/adminpanel.html")
.unwrap()
.replace("{{username}}", &user.username[..])),
true => status::Custom(
Status::Ok,
RawHtml(
fs::read_to_string("/srv/web/adminpanel.html")
.unwrap()
.replace("{{username}}", &user.username[..]),
),
),
false => status::Custom(
Status::Unauthorized,
RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap()),
),
false => status::Custom(Status::Unauthorized, RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap())),
},
None => status::Custom(Status::Unauthorized, RawHtml(
fs::read_to_string("/srv/web/error.html")
.unwrap()
.replace("{{errorcode}}", "401")),
None => status::Custom(
Status::Unauthorized,
RawHtml(
fs::read_to_string("/srv/web/error.html")
.unwrap()
.replace("{{errorcode}}", "401"),
),
),
},
None => status::Custom(Status::Unauthorized, RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap())),
None => status::Custom(
Status::Unauthorized,
RawHtml(fs::read_to_string("/srv/web/invalidperms.html").unwrap()),
),
}
}
#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
struct ApiPermsResult {
perms: Result<Perms, String>,
}
// #[derive(Deserialize, Serialize)]
// #[serde(crate = "rocket::serde")]
// struct ApiPermsResult {
// perms: Result<Perms, String>,
// }
#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
struct Perms {
@ -197,33 +212,26 @@ async fn api_perms(
mut db: Connection<Db>,
username: String,
cookies: &CookieJar<'_>,
) -> Json<ApiPermsResult> {
) -> status::Custom<Json<Result<Perms, &'static str>>> {
match cookies.get_private("token") {
Some(t) => match User::get_by_token(&mut db, t).await {
Some(user) => match user.admin {
true => match User::get_by_username(&mut db, &username).await {
Some(user) => Json(ApiPermsResult {
perms: Ok(Perms {
Some(user) => status::Custom(
Status::Ok,
Json(Ok(Perms {
admin: user.admin,
make_posts: user.make_posts,
comment: user.comment,
}),
}),
None => Json(ApiPermsResult {
perms: Err("User doesn't exist".to_string()),
}),
})),
),
None => status::Custom(Status::NotFound, Json(Err("User doesn't exist"))),
},
false => Json(ApiPermsResult {
perms: Err("You don't have the permission to do this".to_string()),
}),
false => status::Custom(Status::Unauthorized, Json(Err("You don't have the permission to do this"))),
},
None => Json(ApiPermsResult {
perms: Err("Invalid token".to_string()),
}),
None => status::Custom(Status::Unauthorized, Json(Err("Invalid token"))),
},
None => Json(ApiPermsResult {
perms: Err("Not logged in".to_string()),
}),
None => status::Custom(Status::Unauthorized, Json(Err("Not logged in"))),
}
}