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