diff --git a/src/api_data/admin/admin_keys_api.rs b/src/api_data/admin/admin_keys_api.rs new file mode 100644 index 0000000..2693118 --- /dev/null +++ b/src/api_data/admin/admin_keys_api.rs @@ -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, + } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index 1c29937..b12dfd3 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -5,4 +5,5 @@ pub mod admin_auth_options; pub mod admin_auth_success; pub mod admin_id_api; -pub mod admin_info_api; \ No newline at end of file +pub mod admin_info_api; +pub mod admin_keys_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 0ca82b6..a6d255f 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -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_id_api::AdminIDAPI; 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::base_request_handler::BaseRequestHandler; 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()?)?, 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 { - // TODO - unimplemented!(); - } + admin_account_helper::find_admin_by_id(admin_id)? } }; 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::>()) +} + /// Update general settings pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult { let admin_id = r.post_admin_id("id")?; diff --git a/src/data/admin.rs b/src/data/admin.rs index ebcc3d7..1bec3d8 100644 --- a/src/data/admin.rs +++ b/src/data/admin.rs @@ -43,6 +43,7 @@ pub struct AdminKey { pub id: u64, pub admin_id: AdminID, pub name: String, + pub time_add: u64, pub key: Credential, } diff --git a/src/helpers/admin_account_key_helper.rs b/src/helpers/admin_account_key_helper.rs index d3198c8..99f53d6 100644 --- a/src/helpers/admin_account_key_helper.rs +++ b/src/helpers/admin_account_key_helper.rs @@ -33,6 +33,7 @@ fn db_to_admin_key(row: &database::RowResult) -> Res { id: row.get_u64("id")?, admin_id: row.get_admin_id("admin_id")?, name: row.get_str("name")?, + time_add: row.get_u64("time_add")?, key: serde_json::from_str(&row.get_str("credential")?)?, }) } \ No newline at end of file diff --git a/src/routes.rs b/src/routes.rs index 3e391e6..c999597 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -352,6 +352,7 @@ pub fn get_routes() -> Vec { 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/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/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)),