diff --git a/src/api_data/admin/admin_id_api.rs b/src/api_data/admin/admin_id_api.rs new file mode 100644 index 0000000..ff74abb --- /dev/null +++ b/src/api_data/admin/admin_id_api.rs @@ -0,0 +1,18 @@ +//! # Admin ID API +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::admin::AdminID; + +#[derive(Serialize)] +pub struct AdminIDAPI { + id: u64, +} + +impl AdminIDAPI { + pub fn new(id: AdminID) -> Self { + Self { id: id.id() } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index ecfd175..ebfebd7 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -3,4 +3,5 @@ //! @author Pierre Hubert pub mod admin_auth_options; -pub mod admin_auth_success; \ No newline at end of file +pub mod admin_auth_success; +pub mod admin_id_api; \ No newline at end of file diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index d86d119..cc9459a 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -4,6 +4,7 @@ use crate::api_data::admin::admin_auth_options::AdminAuthOptions; use crate::api_data::admin::admin_auth_success::AdminAuthSuccess; +use crate::api_data::admin::admin_id_api::AdminIDAPI; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::{admin_access_token_helper, admin_account_helper}; @@ -35,4 +36,9 @@ pub fn auth_with_reset_token(r: &mut HttpRequestHandler) -> RequestResult { let token = admin_access_token_helper::create(admin.id)?; r.set_response(AdminAuthSuccess::new(token)) +} + +/// Get current admin ID +pub fn get_admin_id(r: &mut HttpRequestHandler) -> RequestResult { + r.set_response(AdminIDAPI::new(r.admin_id()?)) } \ No newline at end of file diff --git a/src/data/base_request_handler.rs b/src/data/base_request_handler.rs index be5cfd3..63da997 100644 --- a/src/data/base_request_handler.rs +++ b/src/data/base_request_handler.rs @@ -33,6 +33,7 @@ use crate::utils::string_utils::{check_emoji_code, check_html_color, check_url, use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; use crate::utils::virtual_directories_utils; use crate::utils::zip_utils::is_valid_zip; +use crate::data::admin::AdminID; #[derive(Serialize)] struct SuccessMessage { @@ -73,6 +74,8 @@ pub trait BaseRequestHandler { self.user_access_token().map(|u| &u.user_id) } + /// Get an admin ID, if available + fn admin_id_opt(&self) -> Option; /// Success message fn success(&mut self, message: &str) -> RequestResult { @@ -200,6 +203,12 @@ pub trait BaseRequestHandler { } + /// Get current admin ID, returning an error in case of error + fn admin_id(&self) -> Res { + self.admin_id_opt().ok_or(ExecError::boxed_new("Could not get required admin ID!")) + } + + /// Check if a POST parameter was present in the request or not fn has_post_parameter(&self, name: &str) -> bool { self.post_parameter_opt(name).is_some() diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index fc1faa4..7299a9e 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -7,12 +7,13 @@ use actix_web::http::{HeaderName, HeaderValue, StatusCode}; use serde::Serialize; use crate::api_data::http_error::HttpError; +use crate::data::admin::{AdminAccessToken, AdminID}; use crate::data::api_client::APIClient; use crate::data::base_request_handler::{BaseRequestHandler, RequestValue}; use crate::data::config::conf; use crate::data::error::{Res, ResultBoxError}; use crate::data::user_token::UserAccessToken; -use crate::helpers::{account_helper, api_helper}; +use crate::helpers::{account_helper, api_helper, admin_access_token_helper}; use crate::routes::RequestResult; /// Http request handler @@ -26,6 +27,7 @@ pub struct HttpRequestHandler { headers: HashMap, client: Option, curr_user_token: Option, + curr_admin_token: Option, } impl HttpRequestHandler { @@ -38,6 +40,7 @@ impl HttpRequestHandler { headers: HashMap::new(), client: None, curr_user_token: None, + curr_admin_token: None, } } @@ -140,6 +143,15 @@ impl HttpRequestHandler { self.bad_request("Invalid origin for admin requests!".to_string()) } + + /// Check admin access token + pub fn check_admin_access_token(&mut self) -> Res { + let token = self.post_string("token")?; + + self.curr_admin_token = Some(admin_access_token_helper::find_by_token(&token)?); + + Ok(()) + } } impl BaseRequestHandler for HttpRequestHandler { @@ -189,4 +201,8 @@ impl BaseRequestHandler for HttpRequestHandler { fn user_access_token(&self) -> Option<&UserAccessToken> { self.curr_user_token.as_ref() } + + fn admin_id_opt(&self) -> Option { + self.curr_admin_token.as_ref().map(|p| p.id) + } } \ No newline at end of file diff --git a/src/data/user_ws_request_handler.rs b/src/data/user_ws_request_handler.rs index 54c124a..18844b6 100644 --- a/src/data/user_ws_request_handler.rs +++ b/src/data/user_ws_request_handler.rs @@ -11,6 +11,7 @@ use crate::data::error::{Res, ResultBoxError}; use crate::data::user_token::UserAccessToken; use crate::data::user_ws_connection::UserWsConnection; use crate::routes::RequestResult; +use crate::data::admin::AdminID; pub enum UserWsResponseType { SUCCESS, @@ -103,4 +104,8 @@ impl BaseRequestHandler for UserWsRequestHandler { fn user_access_token(&self) -> Option<&UserAccessToken> { Some(&self.connection.user_token) } + + fn admin_id_opt(&self) -> Option { + None + } } \ No newline at end of file diff --git a/src/routes.rs b/src/routes.rs index 9f41fab..7235271 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -149,6 +149,17 @@ impl Route { limit_policy, } } + + pub fn admin_post(uri: &'static str, func: RequestProcess) -> Route { + Route { + scope: RouteScope::ADMIN, + method: POST, + need_login: true, + uri, + func, + limit_policy: LimitPolicy::NONE, + } + } } /// Get the list of routes available @@ -338,5 +349,6 @@ pub fn get_routes() -> Vec { // Admin accounts controller Route::limited_admin_post_without_login("/admin/accounts/auth_options", Box::new(admin_account_controller::get_auth_options), LimitPolicy::FAILURE(5)), Route::limited_admin_post_without_login("/admin/accounts/auth_with_reset_token", Box::new(admin_account_controller::auth_with_reset_token), LimitPolicy::FAILURE(5)), + Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), ] } \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index 5c035a8..e3718d3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -228,8 +228,7 @@ fn process_simple_route(route: &Route, req: &mut HttpRequestHandler) -> RequestR req.check_admin_origin()?; if route.need_login { - // TODO : implement - unimplemented!(); + req.check_admin_access_token()?; } } }