diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index fe3b79f..d72a51d 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -53,7 +53,18 @@ pub fn get_admin_id(r: &mut HttpRequestHandler) -> RequestResult { /// Get current admin information pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult { - let admin = admin_account_helper::find_admin_by_id(r.admin_id()?)?; + let admin = match r.has_post_parameter("id") { + false => admin_account_helper::find_admin_by_id(r.admin_id()?)?, + true => { + let admin_id = r.post_admin_id("id")?; + + if admin_id == r.admin_id()? { + admin_account_helper::find_admin_by_id(admin_id)? + } else { + unimplemented!(); + } + } + }; r.set_response(AdminInfoAPI::new(&admin)) } \ No newline at end of file diff --git a/src/data/base_request_handler.rs b/src/data/base_request_handler.rs index 63da997..f6b60bf 100644 --- a/src/data/base_request_handler.rs +++ b/src/data/base_request_handler.rs @@ -13,6 +13,7 @@ use serde::Serialize; use crate::api_data::http_error::HttpError; use crate::constants::PASSWORD_MIN_LENGTH; +use crate::data::admin::AdminID; use crate::data::comment::Comment; use crate::data::config::conf; use crate::data::conversation::{ConversationMember, ConvID}; @@ -23,7 +24,7 @@ use crate::data::group_id::GroupID; use crate::data::post::{Post, PostAccessLevel}; use crate::data::user::UserID; use crate::data::user_token::UserAccessToken; -use crate::helpers::{account_helper, comments_helper, conversations_helper, custom_emojies_helper, friends_helper, groups_helper, posts_helper, user_helper, virtual_directory_helper}; +use crate::helpers::{account_helper, admin_account_helper, comments_helper, conversations_helper, custom_emojies_helper, friends_helper, groups_helper, posts_helper, user_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::routes::RequestResult; use crate::utils::mp3_utils::is_valid_mp3; @@ -33,7 +34,6 @@ 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 { @@ -566,6 +566,21 @@ pub trait BaseRequestHandler { Ok(user_id) } + /// Get the ID of an admin included in a POST request + fn post_admin_id(&mut self, name: &str) -> Res { + let admin_id = AdminID::new(self.post_u64(name)?); + + if admin_id.id() < 1 { + self.bad_request(format!("Invalid admin specified in '{}'!", name))?; + } + + if !admin_account_helper::exists(admin_id)? { + self.not_found(format!("Admin with ID {} not found!", admin_id.id()))?; + } + + Ok(admin_id) + } + /// Get a list of users ID included in the request fn post_users_id(&mut self, name: &str) -> ResultBoxError> { let users = self.post_numbers_list(name, 1)? diff --git a/src/helpers/admin_account_helper.rs b/src/helpers/admin_account_helper.rs index d902097..82eada8 100644 --- a/src/helpers/admin_account_helper.rs +++ b/src/helpers/admin_account_helper.rs @@ -24,6 +24,14 @@ pub fn create(new_admin: &NewAdmin) -> Res { .map(|i| AdminID::new(i)) } +/// Check out whether an admin exists or not +pub fn exists(id: AdminID) -> Res { + database::QueryInfo::new(ADMIN_LIST_TABLE) + .cond_admin_id("id", id) + .exec_count() + .map(|r| r > 0) +} + /// Get admin information by ID pub fn find_admin_by_id(id: AdminID) -> Res { database::QueryInfo::new(ADMIN_LIST_TABLE)