Various Fixes to API v1
All checks were successful
Run Tests on Code / run-tests (push) Successful in 0s

This commit is contained in:
Neshura 2023-12-12 22:11:32 +01:00
parent c0799484bb
commit 8373b278cc
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 54 additions and 42 deletions

View file

@ -48,7 +48,9 @@ async fn verify_user_auth(data: &web::Data<AppState>, auth_token: &str, user_tok
// User Endpoints // User Endpoints
#[utoipa::path( #[utoipa::path(
request_body = schemas::GetUserParams, params(
schemas::GetUserParams
),
responses( responses(
(status = 200, description = "OK", body = User), (status = 200, description = "OK", body = User),
(status = 403, description = "Unauthorized"), (status = 403, description = "Unauthorized"),
@ -59,7 +61,7 @@ async fn verify_user_auth(data: &web::Data<AppState>, auth_token: &str, user_tok
), ),
)] )]
#[get("/api/v1/user")] #[get("/api/v1/user")]
async fn get_user( pub(crate) async fn get_user(
data: web::Data<AppState>, data: web::Data<AppState>,
params: web::Json<schemas::GetUserParams>, params: web::Json<schemas::GetUserParams>,
req: HttpRequest, req: HttpRequest,
@ -195,9 +197,17 @@ pub(crate) async fn update_user(
None => return HttpResponse::Unauthorized().finish(), None => return HttpResponse::Unauthorized().finish(),
}; };
let mut user_permissions: HashMap<String, bool> = HashMap::new();
match params.permissions {
Some(data) => {user_permissions = data.clone()},
None => {},
}
let mut elevated_auth = false; let mut elevated_auth = false;
if params.permissions["game_permissions"] || params.permissions["empire_permissions"] || params.permissions["data_permissions"] || params.permissions["user_permissions"] { if user_permissions.len() != 0 {
elevated_auth = true; if user_permissions["game_permissions"] || user_permissions["empire_permissions"] || user_permissions["data_permissions"] || user_permissions["user_permissions"] {
elevated_auth = true;
}
} }
let auth = verify_user_auth(&data, &auth_token, &params.user_token, schemas::TablePermission::User, elevated_auth).await; let auth = verify_user_auth(&data, &auth_token, &params.user_token, schemas::TablePermission::User, elevated_auth).await;
@ -225,41 +235,43 @@ pub(crate) async fn update_user(
any_param_present = true; any_param_present = true;
} }
for (entry, value) in params.permissions.iter() { if user_permissions.len() != 0 {
match entry.deref() { for (entry, value) in user_permissions.iter() {
"game_permissions" => { match entry.deref() {
user_query_separated.push( " game_permissions = "); "game_permissions" => {
match any_param_present { user_query_separated.push( " game_permissions = ");
true => user_query_separated.push_bind(value), match any_param_present {
false => user_query_separated.push_bind_unseparated(value) true => user_query_separated.push_bind(value),
}; false => user_query_separated.push_bind_unseparated(value)
any_param_present = true; };
}, any_param_present = true;
"empire_permissions" => { },
user_query_separated.push( " empire_permissions = "); "empire_permissions" => {
match any_param_present { user_query_separated.push( " empire_permissions = ");
true => user_query_separated.push_bind(value), match any_param_present {
false => user_query_separated.push_bind_unseparated(value) true => user_query_separated.push_bind(value),
}; false => user_query_separated.push_bind_unseparated(value)
any_param_present = true; };
}, any_param_present = true;
"data_permissions" => { },
user_query_separated.push( " data_permissions = "); "data_permissions" => {
match any_param_present { user_query_separated.push( " data_permissions = ");
true => user_query_separated.push_bind(value), match any_param_present {
false => user_query_separated.push_bind_unseparated(value) true => user_query_separated.push_bind(value),
}; false => user_query_separated.push_bind_unseparated(value)
any_param_present = true; };
}, any_param_present = true;
"user_permissions" => { },
user_query_separated.push( " user_permissions = "); "user_permissions" => {
match any_param_present { user_query_separated.push( " user_permissions = ");
true => user_query_separated.push_bind(value), match any_param_present {
false => user_query_separated.push_bind_unseparated(value) true => user_query_separated.push_bind(value),
}; false => user_query_separated.push_bind_unseparated(value)
any_param_present = true; };
}, any_param_present = true;
_ => {} },
_ => {}
}
} }
} }

View file

@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use utoipa::{ToSchema}; use utoipa::{IntoParams, ToSchema};
// DB Permission Enums // DB Permission Enums
@ -32,7 +32,7 @@ pub struct User {
pub permissions: HashMap<String, bool>, pub permissions: HashMap<String, bool>,
} }
#[derive(Serialize, Deserialize, ToSchema, Debug)] #[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
pub struct GetUserParams { pub struct GetUserParams {
#[schema(example = "abcdef")] #[schema(example = "abcdef")]
pub user_token: String, pub user_token: String,
@ -54,7 +54,7 @@ pub struct UpdateUserParams {
[\"user_permissions\"]: false, [\"user_permissions\"]: false,
}\ }\
")] ")]
pub permissions: HashMap<String, bool>, pub permissions: Option<HashMap<String, bool>>,
} }
#[derive(Serialize, Deserialize, ToSchema, Debug)] #[derive(Serialize, Deserialize, ToSchema, Debug)]