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

Return format string with admin logs

This commit is contained in:
Pierre HUBERT 2021-07-11 13:49:59 +02:00
parent 5d97ca18cb
commit 28b24f39b0
5 changed files with 57 additions and 14 deletions

View File

@ -2,7 +2,7 @@
//!
//! @author Pierre Hubert
use crate::data::admin_action_log::{AdminActionLog, AdminAction};
use crate::data::admin_action_log::{AdminAction, AdminActionLog};
#[derive(serde::Serialize)]
pub struct AdminLogAPI {
@ -10,7 +10,8 @@ pub struct AdminLogAPI {
admin_id: u64,
ip: String,
time: u64,
action: AdminAction
action: AdminAction,
format: &'static str,
}
impl AdminLogAPI {
@ -20,7 +21,8 @@ impl AdminLogAPI {
admin_id: log.admin_id.id(),
ip: log.ip.to_string(),
time: log.time,
action: log.action.clone()
action: log.action.clone(),
format: log.action.format_string(),
}
}
}

View File

@ -62,7 +62,7 @@ pub fn challenge_register_key(r: &mut HttpRequestHandler) -> RequestResult {
/// Register key
pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult {
let name = r.post_string("name")?;
let key_name = r.post_string("name")?;
let creds = r.post_register_public_key_credential("key")?;
let state = r.some_or_internal_error(
@ -73,10 +73,14 @@ pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult {
let wan = get_wan();
let key = wan.register_credential(creds, state, |_| Ok(false))?;
admin_account_key_helper::add_key(r.admin_id()?, &name, key)?;
let key_id = admin_account_key_helper::add_key(r.admin_id()?, &key_name, key)?;
log_admin_action(r.admin_id()?, &r.remote_ip(),
AdminAction::RegisteredAdminKey { name, target: r.admin_id()? })?;
AdminAction::RegisteredAdminKey {
key_id,
key_name,
target: r.admin_id()?,
})?;
r.ok()
}
@ -93,7 +97,11 @@ pub fn delete_auth_key(r: &mut HttpRequestHandler) -> RequestResult {
for key in admin_account_key_helper::get_admin_keys(admin_id)? {
if key.id == key_id {
log_admin_action(r.admin_id()?, &r.remote_ip(),
AdminAction::DeletedAdminKey { name: key.name.to_string(), target: admin_id })?;
AdminAction::DeletedAdminKey {
key_id,
key_name: key.name.to_string(),
target: admin_id,
})?;
admin_account_key_helper::delete_key(key)?;

View File

@ -6,16 +6,49 @@ use crate::data::admin::AdminID;
pub enum AdminAction {
AuthWithResetToken,
AuthWithAccessKey { key: String, key_id: u64 },
RegisteredAdminKey { name: String, target: AdminID },
DeletedAdminKey { name: String, target: AdminID },
RegisteredAdminKey { key_id: u64, key_name: String, target: AdminID },
DeletedAdminKey { key_id: u64, key_name: String, target: AdminID },
GeneratedAdminResetToken { target: AdminID },
CreatedAdmin { id: AdminID, name: String, email: String },
UpdatedAdminGeneralSettings { target: AdminID, new_email: String, new_name: String },
AddAdminRole { target: AdminID, role: String },
RemoveAdminRole { target: AdminID, role: String },
UnsupportedAction,
UnsupportedAction { raw_data: String },
}
impl AdminAction {
pub fn format_string(&self) -> &'static str {
match self {
AdminAction::AuthWithResetToken => { "Authenticated with a reset token" }
AdminAction::AuthWithAccessKey { .. } =>
{ "Authenticated with key #{key_id} : \"{key}\"" }
AdminAction::RegisteredAdminKey { .. } =>
{ "Registered a new key #{key_id} with name \'{key_name}\' for [admin]{target}[/admin]" }
AdminAction::DeletedAdminKey { .. } =>
{ "Deleted key #{key_id} \'{key_name\' of [admin]{target}[/admin]" }
AdminAction::GeneratedAdminResetToken { .. } =>
{ "Generated new admin access reset token for [admin]{target}[/admin]" }
AdminAction::CreatedAdmin { .. } =>
{ "Created a new administrator [admin]#{id}[/admin] with name \'{name}\' and email \'email\'" }
AdminAction::UpdatedAdminGeneralSettings { .. } =>
{ "Updated general admin settings of [admin]{target}[/admin]. New name: \'{new_name}\' / New email: \'{new_email}\'" }
AdminAction::AddAdminRole { .. } =>
{ "Add a new role to [admin]{target}[/admin] => {role}" }
AdminAction::RemoveAdminRole { .. } =>
{ "Removed a role from [admin]{target}[/admin] => {role}" }
AdminAction::UnsupportedAction { .. } => { "Unsupported action. Raw data: {raw_data}" }
}
}
}
pub struct AdminActionLog {
pub id: u64,

View File

@ -11,13 +11,13 @@ use crate::helpers::database;
use crate::utils::date_utils::time;
/// Save a new key in the database
pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res {
pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res<u64> {
database::InsertQuery::new(ADMIN_KEYS_TABLE)
.add_admin_id("admin_id", id)
.add_str("name", name)
.add_u64("time_add", time())
.add_str("credential", &serde_json::to_string(&key)?)
.insert_drop_result()
.insert_expect_result()
}
/// Get the list of keys of a given admin

View File

@ -48,6 +48,6 @@ fn db_to_log(row: &database::RowResult) -> Res<AdminActionLog> {
ip: row.get_str("ip")?,
time: row.get_u64("time")?,
action: serde_json::from_str(&row.get_str("action")?)
.unwrap_or(AdminAction::UnsupportedAction),
.unwrap_or(AdminAction::UnsupportedAction { raw_data: row.get_str("action").unwrap() }),
})
}