1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Update get_admin_info route

This commit is contained in:
Pierre HUBERT 2021-05-13 08:42:59 +02:00
parent add56a9a6d
commit 830c6d0700
3 changed files with 37 additions and 3 deletions

View File

@ -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))
}

View File

@ -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<AdminID> {
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<HashSet<UserID>> {
let users = self.post_numbers_list(name, 1)?

View File

@ -24,6 +24,14 @@ pub fn create(new_admin: &NewAdmin) -> Res<AdminID> {
.map(|i| AdminID::new(i))
}
/// Check out whether an admin exists or not
pub fn exists(id: AdminID) -> Res<bool> {
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<Admin> {
database::QueryInfo::new(ADMIN_LIST_TABLE)