1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-25 23:09:22 +00:00

Can get the list of keys of an admin

This commit is contained in:
Pierre HUBERT 2021-05-14 13:34:13 +02:00
parent 3c4a5a53a1
commit 22b3a73db3
6 changed files with 44 additions and 8 deletions

View File

@ -0,0 +1,23 @@
//! # Admin keys API
//!
//! @author Pierre Hubert
use serde::Serialize;
use crate::data::admin::AdminKey;
#[derive(Serialize)]
pub struct AdminKeyAPI {
id: u64,
name: String,
time_add: u64,
}
impl AdminKeyAPI {
pub fn new(key: &AdminKey) -> Self {
Self {
id: key.id,
name: key.name.to_string(),
time_add: key.time_add,
}
}
}

View File

@ -6,3 +6,4 @@ pub mod admin_auth_options;
pub mod admin_auth_success; pub mod admin_auth_success;
pub mod admin_id_api; pub mod admin_id_api;
pub mod admin_info_api; pub mod admin_info_api;
pub mod admin_keys_api;

View File

@ -8,6 +8,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_auth_success::AdminAuthSuccess;
use crate::api_data::admin::admin_id_api::AdminIDAPI; use crate::api_data::admin::admin_id_api::AdminIDAPI;
use crate::api_data::admin::admin_info_api::AdminInfoAPI; use crate::api_data::admin::admin_info_api::AdminInfoAPI;
use crate::api_data::admin::admin_keys_api::AdminKeyAPI;
use crate::data::admin::{AdminKey, NewAdminGeneralSettings}; use crate::data::admin::{AdminKey, NewAdminGeneralSettings};
use crate::data::base_request_handler::BaseRequestHandler; use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::error::Res; use crate::data::error::Res;
@ -82,19 +83,27 @@ pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult {
false => admin_account_helper::find_admin_by_id(r.admin_id()?)?, false => admin_account_helper::find_admin_by_id(r.admin_id()?)?,
true => { true => {
let admin_id = r.post_admin_id("id")?; let admin_id = r.post_admin_id("id")?;
if admin_id == r.admin_id()? {
admin_account_helper::find_admin_by_id(admin_id)? admin_account_helper::find_admin_by_id(admin_id)?
} else {
// TODO
unimplemented!();
}
} }
}; };
r.set_response(AdminInfoAPI::new(&admin)) r.set_response(AdminInfoAPI::new(&admin))
} }
/// Get an admin keys
pub fn get_keys_list(r: &mut HttpRequestHandler) -> RequestResult {
let admin_id = r.post_admin_id("id")?;
if admin_id != r.admin_id()? {
// TODO : implement
unimplemented!();
}
let keys = admin_account_key_helper::get_admin_keys(admin_id)?;
r.set_response(keys.iter().map(AdminKeyAPI::new).collect::<Vec<AdminKeyAPI>>())
}
/// Update general settings /// Update general settings
pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult { pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult {
let admin_id = r.post_admin_id("id")?; let admin_id = r.post_admin_id("id")?;

View File

@ -43,6 +43,7 @@ pub struct AdminKey {
pub id: u64, pub id: u64,
pub admin_id: AdminID, pub admin_id: AdminID,
pub name: String, pub name: String,
pub time_add: u64,
pub key: Credential, pub key: Credential,
} }

View File

@ -33,6 +33,7 @@ fn db_to_admin_key(row: &database::RowResult) -> Res<AdminKey> {
id: row.get_u64("id")?, id: row.get_u64("id")?,
admin_id: row.get_admin_id("admin_id")?, admin_id: row.get_admin_id("admin_id")?,
name: row.get_str("name")?, name: row.get_str("name")?,
time_add: row.get_u64("time_add")?,
key: serde_json::from_str(&row.get_str("credential")?)?, key: serde_json::from_str(&row.get_str("credential")?)?,
}) })
} }

View File

@ -352,6 +352,7 @@ pub fn get_routes() -> Vec<Route> {
Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)), Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)),
Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)),
Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)), Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)),
Route::admin_post("/admin/accounts/keys", Box::new(admin_account_controller::get_keys_list)),
Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)), Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)),
Route::admin_post("/admin/accounts/challenge_register_key", Box::new(admin_account_controller::challenge_register_key)), Route::admin_post("/admin/accounts/challenge_register_key", Box::new(admin_account_controller::challenge_register_key)),
Route::admin_post("/admin/accounts/register_key", Box::new(admin_account_controller::register_key)), Route::admin_post("/admin/accounts/register_key", Box::new(admin_account_controller::register_key)),