From 8373b278cc6242be75115381e755e1bbc2a668f7 Mon Sep 17 00:00:00 2001 From: Neshura Date: Tue, 12 Dec 2023 22:11:32 +0100 Subject: [PATCH] Various Fixes to API v1 --- src/v1/mod.rs | 90 +++++++++++++++++++++++++++-------------------- src/v1/schemas.rs | 6 ++-- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/v1/mod.rs b/src/v1/mod.rs index cddfbba..118d0ca 100644 --- a/src/v1/mod.rs +++ b/src/v1/mod.rs @@ -48,7 +48,9 @@ async fn verify_user_auth(data: &web::Data, auth_token: &str, user_tok // User Endpoints #[utoipa::path( - request_body = schemas::GetUserParams, + params( + schemas::GetUserParams + ), responses( (status = 200, description = "OK", body = User), (status = 403, description = "Unauthorized"), @@ -59,7 +61,7 @@ async fn verify_user_auth(data: &web::Data, auth_token: &str, user_tok ), )] #[get("/api/v1/user")] -async fn get_user( +pub(crate) async fn get_user( data: web::Data, params: web::Json, req: HttpRequest, @@ -195,9 +197,17 @@ pub(crate) async fn update_user( None => return HttpResponse::Unauthorized().finish(), }; + let mut user_permissions: HashMap = HashMap::new(); + match params.permissions { + Some(data) => {user_permissions = data.clone()}, + None => {}, + } + let mut elevated_auth = false; - if params.permissions["game_permissions"] || params.permissions["empire_permissions"] || params.permissions["data_permissions"] || params.permissions["user_permissions"] { - elevated_auth = true; + if user_permissions.len() != 0 { + 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, ¶ms.user_token, schemas::TablePermission::User, elevated_auth).await; @@ -225,41 +235,43 @@ pub(crate) async fn update_user( any_param_present = true; } - for (entry, value) in params.permissions.iter() { - match entry.deref() { - "game_permissions" => { - user_query_separated.push( " game_permissions = "); - match any_param_present { - true => user_query_separated.push_bind(value), - false => user_query_separated.push_bind_unseparated(value) - }; - any_param_present = true; - }, - "empire_permissions" => { - user_query_separated.push( " empire_permissions = "); - match any_param_present { - true => user_query_separated.push_bind(value), - false => user_query_separated.push_bind_unseparated(value) - }; - any_param_present = true; - }, - "data_permissions" => { - user_query_separated.push( " data_permissions = "); - match any_param_present { - true => user_query_separated.push_bind(value), - false => user_query_separated.push_bind_unseparated(value) - }; - any_param_present = true; - }, - "user_permissions" => { - user_query_separated.push( " user_permissions = "); - match any_param_present { - true => user_query_separated.push_bind(value), - false => user_query_separated.push_bind_unseparated(value) - }; - any_param_present = true; - }, - _ => {} + if user_permissions.len() != 0 { + for (entry, value) in user_permissions.iter() { + match entry.deref() { + "game_permissions" => { + user_query_separated.push( " game_permissions = "); + match any_param_present { + true => user_query_separated.push_bind(value), + false => user_query_separated.push_bind_unseparated(value) + }; + any_param_present = true; + }, + "empire_permissions" => { + user_query_separated.push( " empire_permissions = "); + match any_param_present { + true => user_query_separated.push_bind(value), + false => user_query_separated.push_bind_unseparated(value) + }; + any_param_present = true; + }, + "data_permissions" => { + user_query_separated.push( " data_permissions = "); + match any_param_present { + true => user_query_separated.push_bind(value), + false => user_query_separated.push_bind_unseparated(value) + }; + any_param_present = true; + }, + "user_permissions" => { + user_query_separated.push( " user_permissions = "); + match any_param_present { + true => user_query_separated.push_bind(value), + false => user_query_separated.push_bind_unseparated(value) + }; + any_param_present = true; + }, + _ => {} + } } } diff --git a/src/v1/schemas.rs b/src/v1/schemas.rs index d6d9d65..8fea931 100644 --- a/src/v1/schemas.rs +++ b/src/v1/schemas.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use utoipa::{ToSchema}; +use utoipa::{IntoParams, ToSchema}; // DB Permission Enums @@ -32,7 +32,7 @@ pub struct User { pub permissions: HashMap, } -#[derive(Serialize, Deserialize, ToSchema, Debug)] +#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)] pub struct GetUserParams { #[schema(example = "abcdef")] pub user_token: String, @@ -54,7 +54,7 @@ pub struct UpdateUserParams { [\"user_permissions\"]: false, }\ ")] - pub permissions: HashMap, + pub permissions: Option>, } #[derive(Serialize, Deserialize, ToSchema, Debug)]