adds forbidden_to_players wrapper for db_call

This commit is contained in:
2019-10-20 21:23:57 +02:00
parent edf236ef8c
commit 05a08ea337

View File

@@ -1,6 +1,6 @@
use actix_cors::Cors; use actix_cors::Cors;
use actix_files as fs; use actix_files as fs;
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer}; use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either};
use futures::{Future, IntoFuture}; use futures::{Future, IntoFuture};
use std::env; use std::env;
@@ -18,8 +18,10 @@ struct NewGroupLoot {
items: Vec<db::Item>, items: Vec<db::Item>,
} }
type MaybeForbidden = actix_web::Either<Box<dyn Future<Item = HttpResponse, Error = Error>>, HttpResponse>;
/// Wraps call to the database query and convert its result as a async HttpResponse /// Wraps call to the database query and convert its result as a async HttpResponse
pub fn db_call( fn db_call(
pool: AppPool, pool: AppPool,
query: api::ApiActions, query: api::ApiActions,
) -> impl Future<Item = HttpResponse, Error = Error> ) -> impl Future<Item = HttpResponse, Error = Error>
@@ -34,6 +36,15 @@ pub fn db_call(
}) })
} }
fn forbidden_to_players(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForbidden {
if id != 0 {
Either::B(HttpResponse::Forbidden().finish())
} else {
Either::A(Box::new(db_call(params.0, params.1)))
}
}
fn configure_app(config: &mut web::ServiceConfig) { fn configure_app(config: &mut web::ServiceConfig) {
use api::ApiActions as Q; use api::ApiActions as Q;
config.service( config.service(
@@ -92,12 +103,9 @@ fn configure_app(config: &mut web::ServiceConfig) {
db_call(pool, Q::BuyItems(*player, data.into_inner())) db_call(pool, Q::BuyItems(*player, data.into_inner()))
}, },
)) ))
.route(web::post().to_async( .route(web::post().to(
move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| { move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| {
match *player { forbidden_to_players(*player, (pool, Q::AddLoot(data.items.clone())))
0 => db_call(pool, Q::AddLoot(data.items.clone())),
_ => HttpResponse::Forbidden().finish().into_future(),
}
}, },
)) ))
.route(web::delete().to_async( .route(web::delete().to_async(